RecyclerView GridLayoutManager:更改列宽

时间:2016-05-09 14:31:51

标签: android android-recyclerview gridlayoutmanager

我有一个RecyclerView和一个GridLayoutManager。 列宽相等,但我希望第一列的宽度为80dp。 这可能吗?

  

示例:RecyclerView width 5 columns

final columnsCount = 5;

recyclerView.width = 500dp

column0.width = 80dp

column 1-2-3-4 width = (recyclerview.width - column0.width) / columnsCount

enter image description here

有什么建议吗?

编辑:代码

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">

用法:

@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));
}

2 个答案:

答案 0 :(得分:0)

您可以在适配器中执行此操作。

检查项目的位置是否等于0,如果是,则以80dp宽度夸大布局。

答案 1 :(得分:0)

创建第二个布局,宽度为80dp,以便在RecyclerView.Adapter中使用,然后在onCreateViewHolder()方法中检查项目的位置是否等于0,如果是,则使用80dp对布局进行充气宽度,否则膨胀正常布局。 你应该创建自己的viewTypes,你必须覆盖getItemViewType(int position)方法,这看起来像这样:

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);
}