如何使用GridLayoutManager使用RecyclerView设置列间距?在我的布局中设置边距/填充无效.
android gridview gridlayoutmanager recycler-adapter android-recyclerview
我知道,RecyclerView
已经取代了旧的功能ListView
和GridView
.我正在寻找一个非常基本的例子,它显示了使用的最小网格设置RecyclerView
.我不是在寻找长篇教程风格的解释,只是一个最小的例子.我想最简单的模仿旧GridView的网格将包含以下功能:
使用新的GridLayoutManager:https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html
它需要一个明确的跨度计数,所以问题现在变成:你怎么知道每行有多少"跨度"?毕竟这是一个网格.根据测量的宽度,RecyclerView可以容纳多个跨距.
使用旧的GridView
,您只需设置"columnWidth"属性,它将自动检测适合的列数.这基本上是我想要为RecyclerView复制的:
RecyclerView
这似乎是很常见的行为,所以有一种我没有看到的更简单的方法吗?
android android-gridlayout gridlayoutmanager android-recyclerview
我想用节标题实现类似网格的布局.想想https://github.com/TonicArtos/StickyGridHeaders
我现在应该做什么:
mRecyclerView = (RecyclerView) view.findViewById(R.id.grid);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case MyAdapter.TYPE_HEADER:
return 1;
case MyAdapter.TYPE_ITEM:
return 2;
default:
return -1;
}
}
});
mRecyclerView.setLayoutManager(mLayoutManager);
Run Code Online (Sandbox Code Playgroud)
现在常规项和标题的跨度大小为1.我该如何解决这个问题?
android android-support-library gridlayoutmanager android-recyclerview
我试图在增加最后一个元件行下面的间距RecyclerView
有GridLayoutManager
.ItemDecoration
为了这个目的,我使用自定义底部填充,当它的最后一个元素如下:
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
private int bottomSpace = 0;
public SpaceItemDecoration(int space, int bottomSpace) {
this.space = space;
this.bottomSpace = bottomSpace;
}
public SpaceItemDecoration(int space) {
this.space = space;
this.bottomSpace = 0;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
int childCount = parent.getChildCount();
final int itemPosition = parent.getChildAdapterPosition(view);
final int itemCount = state.getItemCount();
outRect.left = space;
outRect.right = space;
outRect.bottom = space; …
Run Code Online (Sandbox Code Playgroud) 我尝试使用方形图像制作网格布局.我认为必须有可能GridLayoutManager
通过操纵onMeasure
来操纵
super.onMeasure(recycler, state, widthSpec, widthSpec);
Run Code Online (Sandbox Code Playgroud)
代替
super.onMeasure(recycler, state, widthSpec, heightSpec);
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这没有用.
有任何想法吗?
我正在使用基本的RecyclerView和GridLayoutManager.我观察到,smoothScrollToPosition和scrollToPosition也没有正常工作.
a)使用时smoothScrollToPosition
我经常收到错误RecyclerView
"RecyclerView:平滑滚动时传递到目标位置."
并且RecyclerView
没有正确滚动(通常它会错过目标行).这主要是在我试图滚动到某行的第一项时
b)使用scrollToPosition
它时似乎工作得很好但是大多数时候我只能看到行的第一项而其余的都没有显示.
你能给我一些提示,如何正确地完成至少一种方法吗?
非常感谢!
android scroll smooth-scrolling gridlayoutmanager android-recyclerview
我有垂直RecyclerView
使用GridLayoutManager
.我希望每列都居中,但列始于左侧.在下图中,您可以看到我在说什么.我用丑陋的配色方案来说明列和背景.绿色是每个项目的背景RecyclerView
,红色是RecyclerView
自身的背景:
我正在设置:
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
Run Code Online (Sandbox Code Playgroud)
这是column_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="180dp"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="@+id/movie_column_photo"
android:layout_width="80dp"
android:layout_height="120dp"/>
<TextView
android:id="@+id/movie_column_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是recyclerview xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/company_details_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我想要实现的目标:拥有一个带有GridLayoutManager的RecyclerView,它支持drag'n'drop并在拖动时重新排列项目.
旁注:第一次用拖放开发任何东西.
有很多关于如何使用的ListView来实现这一功能的主题,例如:https://raw.githubusercontent.com/btownrippleman/FurthestProgress/master/FurthestProgress/src/com/anappforthat/android/languagelineup/DynamicListView.java
然而,通常的例子是很多的代码,创建拖累视图的位图和感觉应该可以使用来实现相同的结果View.startDrag(...)
,并与RecyclerView notifyItemAdded()
,notifyItemMoved()
而且notifyItemRemoved()
因为它们提供了重新排列动画.
所以我玩了一些并想出了这个:
final CardAdapter adapter = new CardAdapter(list);
adapter.setHasStableIds(true);
adapter.setListener(new CardAdapter.OnLongClickListener() {
@Override
public void onLongClick(View view) {
ClipData data = ClipData.newPlainText("","");
View.DragShadowBuilder builder = new View.DragShadowBuilder(view);
final int pos = mRecyclerView.getChildAdapterPosition(view);
final Goal item = list.remove(pos);
mRecyclerView.setOnDragListener(new View.OnDragListener() {
int prevPos = pos;
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
final int action = dragEvent.getAction();
switch(action) {
case DragEvent.ACTION_DRAG_LOCATION:
View onTopOf = mRecyclerView.findChildViewUnder(dragEvent.getX(), dragEvent.getY());
int i …
Run Code Online (Sandbox Code Playgroud) android drag-and-drop gridlayoutmanager android-recyclerview
我对RecyclerView的ItemTouchHelper有疑问.
我正在制作一个游戏.游戏板实际上是RecyclerView.RecyclerView的GridLayoutManager具有一些跨度计数.我想实现拖放 recyclerview的项目.任何项目都可以拖动所有方向(向上,向下,向左,向右).
private void initializeLayout() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutFrozen(true);
recyclerView.setNestedScrollingEnabled(false);
// set layout manager
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), BOARD_SIZE,
LinearLayoutManager.VERTICAL, true);
recyclerView.setLayoutManager(layoutManager);
// Extend the Callback class
ItemTouchHelper.Callback itemTouchCallback = new ItemTouchHelper.Callback() {
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Log.w(TAG, "onMove");
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Application does not include swipe feature.
}
@Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
int …
Run Code Online (Sandbox Code Playgroud) android drag-and-drop gridlayoutmanager android-recyclerview