-7

如何使高度recyclerViewwrap_content显示滚动条。我想让它的高度与内容一样长,我不希望滚动条在其中,但是当我尝试将内容添加到recyclerView但是,它在那里放置了一个滚动条并修复了回收站视图的高度。请帮我解决这个问题。

4

1 回答 1

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;
    }

}
于 2016-12-05T13:32:04.693 回答