RecyclerView Horizontal Scrollview example How to create horizontal recyclerview ? Horizontal scrollview example
Asked Answered
P

3

5

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

Plumbing answered 3/4, 2018 at 12:50 Comment(2)
add your data item inside horizontal scrollbarHocus
use another recyclerview for horizontal scroll #34458623Leodora
N
4
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
Neurath answered 10/4, 2020 at 14:46 Comment(1)
Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes.Hydrothermal
P
5

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>
Promethean answered 3/4, 2018 at 13:0 Comment(0)
N
4
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
Neurath answered 10/4, 2020 at 14:46 Comment(1)
Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes.Hydrothermal
P
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
Pupa answered 25/3, 2021 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.