如下面这个图:
需要实现的功能是:用手指在某条记录上从左往右划一下,出现一条横线,用于表示这个菜已经上过了;倒过来划,取消这条横线,表示这个菜没上过。
如何实现呢?
看我的吧!
首先需要一个Item的布局,在布局的最上方有一个ImageView用于显示这条横线,如果没有就把这个ImageView背景设置成透明,这个ImageView同这条记录等宽等高!
布局如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height" >
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:singleLine="true"
android:text="名称"
android:textColor="@color/white"
android:textSize="@dimen/font_xxxbig" />
<TextView
android:id="@+id/tv_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:singleLine="true"
android:text="数量"
android:textColor="@color/white"
android:textSize="@dimen/font_xxxbig"
android:layout_weight="1"
/>
<TextView
android:id="@+id/tv_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:singleLine="true"
android:text="价格"
android:textColor="@color/white"
android:textSize="@dimen/font_xxxbig" />
</LinearLayout>
<ImageView
android:id="@+id/iv_line"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
</FrameLayout>
然后就是写ListView了:
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(onItemClickListener);
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
mDetector.onTouchEvent(event);
return false;
}
});
实现手势:
mDetector=new GestureDetector(new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@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) {
// TODO Auto-generated method stub
final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;
if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { //从左往右
if(view!=null){
Combos instance=(Combos)view.getTag();
if(instance!=null){
if(instance.getStatus()==ST_NORMAL){
instance.setStatus(ST_FINISH);
adapter.notifyDataSetChanged();
//dosomething()
}
}
}
}
else if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY){//从右往左
if(view!=null){
Combos instance=(Combos)view.getTag();
if(instance!=null){
if(instance.getStatus()==ST_FINISH){
instance.setStatus(ST_NORMAL);
adapter.notifyDataSetChanged();
//dosomething()
}
}
}
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return true;
}
});
};
再下来就要实现Adapter,提供一段伪代码:
class DetailAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return selectedList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return selectedList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Combos instance = selectedList.get(position);
convertView = LayoutInflater.from(context).inflate(R.layout.item_for_consume_detail_activity, null);
ImageView iv_line = (ImageView) convertView.findViewById(R.id.iv_line);
//根据状态判断该显示“线”还是“透明”
if(instance.getStatus()==ST_FINISH){
iv_line.setBackgroundResource(R.drawable.line_huacai);
}else{
iv_line.setBackgroundColor(Color.TRANSPARENT);
}
iv_line.setTag(instance);
iv_line.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
view=v;
return false;
}
});
return convertView;
}
}
这里对iv_line也要实现OnTouchListener,并且view是个全局变量,需要在手势当中判断一下if(view!=null),确保程序没问题,并且获得对象view.getTag()。最后一定要返回false,确保ListView的OnTouchListener能够继续下去!
整个触摸流程是:
先ImageView中的onTouch起作用-->在ListView中的onTouch起作用-->手势