我正在尝试使用RecyclerView和GridLayoutManager实现类似于上图的布局,我尝试根据位置设置setSpanSizeLookup但是无法模仿上面的设计.
有人可以帮忙吗?
UPDATE

我想知道是否有办法gridlayoutmanager在 android 中自定义以具有像此草图这样的水平布局:

我尝试了一些在 Github 中找到的布局管理器,但没有一个可以帮助我实现这种布局管理器。
像这样:
public class CustomLayoutManager extends StaggeredGridLayoutManager {
public CustomLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomLayoutManager(int spanCount, int orientation) {
super(spanCount, orientation);
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
super.onMeasure(recycler, state, widthSpec, heightSpec);
}
@Override
public void setSpanCount(int spanCount) {
super.setSpanCount(spanCount);
}}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激谢谢
android layout-manager gridlayoutmanager staggeredgridlayout
我正在RecyclerView与Grid layout. 一切正常。但我需要在水平中心设置列。例如。我有 18 个项目,跨度数为 4。对于最后一行,我有 2 个项目。这两个项目应该在水平中心而不是左边。
这是我的RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:layout_gravity="center_horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rv_answer" />
Run Code Online (Sandbox Code Playgroud)
这是我的adapter layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_marginStart="@dimen/marginVSmall"
android:layout_marginEnd="@dimen/marginVSmall"
android:layout_marginTop="@dimen/marginVSmall"
android:layout_marginBottom="@dimen/marginVSmall"
android:layout_height="wrap_content">
<curvegraph.com.vijay.widgets.SquareTextView
android:id="@+id/tv_option"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/circle"
android:gravity="center"
android:text="A"
android:textStyle="bold"
android:maxLength="1"
android:textColor="@color/colorWhite"
android:textSize="@dimen/textXLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
我要设置的 Kotlin 代码 LayoutManager
rv_option.layoutManager = GridLayoutManager(this, 4)
Run Code Online (Sandbox Code Playgroud)
这是我的 Kotlin 代码:
val grid = GridLayoutManager(this, 8)
grid.spanSizeLookup = …Run Code Online (Sandbox Code Playgroud) 我'使用RecyclerView,并GridLayoutManager在我的布局5跨度数.
如何根据GridLayoutManager项目位置确定项目的跨度索引?
当我将视图旋转为横向时,RecyclerView 不会从两列切换到三列。我尝试了这段代码:
mGLManager.setSpanCount(3);
mGLManager = (GridLayoutManager)mRecyclerView.getLayoutManager();
mGLManager.setSpanCount(2);
mRecyclerView.setLayoutManager(mGLManager);
mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 3) );
Run Code Online (Sandbox Code Playgroud)
我还尝试了这些行来刷新它:
mRecyclerView.refreshDrawableState();
mAdapter.notifyDataSetChanged();
mRecyclerView.invalidate();
mRecyclerView.requestLayout();
mRecyclerView.swapAdapter(mRecyclerView.getAdapter(), true);
mRecyclerView.scrollBy(0,0);
Run Code Online (Sandbox Code Playgroud)
这是我当前的代码。
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// set the number of columns based on the orientation
if( newConfig.orientation == Configuration.ORIENTATION_PORTRAIT ) {
mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 2));
mRecyclerView.invalidate();
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
} else if( newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ) {
mRecyclerView.setLayoutManager( new GridLayoutManager(MainActivity.this, 3));
mRecyclerView.invalidate();
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
Toast 表明当我翻转方向时代码会运行。感谢您的帮助!
我需要有关多/单选择的帮助。在这里找到了我要找的东西,因为它很简单。我正在使用一个GridLayoutManager我的适配器中有超过 90 个项目,一个CardView带有 aTextView和 an ImageView,同时使用帖子中描述的过程。
当我选择一个或多个项目时,当我向下滚动时,其他项目“似乎”被选中,因为背景会复制,但它们未被选中。尝试将setOnClickListener, 放入onBindViewHolderMyViewHolder 类中,并且在它们两个类中我都得到相同的行为。向下滚动时,似乎选择了其他项目。使用notifyItemChanged(position)并notifyDataSetChanged()在适配器中,但背景根本没有改变,尽管setSelected工作正常。我setHasFixedSize(true)也在 RecyclerView 设置中使用。
在 onBindViewHolder
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, final int position) {
PatternImages currentPattern = patternImages.get(position);
myViewHolder.setData(currentPattern, position);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(!v.isSelected());
if (v.isSelected()) {
v.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimaryHighLight));
} else {
v.setBackgroundColor(Color.WHITE);
}
notifyItemChanged(position);
}
});
}
Run Code Online (Sandbox Code Playgroud)
模型
public class …Run Code Online (Sandbox Code Playgroud) android android-adapter gridlayoutmanager android-recyclerview
我打算创建一个分页滚动到底部的 RecyclerView。但是 onScrolled 回调根本没有被触发:
mBooksRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.i("Dale", "scrolled");
}
});
mBooksRecyclerView.setNestedScrollingEnabled(false);
if (Utils.hasContent(books)) {
mBooksRecyclerView.setVisibility(View.VISIBLE);
BookCardViewAdapter adapter = new BookCardViewAdapter(this, books);
final GridLayoutManager gridLayoutManager = new GridLayoutManager(BooksActivity.this, 3);
mBooksRecyclerView.setLayoutManager(gridLayoutManager);
mBooksRecyclerView.setAdapter(adapter);
emptyView.setVisibility(View.GONE);
} else {
emptyView.setVisibility(View.VISIBLE);
mBooksRecyclerView.setVisibility(View.GONE);
}
Run Code Online (Sandbox Code Playgroud)
我还尝试从 NestedScrollView 中删除 RecyclerView,但它仍然不起作用。
这是我的 XML 文件:
book_content.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="@dimen/books_category_height"
android:background="@color/gfs_blue">
<android.support.v7.widget.RecyclerView
android:id="@+id/categories_tab"
android:layout_width="match_parent"
android:layout_height="@dimen/books_category_height">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<android.support.v7.widget.RecyclerView …Run Code Online (Sandbox Code Playgroud) java android onscrolllistener gridlayoutmanager android-recyclerview
我有一个包含RecyclerView. 回收器中的每个项目都是一个简单的复选框。我需要把它画成三列。
为此,我使用 GridLayoutManager
layoutManager = GridLayoutManager(context, 3, GridLayoutManager.HORIZONTAL, false)
Run Code Online (Sandbox Code Playgroud)
我在结果中看到的是这个
不错。问题是我需要将每列的宽度设置为对话框宽度的 33%。我的复选框没有任何固定宽度(以像素为单位),因为文本可能会有所不同。这是用于回收站的布局。
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="item" type="..." />
</data>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@={item.enabled}"
android:text="@{item.name}"
android:minWidth="40dp"/>
</layout>
Run Code Online (Sandbox Code Playgroud)
我的回收器创建的宽度等于父级
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
任何想法如何以百分比设置宽度?