2016-03-07 35 views
0

我正在處理一個Android項目,該項目包含一個首先顯示三個項目的列表視圖,然後當滾動到左側時,它將重新加載新的三個項目。請告訴我一些這樣做的概念。如何在listview Android上啓用水平滾動?

+0

的可能的複製[Android中的ListView水平?](http://stackoverflow.com/questions/3240331/horizo​​ntal-listview-in-android) – Dhina

回答

2

如果你需要一個水平滾動的列表視圖,那麼你應該使用一個RecyclerView和一個配置爲水平滾動的LinearLayoutManager。 ListView只能垂直滾動。

+1

RecyclerView不支持低版本。我必須使用listview :( –

+1

RecyclerView在支持庫中,一直到V7(Eclair)。你需要去多少?支持低於16的任何東西都沒有意義,但如果你推它,11是你應該得到的低。 – Francesc

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" 
     />