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

George picture George · Apr 3, 2018 · Viewed 7.4k times · Source

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

Answer

Quick learner picture Quick learner · Apr 3, 2018

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>