Recycler view arrange items in a grid that scrolls horizontally

寵の児 提交于 2019-12-11 16:57:46

问题


I have set of icons that I need to display in a RecyclerView arranged in a grid, and make that grid scroll horizontally. Normally I would use GridLayoutManager for something like this, but my requirements are preventing me from doing this. Namely, not only that the items have to be arranged in a grid, but they also need to be added to the grid row by row and not column by column (this is how GridLayoutManager does it). So for example, if the grid is is 3x3 and I have 4 items, they shouldn't take positions 1, 4, 7 and 2. But positions 1, 2, 3 and 4.

Any alternatives to the RecyclerView that would let me make this work are welcome too.


回答1:


You might consider using HorizontalScrollView for each of your rows. I am giving you a sample implementation.

You need to have the layout of the item to be in each row first. Let us have a layout like the following.

Let us have a layout like this. Let us name it item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some title" />
</LinearLayout>

Now let us have a View class for this layout to be loaded dynamically.

public class CustomItemView extends LinearLayout {
    private Context context;
    private TextView mTextView;

    public CustomItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    public CustomItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomItemView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;

        View v = inflate(context, R.layout.item_layout, null);
        mTextView = (TextView) v.findViewById(R.id.title);

        addView(v);
    }

    public void setTitle(String title) {
        mTextView.setText(title);
    }
}

Now let us have a class to populate the views inside HorizontalScrollView.

public class MyHorizontalScrollView {
    HorizontalScrollView horizontalScrollView;
    LinearLayout linearLayout;
    Context context;

    public MyHorizontalScrollView(final Context context) {
        this.context = context;
        initScrollView();
    }

    private void initScrollView() {
        horizontalScrollView = new HorizontalScrollView(context);
        HorizontalScrollView.LayoutParams params = new HorizontalScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        horizontalScrollView.setLayoutParams(params);
        horizontalScrollView.setHorizontalScrollBarEnabled(false);

        linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(linearLayoutParams);

        horizontalScrollView.addView(linearLayout);
    }

    public LinearLayout addHorizontalScrollView(LinearLayout linearLayout) {
        linearLayout.addView(horizontalScrollView);
        return linearLayout;
    }

    public CustomItemView addNewEntryView(final String newTitle) {
        CustomItemView customItemView = new CustomItemView(context);
        customItemView.setTitle(newTitle);
        linearLayout.addView(customItemView);

        return customItemView;
    }
}

Now get a LinearLayout as the scroll view holder and add views to the based on the items to be added in each row.

In the activity layout of your Activity

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/scrollViewHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>

Now just add your HorizontalRecyclerView to this LinearLayout by looping through your list, thrice at a time.

private LinearLayout mScrollViewHolder;
mScrollViewHolder = (LinearLayout) v.findViewById(R.id.scrollViewHolder);

MyHorizontalScrollView myHorizontalScrollView = new MyHorizontalScrollView(this.getContext());
myHorizontalScrollView.addHorizontalScrollView(mScrollViewHolder);
myHorizontalScrollView.addNewEntryView("Something");


来源:https://stackoverflow.com/questions/49560782/recycler-view-arrange-items-in-a-grid-that-scrolls-horizontally

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