RecyclerView横向滚动视图示例 如何创建横向的RecyclerView?横向滚动视图示例

5
如何使用Recycler View创建一个水平滚动条,以显示“特点”和“规则”下的元素?
我知道可以使用两个单独的Recycler View 来显示“物业类型”和“房间数量”。但是,如果要添加更多规则和特性,并且文本长度不同,是否需要创建两个 Recycler View来显示这些元素并满足列表中项目数量不断增加的需求?
请参考以下图片: enter image description here

将您的数据“item”添加到“水平滚动条”中。 - Nikunj Paradva
使用另一个RecyclerView实现水平滚动。https://dev59.com/t5Lea4cB1Zd3GeqP12ct#34462696 - Raghunandan
3个回答

5
您可以通过设置水平布局管理器来创建一个横向滚动视图。
recyclerViewTeams.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));

示例代码

 <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewProperty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />

在Java类中

 private RecyclerView recyclerViewProperty;


 recyclerViewProperty = (RecyclerView) view.findViewById(R.id.recyclerViewProperty);

recyclerViewTeams.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));

公共类TeamsAdapter扩展RecyclerView.Adapter {

ArrayList<TeamsListingModal> arrayList;
private Activity mContext;
private int selected_position;
private boolean isSelected = false;

public class MyView extends RecyclerView.ViewHolder {

    public TextView textview;
    private RelativeLayout parentLayout;


    public MyView(View view) {
        super(view);
        parentLayout = (RelativeLayout) view.findViewById(R.id.parentLayout);
        textview = (TextView) view.findViewById(R.id.textview);

    }
}


    public TeamsAdapter(Activity activity, ArrayList<CustomModal> arrayList) {
        this.mContext = activity;
        this.arrayList = arrayList;

    }

    @Override
    public MyView onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.property_adapter_item, parent, false);

        return new MyView(itemView);
    }

    @Override
    public void onBindViewHolder(final MyView holder, final int position) {
        //inflate views here

    }

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

}

此处涉及到XML和property_adapter_item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="70dp"
        android:layout_height="70dp"
        app:border_width="5dp" />
</RelativeLayout>

4
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);

3
请不要仅仅发布代码作为答案,同时提供解释你的代码做了什么以及如何解决问题。带有解释的答案通常质量更高,更容易获得赞同。 - Mark Rotteveel

0
In Kotlin Horizontal Scroolview in RecyclerView: - We use this Code 
 recycler_upcoming.layoutManager =
            LinearLayoutManager(sContext, LinearLayoutManager.HORIZONTAL, false)
        recycler_upcoming.adapter =
            InboxUpcomingAdapter(list, sContext)

-> sContext mean Ur Context
-> If you pass false then scroll right to left
-> If You pass true then scroll left to right

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接