2016-12-05 188 views
-7

如何使recyclerView的高度为wrap_content,以便其中没有显示滚动条。我想使它的高度与内容一样长,我不想在其中显示滚动条,但是当我尝试将内容添加到recyclerView它,但是,在那里放置一个滚动条,并修复回收视图的高度。请帮我解决这个问题。如何将recyclerview高度设置为wrap_content?

+2

它使RecyclerView usless ...只是使用的LinearLayout .. 。 – Selvin

+0

为物品创建布局并以图形方式添加它。 – akshay

+0

不理解。请详细说明。 –

回答

1

你需要完成的是动态设置你的RecyclerView的高度,我以前做过这个但是在ListView上。

如果你可以从RecyclerView切换到ListView控件,您可以使用此方法,否则发表评论,我会调整代码为您RecyclerView:

/** 
* Sets ListView height dynamically based on the height of the items. 
* 
* @param listView to be resized 
* @return true if the listView is successfully resized, false otherwise 
*/ 
public static boolean setListViewHeightBasedOnItems(ListView listView) { 

    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter != null) { 

     int numberOfItems = listAdapter.getCount(); 

     // Get total height of all items. 
     int totalItemsHeight = 0; 
     for (int itemPos = 0; itemPos < numberOfItems; itemPos++) { 
      View item = listAdapter.getView(itemPos, null, listView); 
      item.measure(0, 0); 
      totalItemsHeight += item.getMeasuredHeight(); 
     } 

     // Get total height of all item dividers. 
     int totalDividersHeight = listView.getDividerHeight() * 
       (numberOfItems - 1); 

     // Set list height. 
     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalItemsHeight + totalDividersHeight; 
     listView.setLayoutParams(params); 
     listView.requestLayout(); 

     return true; 

    } else { 
     return false; 
    } 

} 
相关问题