在ListView问题中使用onFling的Android findViewById()

时间:2010-12-15 21:15:49

标签: android listview gesture-recognition

我有一个扩展ListActivity的类。 这个类显示一个AbsListView,其中包含每个行上的总线ID和总线名称... 当我在...上执行滑动手势时,我尝试处理行的总线名称('onFling')但是当我这样做时,手势处理正确但我可以获得视图(使用findViewByID())第一行....我需要获得Flinged行的视图!

谢谢!

这是我的代码......:

public class dayBusList extends ListActivity{

     AbsListView lv;
 AABDatabaseManager db;

     GestureDetector mGestureDetector = null;
 View.OnTouchListener mGestureListener = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        db = new AABDatabaseManager(dayBusList.this);
        setListAdapter(new SpeechListAdapter(this));



        lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setBackgroundColor(0xffffffff);
        lv.setFastScrollEnabled(true);
        lv.setSoundEffectsEnabled(true);

        //Gesture detector for swipe...

         mGestureDetector = new GestureDetector(new MyGestureDetector());

             mGestureListener = new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent aEvent) {

                    if (mGestureDetector.onTouchEvent(aEvent))
                        return true;
                    else
                        return false;
                    }
                };
       lv.setOnTouchListener(mGestureListener);

.......>列表视图的适配器

private class SpeechListAdapter extends BaseAdapter {

         private Context mContext;
         private String[] busIDS = db.getDayBusIds();
         private String[] busNAMES = db.getDayBusNames();

            public SpeechListAdapter(Context context) {

                mContext = context;
            }

            public int getCount() {
                return busNAMES.length;
            }

            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                SpeechView sv;

                if (convertView == null) {
                    sv = new SpeechView(mContext, busIDS[position],
                            busNAMES[position]);
                } else {
                    sv = (SpeechView) convertView;
                    sv.setTitle(busIDS[position]);
                    sv.setDialogue(busNAMES[position]);
                }
                return sv;
            }

         }

        private class SpeechView extends LinearLayout {

            private TextView busID;
            private TextView busNAME;

            public SpeechView(Context context, String title, String words) {
                super(context);

                this.setOrientation(HORIZONTAL);

                ImageView busIcon = new ImageView(context);
                busIcon.setImageResource(R.drawable.stm);
                busIcon.setPadding(2, 20, 5, 20);
                addView(busIcon,new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                busID = new TextView(context);
                busID.setId(1);
                busID.setTextSize(30);
                busID.setTextColor(Color.BLUE);
                busID.setPadding(5, 15, 5, 20);
                //busID.setPadding(0, 0,10, 0);
                busID.setText(title);
                addView(busID, new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                busNAME = new TextView(context);
                busNAME.setId(2);
                busNAME.setGravity(Gravity.TOP);
                busNAME.setTextSize(15);
                busNAME.setPadding(0, 0, 0, 0);
                busNAME.setText(words);
                busNAME.setTextColor(Color.GREEN);
                busNAME.setWidth(160);
                addView(busNAME, new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             }

            public void setTitle(String id) {
                busID.setText(id);
            }

            public void setDialogue(String name) {
                busNAME.setText(name);
            }
        }

.......>手势检测的课程

class MyGestureDetector extends SimpleOnGestureListener {
    private static final int SWIPE_MIN_DISTANCE = 200;
    private static final int SWIPE_MAX_OFF_PATH = 100;
    private static final int SWIPE_THRESHOLD_VELOCITY = 100;

    @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                float dX = e2.getX()-e1.getX();
                float dY = e1.getY()-e2.getY();
                if (Math.abs(dY)<SWIPE_MAX_OFF_PATH &&
                    Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY &&
                    Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {
                    if (dX>0) {
                        Toast.makeText(getApplicationContext(), ((TextView) findViewById(1)).getText()+"Right Swipe", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), ((TextView) findViewById(1)).getText()+"Left Swipe", Toast.LENGTH_SHORT).show();
                    }
                    return true;
                } 
                return false;
            }

        }

2 个答案:

答案 0 :(得分:1)

一个选项是覆盖/实现SpeechView类的onTouchEvent()并处理SpeechView实例本身内的滑动。然后,您可以检查SpeechView实例的属性以了解哪个行被刷过。

public class SpeechView extends LinearLayout
{
    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        // 1) process any specific SpeecView instance variables.
        // 2) pass event to a gestureDetector.
        return true;
    }

}

此外,您可以将GestureDetector传递到SpeechListAdapter,并在最初创建时将其提供给SpeechView类。然后你可以将MotionEvents传递给它。

在任何一种情况下,请务必通过返回 true 拦截触摸事件。

public boolean onTouchEvent(MotionEvent me)
{
    m_gestureDetector.onTouchEvent(me);
    return true;
}

答案 1 :(得分:0)

你恰好幸运地findViewById(1))实际定义了。您不应该在手势检测器中调用findViweById。我很惊讶不会崩溃。

相反,您可以在onGestureListener中获取视图。在那里使用它。