代码之家  ›  专栏  ›  技术社区  ›  Andrew Burgess

使用Android的多个列表视图滚动

  •  32
  • Andrew Burgess  · 技术社区  · 17 年前

    这件事我受够了。我有三个不同的列表需要显示在屏幕上。列表完全有可能扩展到屏幕的下边缘,所以我需要滚动。

    我试过用 ScrollView 用一个 LinearLayout 孩子,把我的 ListViews LinearView 但是所有的 列表视图 使用滚动条锁定到固定高度。使用其他类型的布局意味着没有滚动。

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

    6 回复  |  直到 10 年前
        1
  •  6
  •   Pablo Fernandez    17 年前

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

    另一个想法是列出一份清单。但这听起来像是自找麻烦。

        2
  •  11
  •   MDMalik    13 年前

    向前触摸事件 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;
            }
        };
    
        3
  •  6
  •   hgllnt    15 年前

    除了两个网格视图和一个要滚动的列表视图外,我还有一个类似的问题。 我通过手动设置ListView和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();
    

    希望我能帮助别人。

        4
  •  3
  •   Kevin Parker    14 年前

    虽然这个答案是针对滚动视图而不是列表视图的,但问题是相同的,这个解决方案对我来说工作得很好。滚动左滚动视图将滚动右滚动视图和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);
        }
    }
    
        5
  •  1
  •   user5345196    10 年前
    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();
    
        6
  •  -1
  •   Community Mohan Dere    9 年前

    使用可展开的列表视图。 我的答案是: Android ScrollView layout problem