0
我試圖重現Gmail的行爲電子郵件主列表,用戶可以:觸摸和滑動重疊觀點
- 上的點擊列表視圖的一行中顯示電子郵件
- 輕掃listview的行對這封電子郵件執行一些操作。
對於這一點,我創建含有RelativeLayout的其中一個是FlingableRowView(可以swipped前視圖/ flinged)的圖(SmartRow)和一個簡單的視圖(含有的按鈕背面視圖來執行動作)。
<?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);
}
}
你有任何想法如何做到這一點?