问题
I want to implement the ontouch and onclicklistener in android listview item.
If i have selecting the row and click a row., need to call onclicklistener.
at the same time If I have swiping the list row on left and right ., need to call ontouchlistener in adapter file.
how can i do ? please give me a solution for this ?
here swipe working fine.but onclick is not calling from my code.
In activity file.,
class MyUpcomingTouchListener implements OnTouchListener
{
@Override
public boolean onTouch(View v, MotionEvent event) {
ViewHolder1 holder = (ViewHolder1) v.getTag(R.layout.listof_upcoming);
int action = event.getAction();
int position = (Integer) v.getTag();
switch (action) {
case MotionEvent.ACTION_DOWN:
action_down_x = (int) event.getX();
Log.d("action", "ACTION_DOWN - ");
break;
case MotionEvent.ACTION_MOVE:
Log.d("action", "ACTION_MOVE - ");
action_up_x = (int) event.getX();
difference = action_down_x - action_up_x;
break;
case MotionEvent.ACTION_UP:
Log.d("action", "ACTION_UP - ");
calcuateupcomingDifference(holder, position);
action_down_x = 0;
action_up_x = 0;
difference = 0;
break;
}
return true;
}
}
private void calcuateupcomingDifference(final ViewHolder1 holder, final int position) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (difference == 0) {
}
if (difference > 90) {
holder.carudetails.setVisibility(View.GONE);
holder.carucheckin.setVisibility(View.VISIBLE);
holder.caruimage.setVisibility(View.VISIBLE);
upcomingitems.get(position).setVisible(true);
upcominglistAdapter.changeData(upcomingitems);
}
if (difference < -90) {
holder.carudetails.setVisibility(View.VISIBLE);
holder.carucheckin.setVisibility(View.GONE);
holder.caruimage.setVisibility(View.GONE);
upcomingitems.get(position).setVisible(true);
upcominglistAdapter.changeData(upcomingitems);
}
}
});
}
From the adapter file,
convertView.setOnTouchListener(mOnTouchListener);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TodayList.this,
DetailPage.class);
startActivity(intent);
}});
回答1:
To perfrom onClick on a row of ListView you should use onItemClickListener,set it via list.setOnItemClickListener(). If you want to provide this from the adapter,then it will be better to use GestureDetector.SimpleOnGestureListener,where you have events like scroll,double tap,single tap up. Also if you want to perform onClick from the code you have now, you can try:
private void calcuateupcomingDifference(final ViewHolder1 holder, final int position) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int absValue=Math.abs(difference);
if (absValue >= 0 && absValue<=10) {
holder.rowLayout.performOnClick();
}
if (difference > 90) {
holder.carudetails.setVisibility(View.GONE);
holder.carucheckin.setVisibility(View.VISIBLE);
holder.caruimage.setVisibility(View.VISIBLE);
upcomingitems.get(position).setVisible(true);
upcominglistAdapter.changeData(upcomingitems);
}
if (difference < -90) {
holder.carudetails.setVisibility(View.VISIBLE);
holder.carucheckin.setVisibility(View.GONE);
holder.caruimage.setVisibility(View.GONE);
upcomingitems.get(position).setVisible(true);
upcominglistAdapter.changeData(upcomingitems);
}
}
});
// DO not forget to add the rowLayout of converView to ViewHolder1 and to set rowLayout.setOnClickListener();
回答2:
Simple solution:
Return false on your "ACTION_DOWN" on your activity file like below:
class MyUpcomingTouchListener implements OnTouchListener
{
@Override
public boolean onTouch(View v, MotionEvent event) {
...
switch (action) {
case MotionEvent.ACTION_DOWN:
...
return false;
case MotionEvent.ACTION_MOVE:
...
break;
case MotionEvent.ACTION_UP:
...
break;
}
return true;
}
}
Explanation:
onTouch is always called upon pressing the view since this is the initial state of dispatching the events to the view. When you long
press your view this still calls onTouch first and since you
previously returned true in onTouch (which means that you've consumed
this event and it should not be further dispatched) you won't get onLongPress called. What will do the trick is returning false in onTouch.
Credits to this answer
来源:https://stackoverflow.com/questions/26255031/ontouch-and-onclick-in-android-list-item