调度点击ListView的活动时实现OnTOuchListener [英] Dispatch click to ListView when activity implements OnTOuchListener
问题描述
我的活动实现OnTouchListener,它里面有只有在一个ListView控件。
当在ListView的用户联系,我需要派遣ClickEvent到具有OnItemClickListener处理程序的ListView。
My activity implements OnTouchListener and it have one ListView inside it. When user touch over ListView, I need to dispatch ClickEvent to ListView that has OnItemClickListener handler.
我怎样才能做到这一点?
How can I do this?
编辑:
的ListView每个列表项有onTouchEvent的处理程序。
ListView控件有onItemClick处理程序。
Each list item of listView have onTouchEvent handler. ListView have onItemClick handler.
@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或显示previous和一个单一的点击已经被onItemClick处理
My list item is a ViewFlipper, when user touch and drag item, ViewFlipper need perform showNext or ShowPrevious and an single click have to handled by onItemClick
推荐答案
如果您希望能够单击一个项目在列表中,有一些事情发生,你需要做类似如下:
If you want to be able to click on an item in the list and have something happen you need to do something like the following:
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文件:
You will also need an adapter class (in my example my adapter class is called YourAdapterClass
) You can either make this a private class in your ListActivty or a completely new class in its own Java file like the following:
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;
}
}
用于自定义列表视图
通常这种方式(使用定制的行列表,但你可以使用它的一个普通视图以及)
Usually this way is used for custom ListViews (lists with custom made rows, but you can use it for a normal view as well)
希望这有助于你至少在正确的方向开始。
Hope this helps you at least get started in the right direction.
好运。
(这是一个答案,我基本放弃了与此类似自定义的ListView另一个线程和的onclick )
(this was an answer I basically gave for another thread similar to this one Custom ListVIew and onclick)
这篇关于调度点击ListView的活动时实现OnTOuchListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!