RecyclerView GridLayoutManager: change column width

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

I have a RecyclerView and a GridLayoutManager. The columns width are equal but I want the first column with 80dp width. Is this possible?

Example: RecyclerView width 5 columns

final columnsCount = 5;  recyclerView.width = 500dp  column0.width = 80dp  column 1-2-3-4 width = (recyclerview.width - column0.width) / columnsCount 

Any advice?

Edit: code

public class TestRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {     private static final int VIEW_TYPE_START = 0;     private static final int VIEW_TYPE_PROGRESS_ITEM = 1;     private LayoutInflater inflater;      public TestRecyclerViewAdapter(Context context) {         this.inflater = LayoutInflater.from(context);     }      @Override     public int getItemViewType(int position) {         if (position == 0) {             return VIEW_TYPE_START;         } else {             return VIEW_TYPE_PROGRESS_ITEM;         }     }      @Override     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {         final View view;         switch (viewType) {             case VIEW_TYPE_START:                 view = this.inflater.inflate(R.layout.view_item_small, parent, false);                 return new TestViewHolder(view);             case VIEW_TYPE_PROGRESS_ITEM:                 view = this.inflater.inflate(R.layout.view_item, parent, false);                 return new TestViewHolder(view);         }         return null;     }      @Override     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {     }      @Override     public int getItemCount() {         return 5;     } } 

view_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000FF" android:orientation="vertical"> 

view_item_small.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="80dp" android:layout_height="match_parent" android:background="#FF0000" android:orientation="vertical"> 

Usage:

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);      RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);     recyclerView.setAdapter(new TestRecyclerViewAdapter(this));     recyclerView.setLayoutManager(new GridLayoutManager(this, 5)); } 

回答1:

in your adapter you can do that.

check if the item's position equals 0, if so then inflate the layout with 80dp width.



回答2:

Create a second layout with a 80dp width to use in your RecyclerView.Adapter, then in the onCreateViewHolder() method check if the item's position equals 0, if so then inflate the layout with 80dp width, else inflate the normal layout. You should create your own viewTypes to do so, you will have to override the getItemViewType(int position) method for that, this will look something like this:

private static final int VIEWTYPE_FIRST_ITEM = 1; private static final int VIEWTYPE_NORMAL_ITEM = 2;  @Override public int getItemViewType(int position) {     if(position == 0) {         return VIEWTYPE_FIRST_ITEM;     } else {         return VIEWTYPE_NORMAL_ITEM;     } }  @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {     View view;     if(viewType == VIEWTYPE_FIRST_ITEM) {         view =  //TODO inflate layout with 80dp     } else {         view =  //TODO inflate normal layout     }     return new ViewHolder(view); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!