我有以下来自 URL 的 XML,并且想要下面的布局,其中的值填充了我从 URL 获取的 XML 数据。
下面是来自 url 的 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>
使用以下代码
爪哇
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 链接的帮助下完成的