回收者视图在水平滚动的网格中排列项目 [英] Recycler view arrange items in a grid that scrolls horizontally

查看:97
本文介绍了回收者视图在水平滚动的网格中排列项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组图标需要显示在网格中排列的RecyclerView中,并使该网格水平滚动.通常,我会使用GridLayoutManager进行此类操作,但是我的要求使我无法执行此操作.即,不仅必须将项目排列在网格中,而且还需要将它们逐行而不是逐列添加到网格中(这是GridLayoutManager的操作方式).因此,例如,如果网格为3x3且我有4个项目,则它们不应采用位置1、4、7和2,而是位置1、2、3和4.

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.

也欢迎使用RecyclerView的任何替代方法,使我能够完成这项工作.

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

推荐答案

您可能考虑为每行使用HorizontalScrollView.我给您一个示例实现.

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.

让我们拥有这样的布局.让我们将其命名为item_layout.xml

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>

现在让我们有一个View类,可以动态加载此布局.

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

现在让我们拥有一个用于填充HorizontalScrollView内部视图的类.

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

现在获得LinearLayout作为滚动视图持有者,并根据要在每行中添加的项目将视图添加到.

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

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>

现在,只需遍历列表一次即可将HorizontalRecyclerView添加到此LinearLayout中.

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

这篇关于回收者视图在水平滚动的网格中排列项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆