Dispatch click to ListView when activity implements OnTOuchListener
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?
edit:
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 = (View开发者_JS百科Flipper) 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
}
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
}
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.
Good Luck.
(this was an answer I basically gave for another thread similar to this one Custom ListVIew and onclick)
Maybe your activity doesn't need to implement OnTouchListener? Just let the ListView handle the touch event. If that doesn't suit you, then consider passing the touch event to the super class before you process it. That should correctly dispatch the event or tell you if no one handled it.
精彩评论