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?
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.
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.)
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>
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With