使用网格布局管理器和项目装饰器的Recycler视图

时间:2016-05-03 08:05:50

标签: android android-recyclerview recycler-adapter android-gridlayout gridlayoutmanager

我从URL获得了以下XML,并希望下面的布局中填充了我从URL获取的XML数据的值。

以下是来自网址的xml

 <Seat>
<ColumnNo>0</ColumnNo> 
<Deck>2</Deck> 
<RowNo>0</RowNo>
<SeatLabel>E</SeatLabel> 
</Seat>

<Seat>
<ColumnNo>1</ColumnNo> 
<Deck>1</Deck> 
<RowNo>0</RowNo>
<SeatLabel>SL5</SeatLabel> 
</Seat>

<Seat>
<ColumnNo>2</ColumnNo> 
<Deck>1</Deck> 
<RowNo>0</RowNo>
<SeatLabel/> 
</Seat>

<Seat>
<ColumnNo>3</ColumnNo> 
<Deck>1</Deck> 
<RowNo>0</RowNo>
<SeatLabel>SL1</SeatLabel> 
</Seat>

<Seat>
<ColumnNo>4</ColumnNo> 
<Deck>1</Deck> 
<RowNo>0</RowNo>
<SeatLabel>SL2</SeatLabel> 
</Seat>

<Seat>
<ColumnNo>5</ColumnNo> 
<Deck>2</Deck> 
<RowNo>0</RowNo>
<SeatLabel>A</SeatLabel> 
</Seat>

<Seat>
<ColumnNo>6</ColumnNo> 
<Deck>2</Deck> 
<RowNo>0</RowNo>
<SeatLabel>B</SeatLabel> 
</Seat>

<Seat>
<ColumnNo>2</ColumnNo> 
<Deck>1</Deck> 
<RowNo>1</RowNo>
<SeatLabel/> 
</Seat>

<Seat>
<ColumnNo>0</ColumnNo> 
<Deck>2</Deck> 
<RowNo>2</RowNo>
<SeatLabel>F</SeatLabel> 
</Seat>

使用以下代码

A.java

 public class A extends Activity {
          @Override
          protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recycler_view);
            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
            recyclerView.addItemDecoration(new MarginDecoration(this));
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new GridLayoutManager(this, 6));
            recyclerView.setAdapter(new NumberedAdapter(18));


          }
        }

MarginDecoration.java

public class MarginDecoration extends RecyclerView.ItemDecoration {
  private int margin;

  public MarginDecoration(Context context) {
    margin = context.getResources().getDimensionPixelSize(R.dimen.item_margin);
  }

  @Override
  public void getItemOffsets(
      Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.set(margin, margin, margin, margin);
  }
}

activity_recycler_view.xml

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/item_margin"
    android:clipToPadding="false"/>

以上所有我在Github link

的帮助下做了

1 个答案:

答案 0 :(得分:0)

你需要:

你可以找到几个tutorials寻找像&#34; recyclerview教程&#34;,&#34; xml解析android&#34;和#34; android网络库&#34;。