Scrolling with Multiple ListViews for Android

前端 未结 6 987
孤城傲影
孤城傲影 2020-11-29 04:46

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 bot

6条回答
  •  情深已故
    2020-11-29 05:15

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

提交回复
热议问题