Ontouch和的onclick android中列表项 [英] Ontouch and onclick in android list item
问题描述
我要实现android中ListView项的ontouch和onclicklistener。
如果我已经选择行并单击某一行,需要调用onclicklistener。
在同一时间。如果我已经刷卡的左,右,列表行,需要适配器文件来调用ontouchlistener。
我该怎么办?请给我一个解决方案?
在这里刷卡fine.but的onclick工作不是从我的code调用。
在活动文件。
类MyUpcomingTouchListener实现OnTouchListener
{
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){ ViewHolder1架=(ViewHolder1)v.getTag(R.layout.listof_upcoming);
INT行动= event.getAction();
INT位置=(整数)v.getTag(); 开关(动作){
案例MotionEvent.ACTION_DOWN:
action_down_x =(int)的event.getX();
Log.d(行动,ACTION_DOWN - );
打破;
案例MotionEvent.ACTION_MOVE:
Log.d(行动,ACTION_MOVE - );
action_up_x =(int)的event.getX();
差值= action_down_x - action_up_x;
打破;
案例MotionEvent.ACTION_UP:
Log.d(行动,ACTION_UP - );
calcuateupcomingDifference(保持,位);
action_down_x = 0;
action_up_x = 0;
差= 0;
打破;
}
返回true; }}
私人无效calcuateupcomingDifference(最终ViewHolder1持有人,最终诠释位置){
runOnUiThread(新的Runnable(){ @覆盖
公共无效的run(){
如果(差异== 0){
}
如果(差大于90){
holder.carudetails.setVisibility(View.GONE);
holder.carucheckin.setVisibility(View.VISIBLE);
holder.caruimage.setVisibility(View.VISIBLE);
upcomingitems.get(位置).setVisible(真);
upcominglistAdapter.changeData(upcomingitems);
}
如果(差值< -90){
holder.carudetails.setVisibility(View.VISIBLE);
holder.carucheckin.setVisibility(View.GONE);
holder.caruimage.setVisibility(View.GONE);
upcomingitems.get(位置).setVisible(真);
upcominglistAdapter.changeData(upcomingitems);
}
}
});
}
从适配器文件,
convertView.setOnTouchListener(mOnTouchListener);
convertView.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意向意图=新意图(TodayList.this,
DetailPage.class);
startActivity(意向);
}});
简单的解决方案:
返回假
在ACTION_DOWN
像下面的活动文件:
类MyUpcomingTouchListener实现OnTouchListener
{
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
...
开关(动作){
案例MotionEvent.ACTION_DOWN:
...
返回false;
案例MotionEvent.ACTION_MOVE:
...
打破;
案例MotionEvent.ACTION_UP:
...
打破;
}
返回true;
}
}
说明:
onTouch
总是呼吁pressing的观点,因为这是调度事件视图的初始状态。当你长
preSS您认为,这还叫 onTouch
第一,因为你
previously返回真正
在 onTouch
(这意味着你已经消耗
这个事件,它不应该再派遣),你不会得到所谓的 onLong preSS
。什么会做的伎俩是返回假
在 onTouch
。
贷这个回答
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);
}});
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
这篇关于Ontouch和的onclick android中列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!