我正在尝试重现Gmail在电子邮件主列表中的行为,用户可以:
为此,我创建了一个View(SmartRow),其中包含一个RelativeLayout,其中包含一个FlingableRowView(可以翻转/甩尾的前视图)和一个简单的View(后视图包含执行操作的按钮)。
<?xml version="1.0" encoding="utf-8"?>
<view xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.riot.SmartRow" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff005678"
android:orientation="horizontal"
android:visibility="visible" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO"
android:textSize="40dp" >
</TextView>
<Button
android:id="@+id/buttonCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout> />
<view
android:id="@+id/front"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.riot.FlingableRowView"
android:visibility="visible" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff808080"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="22dp"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp" >
</TextView>
</LinearLayout>
</view>
</RelativeLayout>
</view>
使FlingableRowView滑动/投掷的所有东西都能正常工作。我的问题是SmartView不拦截触摸和长触摸,因此listview的OnItemClickListener和MultiChoiceModeListener不起作用。这是我与onTouchEvent和dispatchTouchEvent战斗的日子,但我无法找到如何处理它。 以下是SmartView的一些代码:
public class SmartView extends RelativeLayout {
View mBackView;
FlingableRowView mFrontView;
public SmartView(Context context) {
this(context, null, 0);
}
public SmartView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SmartView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mBackView = findViewById(R.id.back);
mFrontView = (FlingableRowView) findViewById(R.id.front);
mBackView.setVisibility(View.GONE);
mFrontView.setVisibility(View.VISIBLE);
// this listener reports how much the front view is swipped (between 0.0f and 1.0f)
mFrontView.setOnFlingedListener(new OnFlingedListener() {
@Override
public void onFlinged(float val) {
if (val == 0.0f) {
mBackView.setVisibility(View.GONE);
mFrontView.setVisibility(View.VISIBLE);
} else if (val == 1.0f) {
mBackView.setVisibility(View.VISIBLE);
mFrontView.setVisibility(View.GONE);
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mFrontView.getVisibility() == View.VISIBLE) {
return mFrontView.dispatchTouchEvent(ev);
} else if (mBackView.getVisibility() == View.VISIBLE) {
return mBackView.dispatchTouchEvent(ev);
}
return super.dispatchTouchEvent(ev);
}
}
你知道怎么做吗?