如何在listview Android上启用水平滚动?

时间:2016-03-07 01:17:41

标签: android listview

我正在开发一个Android项目,其中包含一个列表视图,该视图首先显示三个项目,然后在向左滚动时重新加载新的三个项目。请告诉我一些概念。

2 个答案:

答案 0 :(得分:2)

如果您需要一个水平滚动的列表视图,那么您应该使用一个RecyclerView,并将LinearLayoutManager配置为水平滚动。 ListView只能垂直滚动。

答案 1 :(得分:1)

    class GestureList extends ListView {
   int flag=BaseActivity.flag;
   Context context;
   GestureDetector gestureDetector;

   public GestureList(Context context, AttributeSet attrs) {
     super(context, attrs);
     this.context=context;
     gestureDetector=new GestureDetector(context,new Gesture(context));     
   }
   public GestureList(Context context, AttributeSet attrs, int defStyle) {
     super(context, attrs, defStyle);
     // TODO Auto-generated constructor stub
     this.context=context;
     gestureDetector=new GestureDetector(context,new Gesture(context));
   }
   public GestureList(Context context) {
     super(context);
     // TODO Auto-generated constructor stub
     this.context=context;
     gestureDetector=new GestureDetector(context,new Gesture(context));
   }
   @Override
   public boolean onTouchEvent(MotionEvent ev) {

     if(gestureDetector.onTouchEvent(ev)) return true;
     return super.onTouchEvent(ev);
   }  
}

___________________________________

    public class Gesture implements OnGestureListener{

  int flag=BaseActivity.flag;

  int length=BaseActivity.myClass.length;
  Class[] myClass=BaseActivity.myClass;
  Context context;
  public Gesture(Context context) {
     // TODO Auto-generated constructor stub
     this.context=context;
  }
  @Override
  public boolean onDown(MotionEvent e) {
     // TODO Auto-generated method stub
     return false;
  }

  @Override
  public void onShowPress(MotionEvent e) {
     // TODO Auto-generated method stub

  }
  @Override
  public boolean onSingleTapUp(MotionEvent e) {
     // TODO Auto-generated method stub
     return false;
  }
  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
       float distanceY) {
     // TODO Auto-generated method stub
     return false;
  }
  @Override
  public void onLongPress(MotionEvent e) {
     // TODO Auto-generated method stub

  }
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
       float velocityY) {           
      //left
        if (e1.getX() - e2.getX() > 50) { 
          Log.i("Fling", "Gesture:left "); 
          if (++flag>=length) {
         flag=length-1;
       BaseActivity.flag=flag;
         return true;
       }       
          BaseActivity.flag=flag;
     Intent intent=new Intent(context, myClass[flag]);

     intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|Intent. FLAG_ACTIVITY_REORDER_TO_FRONT);

     context.startActivity(intent);     
     return true; 
        }
        else if (e2.getX() - e1.getX()>50) {
          Log.i("Fling", "Gesture:right "); 
      if (--flag<0) {
         flag=0;

       BaseActivity.flag=flag;
         return true;
       }       
      BaseActivity.flag=flag;
     Intent intent=new Intent(context,myClass[flag]);

     intent.setFlags(Intent. FLAG_ACTIVITY_NO_HISTORY|Intent. FLAG_ACTIVITY_REORDER_TO_FRONT);  
     context.startActivity(intent); 
//       System.exit(0);         
     return true; 
        } 
        return true; 
  }
}

_______________________________________


    <com.jph.custom.GestureList android:id="@+id/list"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:focusable="false"
        />