Recyclerview Horizontal span and spacing issues

岁酱吖の 提交于 2019-12-11 04:17:09

问题


So this is what im trying to achieve, is a recycler view with a 3x3 grid and when items exceed the 3x3 ,they spill into the next screen/page.

So ive used a recyclerview with horizontal scrolling

This is what ive achieved so far

and my current code does work when the count of items is 7 and above.

When the count is below 7 im facing issues with which i need help.

Listing my issues as questions

Question 1:

Why is the recyclerview items populated in vertical fashion?

1 4
2 5
3 6

I've set my recyclerview to scroll horizontally,

new GridLayoutManager(this, span, GridLayoutManager.HORIZONTAL, false);

Question 2:

When my item count is 6 , how do i avoid the space between the rows?

Question 3:

When my items are say 4 ,i want to show my items as

1 2 3
4

or

1 3 4
2

currently it shows as

If i set span to 1,ill have the grid as

1 2 3 4

causing 4 to go off-screen like so

1 2 3 |4

which i want to avoid.

==============

Code ive used

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="88dp"
    android:layout_height="88dp"
    android:gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/border"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@android:color/darker_gray"
        android:orientation="vertical"
        android:weightSum="1">

        <TextView
            android:id="@+id/lblTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="TEST"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/lblDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lblTitle"
            android:layout_centerHorizontal="true"
            android:text="TEST"
            android:textSize="13sp" />
    </RelativeLayout>
</LinearLayout>

Adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.CustomViewHolder> {
    Context ctx;
    List<String> listText;

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        TextView lblText;

        public CustomViewHolder(View view) {
            super(view);
            view.setClickable(true);
            lblText = (TextView) view.findViewById(R.id.lblDesc);
        }
    }

    public RecyclerViewAdapter(Context ctx, List<String> listText) {
        this.ctx = ctx;
        this.listText = listText;
    }

    @Override
    public RecyclerViewAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_child_row, parent, false);
        return new CustomViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(CustomViewHolder holder, int position) {
        holder.lblText.setText(listText.get(position));
    }


    @Override
    public int getItemCount() {
        return listText.size();
    }
}

Activity

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerTest;
    private GridLayoutManager gridLayoutRecyclerview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerTest = (RecyclerView) findViewById(R.id.recyclerTest);
        List<String> items = Arrays.asList("1", "2", "3", "4");
        /*List<String> items = Arrays.asList("1", "2", "3", "4", "5");*/
        /*List<String> items = Arrays.asList("1", "2", "3", "4", "5", "6", "7");*/
        /*List<String> items = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");*/
        /*List<String> items = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13","14");*/
        /*List<String> items = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12","13","14","15");*/
         /*List<String> items = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12","13","14","15","16","17");*/    

        int span = 0;    
        if (items.size() <= 3) {
            span = 1;
        } else if (items.size() <= 6) {
            span = 2;
        } else {
            span = 3;
        }

        gridLayoutRecyclerview = new GridLayoutManager(this, span, GridLayoutManager.HORIZONTAL, false);

        recyclerTest.setLayoutManager(gridLayoutRecyclerview);

        SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
        snapHelper.attachToRecyclerView(recyclerTest);

        recyclerTest.setAdapter(new RecyclerViewAdapter(this, items));
    }
}

回答1:


Why is the recyclerview items populated in vertical fashion?

1 4
2 5
3 6

A RecyclerView is intended for Lists that scroll. If you have a horizontal layout, it will fill the first column, followed by the second, then the third, etc. After all, that's how we would expect a list to work. And that's why the "numbering is off". It starts with the first column, going on to the next once it is filled.

When my item count is 6 , how do i avoid the space between the rows?

I don't know your setup, but I'm guessing your recyclerviews height is set to match_parent, so it's probably just using up all the available height. wrap_content height for the recyclerview might help.

When my items are say 4 ,i want to show my items as

1 2 3
4

or

1 3 4
2

Again, see above, that's not how a RecyclerView works. It will always fill the columns one by one, and not start with a row. 4 Items with a column count of 2 will always fill the first columns first, like below.

1 3
2 4

A RecyclerView might not fit all your needs. You could switch to a vertical grid layout with 3 columns with less than 9 items. That way it would not scroll horizontally, and it would even fill the views up like you described. For more than 9 items you'd still show the horizontal version and have it scroll.

What you're actually describing is a custom layout: You want a view that behaves in a specific fashion. You could have a look on how to create your own ViewGroup and use that to lay out all the views however you like. Unless you want to show long lists, that might be a good alternative option.



来源:https://stackoverflow.com/questions/46206283/recyclerview-horizontal-span-and-spacing-issues

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