В представлении Recycler элементы расположены в сетке, которая прокручивается горизонтально
У меня есть набор значков, которые мне нужно отобразить в RecyclerView
расположены в сетке, и заставьте эту сетку прокручиваться горизонтально. Обычно я бы использовал GridLayoutManager
что-то вроде этого, но мои требования мешают мне сделать это. А именно, не только то, что элементы должны быть расположены в сетке, но они также должны быть добавлены в сетку строка за строкой, а не столбец за столбцом (вот как GridLayoutManager
Является ли). Так, например, если сетка имеет размер 3х3, и у меня есть 4 элемента, они не должны занимать позиции 1, 4, 7 и 2. Но позиции 1, 2, 3 и 4.
Любые альтернативы RecyclerView
это позволило бы мне сделать эту работу тоже приветствуется.
1 ответ
Вы могли бы рассмотреть возможность использования HorizontalScrollView
для каждого из ваших рядов. Я даю вам пример реализации.
Вы должны иметь макет элемента, чтобы быть в каждой строке первым. Позвольте нам иметь макет, подобный следующему.
Давайте иметь такой макет. Давайте назовем это item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some title" />
</LinearLayout>
Теперь позвольте нам иметь View
класс для этого макета, который будет загружен динамически.
public class CustomItemView extends LinearLayout {
private Context context;
private TextView mTextView;
public CustomItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public CustomItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public CustomItemView(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
this.context = context;
View v = inflate(context, R.layout.item_layout, null);
mTextView = (TextView) v.findViewById(R.id.title);
addView(v);
}
public void setTitle(String title) {
mTextView.setText(title);
}
}
Теперь давайте создадим класс для заполнения представлений внутри HorizontalScrollView
,
public class MyHorizontalScrollView {
HorizontalScrollView horizontalScrollView;
LinearLayout linearLayout;
Context context;
public MyHorizontalScrollView(final Context context) {
this.context = context;
initScrollView();
}
private void initScrollView() {
horizontalScrollView = new HorizontalScrollView(context);
HorizontalScrollView.LayoutParams params = new HorizontalScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalScrollView.setLayoutParams(params);
horizontalScrollView.setHorizontalScrollBarEnabled(false);
linearLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.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) {
CustomItemView customItemView = new CustomItemView(context);
customItemView.setTitle(newTitle);
linearLayout.addView(customItemView);
return customItemView;
}
}
Теперь получите LinearLayout
в качестве держателя вида прокрутки и добавления видов на основе элементов, добавляемых в каждую строку.
В макете деятельности вашего Activity
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fillViewport="true">
<LinearLayout
android:id="@+id/scrollViewHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
Теперь просто добавьте свой HorizontalRecyclerView
к этому LinearLayout
просматривая свой список, трижды за один раз.
private LinearLayout mScrollViewHolder;
mScrollViewHolder = (LinearLayout) v.findViewById(R.id.scrollViewHolder);
MyHorizontalScrollView myHorizontalScrollView = new MyHorizontalScrollView(this.getContext());
myHorizontalScrollView.addHorizontalScrollView(mScrollViewHolder);
myHorizontalScrollView.addNewEntryView("Something");