首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列表视图/回收视图在Scrollview/NestedView中未展开

列表视图/回收视图在Scrollview/NestedView中未展开
EN

Stack Overflow用户
提问于 2015-11-13 18:34:01
回答 2查看 967关注 0票数 1

我在scroll/nestedscroll视图中尝试了列表和回收器视图,列表的高度不能正常扩展。如果我硬编码列表的高度比它工作,否则包装内容不工作。我在互联网上搜索了很多,由于listview在滚动视图中,两个对象都有滚动功能,这就是为什么一个对象不让另一个对象滚动。这就是为什么listview的高度不起作用。我知道这是一种糟糕的做法,但我别无选择,我的布局包含几个对象,比如checkbox和edittext。我还需要滚动其他对象。

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

发布于 2015-11-13 21:41:05

尝试在ScrollView上设置属性fillViewport="true"

票数 2
EN

Stack Overflow用户

发布于 2016-02-06 17:37:25

您可以使用以下命令:

代码语言:javascript
复制
<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>

和:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33690720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档