2016-03-05 62 views
25

我正在做一个ToDo列表应用程序,并且在测试它时,出于某种原因,每当我尝试向下滚动时,项目之间会形成巨大的空白。无论何时拖放项目,总是会发生这种情况,但我看不到我的ItemTouchHelper适配器和回调类出现任何错误。如果你能帮助我,那将是非常棒的。RecyclerView项目向下滚动时出现大的间隙形式

前: Before

后: After

RecyclerAdapter.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会自行解决问题。删除对话框后,问题重复(即滚动放置项目之间的巨大空间)

+0

这可能与您的代码无关。这是关于你的布局。显示您在此使用的布局。 –

+0

@DougStevenson尽管我很怀疑这是我的布局,但在发生此问题之前,我根本没有触及它们。不过,我仍然会感激您提供的任何见解。 –

+4

也许这是原因http://stackoverflow.com/questions/35728179/recyclerview-items-with-big-empty-space-after-23-2-0 – Luksprog

回答

59

Luksprog的答案Gabriele Mariotti的答案工程。

按照doc

随着发布2.3.0有一个令人兴奋的新功能的布局管理API: 自动测量! 这允许RecyclerView根据其内容的大小自行调整大小。 >这意味着以前不可用的场景,例如使用WRAP_CONTENT>可用于RecyclerView的维度。 您会发现所有内置的LayoutManagers现在都支持自动测量。

由于这一变化,一定要仔细检查布局参数,您的项目意见以前被忽视的布局参数(如MATCH_PARENT在滚动方向)现在将充分尊重

在您的项目布局,你必须改变:

android:layout_height="match_parent" 

android:layout_height="wrap_content" 
+0

非常感谢您的支持 –

23

在回收站视图改变match_parentWRAP_CONTENT

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

而且改变伊特米布局XML

制作父布局高度match_parentWRAP_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" 
     /> 

2

它,因为你正在使用match_parent在您的垂直方向的行项目的根视图的高度列表视图/ recyclerview。当您使用该项目时,该项目完全展开到其父项。 使用wrap_content高度当recyclerview是垂直面向和宽度当它是horizantally导向。

0

我有这个问题,我只是用户

android:layout_height="wrap_content" 

为每个项目的母公司。

+0

这个答案与接受的不同之处如何? –

0

避免采取在项目布局视图与容器是这样的:

<?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。这应该可以解决问题。为了获得更多的透明度,可以尝试提供背景颜色并通过它来查看实际问题。

相关问题