在android列表项中进行Ontouch和onclick

时间:2014-10-08 10:46:15

标签: android android-listview swipe baseadapter ontouchlistener

我想在android listview项目中实现ontouch和onclicklistener。

如果我选择了行并单击了一行,则需要调用onclicklistener。

同时如果我左右滑动列表行,需要在适配器文件中调用ontouchlistener。

我该怎么办?请给我一个解决方案吗?

这里刷卡工作正常。但是onclick没有从我的代码中调用。

在活动档案。,

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);
            }
        }
    });
}

从适配器文件

 convertView.setOnTouchListener(mOnTouchListener);
 convertView.setOnClickListener(new  View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TodayList.this,
                            DetailPage.class);
                startActivity(intent);
               }});

2 个答案:

答案 0 :(得分:0)

要点击onView的一行ListView,你应该使用onItemClickListener,通过list.setOnItemClickListener()设置它。如果你想从适配器提供这个,那么最好使用GestureDetector.SimpleOnGestureListener,你有滚动,双击,单击等事件。 此外,如果您想从现有代码执行onClick,可以尝试:

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(); 

答案 1 :(得分:0)

简单的解决方案:

在您的活动文件false上返回"ACTION_DOWN",如下所示:

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;
    }
}

说明:

按下视图时始终会调用

onTouch,因为这是将事件分派给视图的初始状态。当你渴望 按下你的视图,这仍然是首先调用onTouch,因为你 之前已在true中返回onTouch(这意味着您已被消费 这个事件,不应该进一步发送)你不会被onLongPress召唤。诀窍是在false中返回onTouch

this回答