Android - ListView inside Gallery makes the scrolling not Smooth

后端 未结 4 683
無奈伤痛
無奈伤痛 2020-12-30 14:35

I implemented a gallery, and inside it I have many listviews from left to right. For some reason Gallery works great with all views but not with listview. With listview, whe

4条回答
  •  孤城傲影
    2020-12-30 15:15

    I had have the same problem. And the solution is less than simple.

      • Set the listview to be not focusable in touch mode.

      listView.setFocusableInTouchMode(false);

      • If you need focus, call it from onItemSelected listener.
    1. Use OnInterceptTouchEvent to get Touch Events from your child. And make some modification in onScroll and onTouchEvent of your Gallery

      OnItemSelectedListener mOnItemSelected = new OnItemSelectedListener()
      {   
          @Override
          public void onItemSelected(AdapterView parent, View view,
                  int position, long id)
          {
            view.requestFocusFromTouch();
          }
          @Override
          public void onNothingSelected(AdapterView parent)
          {               
          }
      };
      
      @Override
      public boolean onInterceptTouchEvent(MotionEvent ev) {
          onTouchEvent(ev);
          return scrollingHorizontally;
      }
      
      @Override
      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
          if(Math.abs(distanceX)>Math.abs(distanceY) || scrollingHorizontally == true){
              scrollingHorizontally = true;
              super.onScroll(e1, e2, distanceX, distanceY);
          }
          return scrollingHorizontally;
      }
      
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          switch(event.getAction()) {
          case MotionEvent.ACTION_UP:
          case MotionEvent.ACTION_CANCEL:
              scrollingHorizontally = false;
              break;
          default:
                  break;
          }
          super.onTouchEvent(event);
          return scrollingHorizontally;
      }
      

提交回复
热议问题