我想在android listview項目中實現ontouch和onclicklistener。Android和列表項onclick
如果我選擇行並單擊一行,需要調用onclicklistener。
同時如果我有左右滑動列表行,需要在適配器文件中調用ontouchlistener。
我該怎麼辦?請給我一個這樣的解決方案?
這裏刷卡工作fine.but 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);
}});
你想上的所有觀點,即代表名單或該行的某些特殊項目的行執行的onClick? – Eddy 2014-10-08 10:54:06
我需要在列表的選定行上執行onclick()函數。 – 2014-10-08 10:55:32
在您的列表視圖項目中使用手勢監聽器,它將同時提供觸摸以及點擊監聽器。 – Meenal 2014-10-08 11:34:22