我正在做一个待办列表的应用程序,并在测试它,出于某种原因,一个巨大的缺口,每当我尝试向下滚动项目之间形成。它总是会发生,每当我拖放项目,但我没有看到任何错误,我ItemTouchHelper适配器和回调类。这将是真棒,如果你能帮助我。
之前:
后:
recycler adapter.Java
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.RecyclerVH> implements ItemTouchHelperAdapter{
private LayoutInflater layoutInflater;
ArrayList<Info> data;
Context context;
public RecyclerAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
this.context = context;
}
public void setData(ArrayList<Info> data) {
this.data = data;
notifyDataSetChanged();
}
@Override
public RecyclerVH onCreateViewHolder(ViewGroup viewGroup, int position) {
View view = layoutInflater.inflate(R.layout.custom_row, viewGroup, false);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("R.A onClick Owen", "onClick method triggered");
}
});
RecyclerVH recyclerVH = new RecyclerVH(view);
return recyclerVH;
}
@Override
public void onBindViewHolder(RecyclerVH recyclerVH, int position) {
Log.d("RecyclerView", "onBindVH called: " + position);
final Info currentObject = data.get(position);
// Current Info object retrieved for current RecyclerView item - USED FOR DELETE
recyclerVH.listTitle.setText(currentObject.title);
recyclerVH.listContent.setText(currentObject.content);
/*recyclerVH.listTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Open new Activity containing note content
Toast.makeText(this, "Opening: " + currentObject.title, Toast.LENGTH_LONG).show();
}
});*/
}
public void deleteItem(int position) {
DBInfo dbInfo = new DBInfo(context);
dbInfo.deleteNote(data.get(position));
// Deletes RV item/position's Info object
data.remove(position);
// Removes Info object at specified position
notifyItemRemoved(position);
// Notifies the RV that item has been removed
}
@Override
public int getItemCount() {
return data.size();
}
// This is where the Swipe and Drag-And-Drog methods come into place
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
// Swapping positions
// ATTEMPT TO UNDERSTAND WHAT IS GOING ON HERE
Collections.swap(data, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onItemDismiss(int position) {
// Deleting item from RV and DB
deleteItem(position);
}
class RecyclerVH extends RecyclerView.ViewHolder implements View.OnClickListener{
// OnClickListener is implemented here
// Can also be added at onBindViewHolder above
TextView listTitle, listContent;
public RecyclerVH(View itemView) {
super(itemView);
listTitle = (TextView) itemView.findViewById(R.id.title);
listContent = (TextView) itemView.findViewById(R.id.content);
listTitle.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(context, "Opening: Note" + getLayoutPosition(), Toast.LENGTH_SHORT).show();
// PS NEVER ADD listTitle VARIABLE AS PUBLIC VARIABLE ABOVE WHICH IS GIVEN VALUE AT ONBINDVH
// THIS IS BECAUSE THE VALUE WILL CHANGE IF ITEM IS ADDED OR DELETED
}
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:gravity="bottom|end"
android:onClick="addNote"
android:src="@drawable/fab_ic_add"
fab:fab_colorNormal="@color/colorPrimary"
fab:fab_colorPressed="@color/colorPrimaryDark"
fab:fab_colorRipple="@color/colorPrimaryDark" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
custom_row.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">
<LinearLayout
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="@string/test"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/main"
android:paddingLeft="8dp">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
非常感谢你对谁可以帮我。我拉我的头发,我的类型。
编辑:我已经证实,这不是我的ItemTouchHelper类这就是问题所在。 (试过没有它被称为运行时,仍然出现问题。)另外,似乎当显示对话框和键盘长大了,在后台RecyclerView自行解决该问题。对话框被移除之后,该问题重复(即滚动放项之间大量的空间)
Luksprog的回答加布里埃莱马里奥蒂的答案作品。
按照doc
随着版本2.3.0有一个令人兴奋的新功能的布局管理API:自动测量!这使得基于其内容的大小RecyclerView大小本身。 >这意味着,先前不可用的情况下,诸如使用WRAP_CONTENT>为RecyclerView的尺寸,现在都是可能的。你会发现所有内置的布局管理现在支持自动测量。
由于这种变化,一定要仔细检查你的项目视图的布局参数:以前被忽视的布局参数(如MATCH_PARENT在滚动方向)现在将得到充分的尊重。
在您的项目布局,你必须改变:
android:layout_height="match_parent"
同
android:layout_height="wrap_content"
在回收站视图match_parent变化WRAP_CONTENT:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
在项目布局XML还改变
让家长布局高度match_parent为wrap_content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
这件事给了我很多次!
所有你需要做的是...使排文件WRAP_CONTENT和解决你的问题的layout_height ..
android:layout_height="wrap_content"
编码愉快!
它的,因为你正在使用match_parent在您的垂直取向的ListView / recyclerview行项目的根视图的高度。当您使用该项目扩展完全WRT其父。当recyclerview竖直定向和宽度当其horizantally取向使用WRAP_CONTENT为高度。
我有这个问题,我只是用户
android:layout_height="wrap_content"
为每个项目的母公司。
避免采取在项目布局视图用这样的容器:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
</android.support.constraint.ConstraintLayout>
</layout>
在这种情况下match_parent
将尽自己的工作和问题将依然存在!而是采取这样的:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
</layout>
[注:上面的代码数据绑定连接到它,不要如果不使用数据绑定使用<layout>
&<data>
标签]
除此之外,如果你必须使用任何视图容器群,不是采取看看在容器中的高度和宽度属性,并尝试改变那些从match_parent
到wrap_content
。这应该解决的问题。更多的透明度,可以试着给背景颜色和识破它来确定实际的问题。
只是为了记录:因为在我的情况下,我们不得不实施一些“superfancy UI”重叠RecyclerView和模糊效果的工具栏,我不得不让clipToPadding="false"
-XML中摆脱RecyclerView
的。
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/height_toolbar"
android:paddingBottom="@dimen/height_bottom_bar_container"
//android:clipToPadding="false" <-- remove this flag!
android:scrollbars="vertical"
/>
paddingTop
and paddingBottom
的工作,但我们有一些GapViews取代它(空观点,与paddingTop和paddingBottom来的高度)
如果有人使用| recyclerViewObject.setHasFixedSize(真);
尝试删除它,它是导致该问题在我的情况。
你可以尝试设置你的ViewGroup拿着recyclerview包内容