【问题标题】:Recycler View With Grid layout Manager and item decorator带有网格布局管理器和项目装饰器的回收站视图
【发布时间】:2016-05-03 08:05:49
【问题描述】:

我有以下来自 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>

下面的代码

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的帮助下完成了以上所有工作

【问题讨论】:

  • 你的问题是什么?

标签: android android-recyclerview android-gridlayout gridlayoutmanager


【解决方案1】:

你需要:

你可以找到几个tutorials 寻找诸如“recyclerview 教程”、“xml 解析 android”和“android 网络库”之类的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    相关资源
    最近更新 更多