我是 android 开发的新手,我正在尝试实现一种方法来显示 6 个可以通过手势(左-右-fling、右-左-fling)更改的视图并使用以下 ListView Pull-To-Refresh -列表视图。这 6 个视图具有相同的结构,但内容不同。这就是为什么我认为一个带有 ViewFlipper的 Pull-To-Refresh-Listview应该就足够了,并且在 Fling 上,只有 View 的内容应该被替换?
主要的.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E1E1E1"
android:orientation="vertical"
android:scrollbars="vertical" >
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.markupartist.android.widget.PullToRefreshListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#AB1A1A"
android:textColor="#FFFFFF" >
</com.markupartist.android.widget.PullToRefreshListView>
</ViewFlipper></LinearLayout>
MainActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_objSeparatedListAdapter = new SeparatedListAdapter(this);
m_objPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listView);
m_linkedListDays.add("Te");
m_linkedListMeals.add("st");
ArrayAdapter<String> listadapter = new ArrayAdapter<String>(this, R.layout.list_item, m_linkedListMeals);
for (int i = 0; i < m_linkedListDays.size(); i++) {
m_objSeparatedListAdapter.addSection(m_linkedListDays.get(i), listadapter);
}
m_objPullToRefreshListView.setAdapter(m_objSeparatedListAdapter);
m_objPullToRefreshListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// GET DATA
}
});
m_objPullToRefreshListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// FORWARD TO THE OTHER VIEW
return true;
}
});
m_objAnimationLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
m_objAnimationLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
m_objAnimationRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
m_objAnimationRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
m_objGestureDetector = new GestureDetector(new MyGestureDetector());
m_objPullToRefreshListView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return m_objGestureDetector.onTouchEvent(event);
}
});
}
我的手势检测器
class MyGestureDetector extends SimpleOnGestureListener{
DisplayMetrics dm = getResources().getDisplayMetrics();
private int REL_SWIPE_MIN_DISTANCE = (int)(120.0f * dm.densityDpi / 160.0f + 0.5);
private int REL_SWIPE_MAX_OFF_PATH = (int)(250.0f * dm.densityDpi / 160.0f + 0.5);
private int REL_SWIPE_THRESHOLD_VELOCITY = (int)(200.0f * dm.densityDpi / 160.0f + 0.5);
// Detect a single-click and call my own handler.
@Override
public boolean onSingleTapUp(MotionEvent e) {
ListView lv = (PullToRefreshListView) m_objViewFlipper.getCurrentView();
int pos = lv.pointToPosition((int)e.getX(), (int)e.getY());
myOnItemClick(pos);
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH) return false;
if (e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
onRTLFling();
m_objViewFlipper.setInAnimation(m_objAnimationLeftIn);
m_objViewFlipper.setOutAnimation(m_objAnimationLeftOut);
m_objViewFlipper.showNext();
} else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
onLTRFling();
m_objViewFlipper.setInAnimation(m_objAnimationRightOut);
m_objViewFlipper.setOutAnimation(m_objAnimationRightIn);
m_objViewFlipper.showPrevious();
}
return false;
}
}
那么,问题又是如何使用一个 Pull-To-Refresh-Listview 来管理多个视图 (6),其中还包括投掷手势?
感谢您的时间和帮助!