3

我創建了一個EditText,一個按鈕和一個RecyclerView(1個TextView的和1個ImageView的兒童組成)來添加TextViews that looks like this所有元素。在頂部的EditText中,用戶可以輸入文本並點擊+按鈕。這將它們的文本添加到用於更新RecyclerView的List(String)。用戶可以點擊右邊的x來從RecyclerView刪除一個條目。 Here is an image of the overall fragment layoutRecyclerView沒有完全展開,顯示

您可以在圖像中看到的問題是,在幾次提交後,RecyclerView停止擴展並保持固定尺寸(注意右下方的小點)。您可以在RecyclerView中滾動查看項目,項目仍然添加到項目中,但它不會擴展爲全尺寸(圖像中的項目有20多個項目)。如果我刪除一個項目,高度增長出於某種原因,但它仍然不會顯示所有元素,並且當我添加一個新項目時它會縮回。

我試過的東西

這是RecyclerView代碼。層級結構中去

<LinearLayout> 
    <ScrollView> 
     <LinearLayout> 
     <RecyclerView /> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

所有的高度和寬度都設置爲匹配父,所有方向垂直:

<android.support.v7.widget.RecyclerView 
      android:id="@+id/ingredientsRecyclerView" 
      android:layout_marginTop="4dp" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clipChildren="false" 
      android:clipToPadding="false" 
      > 

的代碼添加到RecyclerView一個條目(適配器外):

ingredientsButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (ingredientsText.getText().toString().length() > 0) { 
       mIngredients.add(ingredientsText.getText().toString()); 
       ingredientsText.setText(""); 
       mAdapter.notifyDataSetChanged(); 
       mRecyclerView.setBackgroundResource(R.drawable.rounded_edittext); 
      } 
     } 
    }); 

最後,該代碼刪除從RecyclerView的條目(RV轉接器內):

cancelImage.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
         mIngredients.remove(getAdapterPosition()); 
       mAdapter.notifyItemRemoved(getAdapterPosition()); 
       mAdapter.notifyItemRangeChanged(getAdapterPosition(), mIngredients.size()); 
       if(mAdapter.getItemCount() == 0) 
       mRecyclerView.setBackgroundResource(0); 
      } 
     }); 

任何援助將大受讚賞,因爲我無法弄清楚我的生活!

回答

19

RecyclerView

+0

閃電般的反應和完全正確!感謝幫助隊友! – user2805004

+0

哇!你是冠軍! – Sam

0

k0sh使用NestedScrollView代替ScrollViewsetNestedScrollEnabled(false)是絕對正確的。只需添加,使用完整的類名稱,即android.support.v4.widget.NestedScrollView(確保您的build.gradle文件中包含v4支持庫),否則Android將找不到NestedScrollView類。來源:Error inflating class - NestedScrollView - class not found