Android findViewById() 与 ListView 中的 onFling 问题

发布于 2024-10-07 19:10:16 字数 5471 浏览 5 评论 0原文

我有一个扩展 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;
            }

        }

I have a class that extend ListActivity.
This class show a AbsListView filled with bus IDs and a bus Names on each row...
I try to handle the bus Name of a row when I make a swipe gesture on...('onFling') but when I do that, the gesture is handled correctly but I can just get the Views (with findViewByID()) of the first row ....I need to get the Views of the Flinged row!

Thank you!

This is my code...:

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);

.......>THE ADAPTER FOR THE LIST VIEW

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);
            }
        }

.......>THE class for the gesture detection

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;
            }

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2024-10-14 19:10:16

一种选择是重写/实现 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;
}

One option is to override/implement the onTouchEvent() of your SpeechView class and handle the swipe within the SpeechView instance itself. You can then examine the properties of the SpeechView instance to know which row was swiped.

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;
    }

}

Additionally, you can pass a GestureDetector into your SpeechListAdapter and feed the to the SpeechView class when it is originally created. Then you can pass the MotionEvents into it.

In either case, be sure to intercept the touch event by returning true.

public boolean onTouchEvent(MotionEvent me)
{
    m_gestureDetector.onTouchEvent(me);
    return true;
}
时光倒影 2024-10-14 19:10:16

您只是很幸运,实际上已经定义了 findViewById(1)) 。您不应在手势检测器中调用 findViweById。我很惊讶它没有崩溃。

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

You just happen to be lucky that findViewById(1)) is actually defined. You shouldn't call findViweById in your gesture detector. I'm surprised that doesn't crash.

Instead, you get the view back in your onGestureListener. Use it there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文