ScrollView 中的 ListView

10

停止争论,这不会解决问题,有人已经找到了解决方案,这是Moisés Olmedo的答案:https://dev59.com/Yuo6XIcBkEYKwwoYSCpJ?rq=1 - Bhimbim
4个回答

6

为了防止整个屏幕过长,我需要在scrollview中使用listview。这样可以设置列表显示的项目数,其余部分将滚动。

我在我的应用程序中使用以下类,迄今为止它似乎运行得非常好。

 public class NestedListView extends ListView implements OnScrollListener,
        OnTouchListener {
    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 4;

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

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

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

    @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);
                    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) {
        listViewTouchAction = event.getAction();
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
            }
        }
        return false;
    }
}

我对这个解决方案的问题在于它消耗了大量的CPU功率。在onMeasure方法中,您要求listadapter重新创建您请求的每个视图,这将使整个应用程序非常缓慢。我认为您应该缓存您所请求的视图,并仅在它们无效时重新创建它们。 - Willem Meints
虽然我在我的 Atrix 上没有注意到这个解决方案的任何问题,但我确信在旧设备上可能会出现问题。 - Jeff

6

ListView具有内置的滚动功能。可以将其封装在其他布局中,例如LinearLayoutRelativeLayout


2
使用以下方法,尽情享受吧!
    private void setListViewScrollable(final ListView list) {
    list.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            listViewTouchAction = event.getAction();
            if (listViewTouchAction == MotionEvent.ACTION_MOVE)
            {
                list.scrollBy(0, 1);
            }
            return false;
        }
    });
    list.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view,
                int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE)
            {
                list.scrollBy(0, -1);
            }
        }
    });
}
listViewTouchAction是一个全局整数值。如果您想要替换这一行,请将其替换为:
list.scrollBy(0, 1);

如果您有其他的建议,请与我们分享。

1

不应该在ScrollView中嵌套ListView。

如果您想要与ListView一起滚动其他视图,可以查看this answer


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接