【发布时间】:2014-08-05 23:42:01
【问题描述】:
我使用视图翻转器在滑动列表项时显示一个按钮。但是每当我滑动列表项时,什么都没有发生。
代码
public class CustomList extends BaseAdapter implements View.OnTouchListener {
private Context context;
public Activity activity;
String[] a;
public CustomList(Context context, String[] a) {
this.context = context;
this.a = a;
}
@Override
public int getCount() {
return a.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
final Viewholder holder;
if (view == null) {
holder = new Viewholder();
view = LayoutInflater.from(context).inflate(R.layout.activity_my, viewGroup, false);
holder.flipper = (ViewFlipper) view.findViewById(R.id.flipper);
view.setTag(holder);
} else {
holder = (Viewholder) view.getTag();
}
//
return view;
}
GestureDetector.SimpleOnGestureListener simpleOnGestureListener
= new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Viewholder holder = new Viewholder();
float sensitvity = 50;
if ((e1.getX() - e2.getX()) > sensitvity) {
holder.flipper.showNext();
} else if ((e2.getX() - e1.getX()) > sensitvity) {
holder.flipper.showPrevious();
}
return true;
}
};
GestureDetector gestureDetector
= new GestureDetector(simpleOnGestureListener);
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private class Viewholder {
ViewFlipper flipper;
float startX, endX;
}
}
Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/flipper">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</ViewFlipper>
</LinearLayout>
为什么滑动不起作用? 我也希望只有一半的列表项应该被刷掉而不是完整的
【问题讨论】:
标签: android viewflipper swipe-gesture