0

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);
               }});
Krishna Veni
  • 2,217
  • 8
  • 27
  • 53

3 Answers3

0

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(); 
Eddy
  • 489
  • 4
  • 10
  • Already i have tried with your's logic.it means i have swiping the row., the onclick functionality only working at that time ontouch functionality was not working.ok i will go another way GestureDetector.SimpleOnGestureListener – Krishna Veni Oct 08 '14 at 11:16
  • I'm a little bit busy now,and can not test the code and say for 100% will it work or not,but at least a gave you a start point. hope it will help. If i will find some time i will test and post the results. – Eddy Oct 08 '14 at 11:26
0

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

Community
  • 1
  • 1
ChinoCarlo
  • 28
  • 4
0

Just use another variable (boolean isClick):

        rowView.setOnTouchListener(new View.OnTouchListener() {
        private float startX;
        private boolean isSwipeActive;
        private boolean isClick;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = event.getX();
                    isClick = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    isSwipeActive = true;
                    float deltaX = event.getX() - startX;
                    if (isSwipeActive && deltaX < -1 ) {
                        isClick = false;
                        holder.buttonsLayout.setVisibility(View.VISIBLE);
                        holder.mainContentLayout.animate().x(-150).y(0).setDuration(500);
                        item_list.get(position).isSwiped = true;
                    }else if (isSwipeActive && deltaX > 1 ) {
                        isClick = false;
                        holder.mainContentLayout.animate().x(0).y(0).setDuration(500);
                        Handler handler = new Handler();
                        Runnable runnable = () -> holder.buttonsLayout.setVisibility(View.GONE);
                        handler.postDelayed(runnable, 500);
                        item_list.get(position).isSwiped = false;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    isSwipeActive = false;
                    if(isClick) rowView.performClick();
                    isClick = false;
                    break;
            }
            return true;
        }
    });