Listview / Recycler View未在Scrollview / NestedView

时间:2015-11-13 10:34:01

标签: android listview android-recyclerview android-scrollview

我在scroll / nestedscroll视图中尝试了list和recycler视图,列表的高度未正确扩展。如果我硬编码列表的高度比它的工作,否则包装内容不起作用。 我在互联网上搜索了很多,由于滚动视图中的列表视图,并且两个对象都具有滚动功能,这就是为什么一个对象不允许其他滚动的原因。这就是为什么listview的高度不起作用的原因。我知道它的不良做法,但我没有选择我的布局包含几个对象,如checkbox和edittext。我也需要滚动其他对象。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
        xmlns-android="http-//schemas.android.com/apk/res/android"
        xmlns-app="http-//schemas.android.com/apk/res-auto"
        android-isScrollContainer="false"
        android-layout_width="match_parent"
        android-layout_height="match_parent"
        android-layout_gravity="fill_vertical"
        android-clipToPadding="false"
        app-layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android-layout_width="match_parent"
            android-layout_height="match_parent"
            android-orientation="vertical"
            android-paddingTop="1dp">

     <ListView
        android-id="@+id/list"
        android-layout_width="fill_parent"
        android-layout_height="match_parent"
        android-divider="@null" />
         </LinearLayout>
</android.support.v4.widget.NestedScrollView>

2 个答案:

答案 0 :(得分:1)

您可以使用:

<android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false">

        <com.example.NestedListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.NestedScrollView>

public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {

    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;
        setOnScrollListener(this);
        setOnTouchListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null, this);
                    //now it will not throw a NPE if listItem is a ViewGroup instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    }
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();
                }
                newHeight += getDividerHeight() * listPosition;
            }
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
                }
            }
        } else {
            newHeight = getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), newHeight);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
            }
        }
        return false;
    }
}

希望得到帮助

答案 1 :(得分:0)

尝试在ScrollView上设置属性{{1}}。