当activity实现OnTOuchListener时,Dispatch单击ListView

时间:2011-07-06 18:48:59

标签: android android-layout android-listview

我的活动实现了OnTouchListener,里面有一个ListView。 当用户触摸ListView时,我需要将ClickEvent分派给具有OnItemClickListener处理程序的ListView。

我该怎么做?

修改

listView的每个列表项都有onTouchEvent处理程序。 ListView有onItemClick处理程序。

  @Override
    public boolean onTouch(View view, MotionEvent event) {
        float actionUpX;

        switch(event.getAction()){

        case MotionEvent.ACTION_DOWN:
            actionDownX = event.getX();
            break;

        case MotionEvent.ACTION_UP:
            actionUpX = event.getX();

            mFlipper = (ViewFlipper) view.findViewById(R.id.view_listitem_flipper);


            if(actionDownX < actionUpX){
                // |--->
                mFlipper.showNext();                
            } else if(actionDownX > actionUpX){
                // <---|
                mFlipper.showPrevious();
            } else {
                    //Click
                    //Need to dispatch itemClickEvent to ListView

                    //view.performClick();  this line causes StackOverflowException 
                    }
            break;
        }
        return true;
    }

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    //Need do an action using position of view

}

我的列表项是ViewFlipper,当用户触摸并拖动项时,ViewFlipper需要执行 showNext或ShowPrevious只需单击一下onItemClick

即可

2 个答案:

答案 0 :(得分:2)

如果您希望能够点击列表中的某个项目并发生某些事情,您需要执行以下操作:

public class YourClass extends ListActivity {
    //Your Variables
    ArrayList<Type> yourlist;
    YourAdapterClass adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        yourlist = new ArrayList<Type>();
        this.adapter = new YourAdapterClass(this, R.layout.row, yourlist);
        setListAdapter(this.adapter);

        //you might be able to see if the below works instead of overriding 
        //the OnListItemClickListener farther below
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Log.i("testy", "I Clicked on Row " + position + " and it worked!");
             }
         });
    }

    @Override
    /**
     * When the user selects an item in the list, do an action
     * @param ListView l
     * @param View v
     * @param int position
     * @param long id
     */
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        final int index = position;

        //You can add whatever you want to happen when you click here

        Log.i("testy", "I Clicked on Row " + index + " and this overriding worked!");

    }

//other methods can go here for you list


}

您还需要一个适配器类(在我的示例中,我的适配器类称为YourAdapterClass)您可以将它作为ListActivty中的私有类,或者在其自己的Java文件中创建一个全新的类,如下所示:

public class YourAdapterClass extends ArrayAdapter {
        protected ArrayList items;

        public YourAdapterClass(Context context, int textViewResourceId, ArrayList items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if(v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }

          //do stuff to your list view row if it is a custom row with multiple 
          //components like text views, image views etc.

            return v;


       }
}

通常这种方式用于自定义ListViews(带有自定义行的列表,但您也可以将它用于普通视图)

希望这有助于您至少开始朝着正确的方向前进。

祝你好运。

(这是我基本上给出的另一个类似于这个Custom ListVIew and onclick的线程的答案)

答案 1 :(得分:0)

也许您的活动不需要实现OnTouchListener?让ListView处理触摸事件。如果这不适合您,请考虑在处理之前将触摸事件传递给超类。这应该正确地发送事件或告诉你是否有人处理它。