ScrollView中的Infinite RecyclerView?

时间:2016-01-17 09:03:34

标签: android android-viewpager scrollview android-recyclerview

我想要一个包含ViewPagerRecyclerView的布局。但在NestedScrollview内,ViewPager不会与RecyclerView滚动。

1 个答案:

答案 0 :(得分:1)

根据列表中的项目数量,您必须为RecyclerView指定固定高度。使用以下函数计算ListView高度。

 public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

and

setListViewHeightBasedOnChildren(recyclerView);