2016-10-04 139 views
2

Display pic增加recyclerview的高度動態的項目添加

在我的應用程序,我想提高回收視線的高度動態。當我在投訴中添加新項目時,回收站視圖的高度應該增加。有沒有辦法做到這一點?請幫我解決一下這個。提前致謝。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/complaint_text_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/background_line" 
      android:padding="@dimen/layout_margin" 
      android:text="Complaints" 
      android:textColor="@color/textColorFullBlack" /> 

     <LinearLayout 
      android:id="@+id/complaints_container" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginBottom="@dimen/layout_margin" 
      android:layout_weight="1"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/complaint_recycler_view" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="@dimen/layout_margin" 
       android:divider="@color/toolBarBackground" 
       android:groupIndicator="@android:color/transparent" /> 

     </LinearLayout> 

     <TextView 
      android:id="@+id/instructions_text_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/background_line" 
      android:padding="@dimen/layout_margin" 
      android:text="Instructions" 
      android:textColor="@color/textColorFullBlack" /> 

     <FrameLayout 
      android:id="@+id/instructions_container" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginBottom="@dimen/layout_margin" 
      android:layout_weight="1"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/instructions_recycler_view" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="@dimen/layout_margin" 
       android:divider="@color/toolBarBackground" 
       android:groupIndicator="@android:color/transparent" /> 

     </FrameLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="@color/colorPrimary" 
       android:orientation="horizontal"> 

       <in.palmpower.videocon.uicommon.widget.EditText 
        android:id="@+id/search_text_field" 
        style="@style/FormEditText" 
        android:layout_width="0dp" 
        android:layout_gravity="center_vertical" 
        android:layout_margin="@dimen/one_dp" 
        android:layout_weight="1" 
        android:background="@android:color/white" 
        android:inputType="textFilter" 
        android:padding="@dimen/search_edit_text_padding" 
        android:privateImeOptions="nm" /> 

       <Button 
        android:id="@+id/add_button" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_gravity="center_vertical" 
        android:background="?attr/selectableItemBackground" 
        android:contentDescription="@string/edittext_hint_search" 
        android:padding="@dimen/button_padding" 
        android:text="@string/add_button_string" 
        android:textColor="@color/textColorWhite" /> 

      </LinearLayout> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 
+0

@Christopher我已經在xml中指定了高度。沒有什麼更多。 – Harshith

+0

在您的recyclerview上設置了wrap_content,並且您的行項目母版面 – k0sh

回答

-1

如果您希望recyclerView參與屏幕,WRAP_CONTENT將不起作用。 如果您希望其他內容位於recyclerView下方,則可以將recyclerView放入LinearLayout中,並使用以下代碼動態更改高度。

我假設你正在使用的列表(complaint_list),用於存儲complaints.Adjust的complaint_height相應

ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams(); 
int complaint_height=400; 
if (complaints_list.size() > 2) { 
params.height = ((complaint_list.size() - 1) * 100) + complaint_height; 
} else { 
    params.height = complaint_height+100; 
} 
mRecyclerView.setLayoutParams(params); 
+0

感謝您的回答。但是這個代碼 – Harshith

+1

沒有影響你把recyclerView放在LinearLayout裏面用wrap_content嗎? –

0

這會爲你工作

RecyclerView mRecyclerView 
      = (RecyclerView) findViewById(R.id.complaint_recycler_view); 
int originalItemSize = iteList.size(); 

ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams(); 

if (itemList.size() > originalItemSize){ 
    params.height = params.height + 100; 
    originalItemSize = itemList.size(); 
    mRecyclerView.setLayoutParams(params); 
} 
3

就死在同樣的問題,我用的是RecyclerViewwrap_content裏面LinearLayout,我改變LinearLayoutRelativeLayout它設置RecyclerView身高基於兒童。

+0

經過許多小時的搜索和嘗試,這對我來說真的很有用。謝謝 – blueware

+0

很高興這有幫助。 –