Recycler View Arrange Items In A Grid That Scrolls Horizontally
I have set of icons that I need to display in a RecyclerView arranged in a grid, and make that grid scroll horizontally. Normally I would use GridLayoutManager for something like t
Solution 1:
You might consider using HorizontalScrollView
for each of your rows. I am giving you a sample implementation.
You need to have the layout of the item to be in each row first. Let us have a layout like the following.
Let us have a layout like this. Let us name it item_layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Some title" /></LinearLayout>
Now let us have a View
class for this layout to be loaded dynamically.
publicclassCustomItemViewextendsLinearLayout {
private Context context;
private TextView mTextView;
publicCustomItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
publicCustomItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
publicCustomItemView(Context context) {
super(context);
initView(context);
}
privatevoidinitView(Context context) {
this.context = context;
Viewv= inflate(context, R.layout.item_layout, null);
mTextView = (TextView) v.findViewById(R.id.title);
addView(v);
}
publicvoidsetTitle(String title) {
mTextView.setText(title);
}
}
Now let us have a class to populate the views inside HorizontalScrollView
.
publicclassMyHorizontalScrollView {
HorizontalScrollView horizontalScrollView;
LinearLayout linearLayout;
Context context;
publicMyHorizontalScrollView(final Context context) {
this.context = context;
initScrollView();
}
privatevoidinitScrollView() {
horizontalScrollView = newHorizontalScrollView(context);
HorizontalScrollView.LayoutParamsparams=newHorizontalScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalScrollView.setLayoutParams(params);
horizontalScrollView.setHorizontalScrollBarEnabled(false);
linearLayout = newLinearLayout(context);
LinearLayout.LayoutParamslinearLayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
horizontalScrollView.addView(linearLayout);
}
public LinearLayout addHorizontalScrollView(LinearLayout linearLayout) {
linearLayout.addView(horizontalScrollView);
return linearLayout;
}
public CustomItemView addNewEntryView(final String newTitle) {
CustomItemViewcustomItemView=newCustomItemView(context);
customItemView.setTitle(newTitle);
linearLayout.addView(customItemView);
return customItemView;
}
}
Now get a LinearLayout
as the scroll view holder and add views to the based on the items to be added in each row.
In the activity layout of your Activity
<ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/white"android:fillViewport="true"><LinearLayoutandroid:id="@+id/scrollViewHolder"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" /></ScrollView>
Now just add your HorizontalRecyclerView
to this LinearLayout
by looping through your list, thrice at a time.
private LinearLayout mScrollViewHolder;
mScrollViewHolder = (LinearLayout) v.findViewById(R.id.scrollViewHolder);
MyHorizontalScrollViewmyHorizontalScrollView=newMyHorizontalScrollView(this.getContext());
myHorizontalScrollView.addHorizontalScrollView(mScrollViewHolder);
myHorizontalScrollView.addNewEntryView("Something");
Post a Comment for "Recycler View Arrange Items In A Grid That Scrolls Horizontally"