Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView Horizontal Scrollview example How to create horizontal recyclerview ? Horizontal scrollview example

How can I create a horizontal scroll using a recycler view, for the elements under Features and Rules?

I do know that for Property type and Number of rooms I can use two separate recycler views. However, Would I need to create two recycler views to display those elements since the length of the text elements will be uneven and to cater for an ever-expanding list of items if more rules and features are added?

enter image description here

like image 249
George Avatar asked Apr 03 '18 12:04

George


People also ask

How do you make a RecyclerView horizontal in XML?

In xml file In your layout xml file set orientation to horizontal and layoutManager to the one of LinearLayoutManager , GridLayoutManager , StaggeredGridLayoutManager . Choose according to your requirement.

How do I add space between items in RecyclerView?

The simplest way is to add top/bottom margins around the first item in the adapter's row. android:layout_marginBottom="4dp". (Note adding the margins to the parent layout won't cut it.)


2 Answers

You can create a horizontal scrollview by doing this ie set horizontal layout manager

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

Example code

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

in Java class

 private RecyclerView recyclerViewProperty;


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

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

public class TeamsAdapter extends 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();
    }

}

and xml here 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>
like image 124
Quick learner Avatar answered Nov 15 '22 06:11

Quick learner


LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
like image 23
Meysam Keshvari Avatar answered Nov 15 '22 06:11

Meysam Keshvari