2016-03-05 65 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。這應該可以解決問題。爲了獲得更多的透明度,可以嘗試提供背景顏色並通過它來查看實際問題。

相關問題