How to build a Horizontal ListView with RecyclerVi

2018-12-31 15:16发布

I need to implement a horizontal listview in my Android application. I did a bit of research and came across How can I make a horizontal ListView in Android? and Horizontal ListView in Android? however, these questions were asked before Recyclerview was released. Is there a better way to implement this now with Recyclerview?

11条回答
路过你的时光
2楼-- · 2018-12-31 16:07

If you want to use a RecyclerView with the GridLayoutManager, this is the way to achieve horizontal scroll.

recyclerView.setLayoutManager(
new GridLayoutManager(recyclerView.getContext(), rows, GridLayoutManager.HORIZONTAL, false));
查看更多
公子世无双
3楼-- · 2018-12-31 16:08
    <HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:scrollbars="vertical|horizontal" />
</HorizontalScrollView>
查看更多
唯独是你
4楼-- · 2018-12-31 16:13

It's for both for Horizontal and for Vertical.

RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_recycler);
    recyclerView = (RecyclerView)findViewById(R.id.recyclerViewId);

    RecyclAdapter adapter = new RecyclAdapter();

    //Vertical RecyclerView
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);

    //Horizontal RecyclerView
    //recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false));

    recyclerView.setAdapter(adapter);

}
查看更多
闭嘴吧你
5楼-- · 2018-12-31 16:16
 <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
查看更多
笑指拈花
6楼-- · 2018-12-31 16:18

Trying to build a horizontal ListView is taking too much time. I have resolved it in two ways.

1.By using a ViewPager whose adapter extends from PagerAdapter.

2.By using RecyclerView just as above. Need to apply LayoutManager as in the following code:

LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);
查看更多
登录 后发表回答