使用 Android 的多个 ListView 滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/306626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:07:09  来源:igfitidea点击:

Scrolling with Multiple ListViews for Android

androidlistviewscrollviewandroid-linearlayout

提问by Andrew Burgess

I'm completely stumped on this one. I have three different lists that need to be displayed on the screen. It's completely possible that the lists will extend past the bottom edge of the screen, so I would need scrolling.

我完全被这个难住了。我有三个不同的列表需要显示在屏幕上。列表完全有可能超出屏幕的底部边缘,因此我需要滚动。

I've tried using a ScrollViewwith a LinearLayoutchild, and putting my ListViewsin the LinearView, but all of the ListViewslock to a fixed height with scroll bars. Using other kinds of Layouts means no scrolling.

我试过ScrollViewLinearLayout孩子使用 a ,并将我的ListViews放在LinearView,但所有的ListViews锁都带有滚动条的固定高度。使用其他类型的布局意味着没有滚动。

Does anyone have any suggestions, or will I need to programmatically add the list items to some layout and hope for the best?

有没有人有任何建议,或者我是否需要以编程方式将列表项添加到某些布局并希望最好?

采纳答案by pupeno

I haven't done this yet, but I've been thinking about it since I'll probably need to do it. I can think of three options: use only one list with the contents of all lists. You can make a ListAdapter that does that and inserts some kind of header. This could probably be something very re-usable.

我还没有这样做,但我一直在考虑,因为我可能需要这样做。我可以想到三个选项:只使用一个包含所有列表内容的列表。您可以制作一个 ListAdapter 来执行此操作并插入某种标题。这可能是非常可重用的东西。

The other idea is to make a list of lists. But that sounds like asking for trouble.

另一个想法是制作一个列表列表。但这听起来像是自找麻烦。

回答by Richard

Forward touch event from touchedview to other views. All your views will be synchronized expand/collapsed too.

将触摸事件从touched视图转发到其他视图。您的所有视图也将同步展开/折叠。

   OnTouchListener mOnTouch = new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {            
            MotionEvent newEvent = MotionEvent.obtain(event);
            switch(event.getAction()){  
            case MotionEvent.ACTION_MOVE:
                if(mTouched == null){
                    mTouched = v;
                }
                mMovingFlag = true;
                break;
            case MotionEvent.ACTION_UP:
                if(mMovingFlag==false){
                    newEvent.setAction(MotionEvent.ACTION_CANCEL);
                }
                mMovingFlag = false;
                break;
            default:
                mMovingFlag = false;
                if(mTouched != null && mTouched.equals(v)){
                    mTouched = null;
                }
                break;

            }
            if(mTouched == null || mTouched.equals(v)){
                int items = mLayoutWithListViews.getChildCount();
                for(int list=0; list<items; list++){
                    AbsListView listView =mLayoutWithListViews.getChildAt(list));
                    if(listView != v){
                         listView.onTouchEvent(newEvent);
                    }
                }
            }
            return false;
        }
    };

回答by hgllnt

I had a similar problem save that I had two GridViews and one ListView to scroll. I solved it by manually setting the height of the ListViews and GridView:

我有一个类似的问题,除了我有两个 GridViews 和一个 ListView 可以滚动。我通过手动设置 ListViews 和 GridView 的高度来解决它:

    ListAdapter listAdapter = listView.getAdapter();

    int rows = listAdapter.getCount() / columns;
    int height = 60 * rows; // Insert the general cell height plus the dividers.

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = height;
    listView.setLayoutParams(params);
    listView.requestLayout();

Hope I could help someone.

希望我能帮助别人。

回答by Kevin Parker

Although this answer is for scroll view's instead of list view's, the problem is the same and this solution worked well for me. Scrolling the left scrollview will scroll the right scrollview and viceversa.

虽然这个答案是针对滚动视图而不是列表视图,但问题是一样的,这个解决方案对我来说效果很好。滚动左侧滚动视图将滚动右侧滚动视图,反之亦然。

 public class MainActivity extends Activity implements OnTouchListener {
    private ScrollView scrollViewLeft;
    private ScrollView scrollViewRight;
    private static String LOG_TAG = MainActivity.class.getName();
    private boolean requestedFocus = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scrollViewLeft = (ScrollView) findViewById(R.id.scrollview_left);
        scrollViewRight = (ScrollView) findViewById(R.id.scrollview_right);

        scrollViewLeft.setOnTouchListener(this);
        scrollViewRight.setOnTouchListener(this);

        scrollViewLeft.setFocusableInTouchMode(true);
        scrollViewRight.setFocusableInTouchMode(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        Log.d(LOG_TAG, "--> View, event: " + view.getId() + ", " + motionEvent.getAction() + ", " + view.isFocused());
        Log.d(LOG_TAG, "--> " + scrollViewLeft.isFocused() + ", " + scrollViewRight.isFocused());

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN && requestedFocus == false){
            view.requestFocus();
            requestedFocus = true;
        }else if (motionEvent.getAction() == MotionEvent.ACTION_UP){
            requestedFocus = false;
        }

        if (view.getId() == R.id.scrollview_left && view.isFocused()){
            scrollViewRight.dispatchTouchEvent(motionEvent);
        }else if (view.getId() == R.id.scrollview_right && view.isFocused()){
            scrollViewLeft.dispatchTouchEvent(motionEvent);
        }

        return super.onTouchEvent(motionEvent);
    }
}

回答by Kevin Parker

ListAdapter listAdapter = listView.getAdapter();
int rows = listAdapter.getCount() / columns;
 int height = 60 * rows; // Insert the general cell height plus the dividers.
 ViewGroup.LayoutParams params = listView.getLayoutParams();
 params.height = height;
 listView.setLayoutParams(params);
 listView.requestLayout();

回答by Kiran Parmar

Use expandable ListView. Chk my answer for a similar question here: Android ScrollView layout problem

使用可扩展的 ListView。在这里查一下我对类似问题的回答: Android ScrollView layout problem