问题描述
我为列表视图创建自定义适配器。在 getView
方法中,我为 LinearLayout
设置了 onTouchListener
自定义适配器。由于某些原因,因此我的列表视图中的 onItemClickListener
无法运行。
这是我的自定义适配器 TransactionAdapter
方法的代码:
I creating custom adapter for my list view. In getView
method, I set onTouchListener
for LinearLayout
in my custom adapter. For some reason, onItemClickListener
in my listview can't run because of this.This is my code for my custom adapter TransactionAdapter
method:
public class TransactionAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<CTransaction> transactions;
public TransactionAdapter(Context context, ArrayList<CTransaction> transactions){
inflater = LayoutInflater.from(context);
this.transactions = transactions;
}
@Override
public int getCount() {
return transactions.size();
}
@Override
public Object getItem(int position) {
return transactions.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = convertView = inflater.inflate(R.layout.detail_transaction, parent, false);
LinearLayout llBackground = (LinearLayout)convertView.findViewById(R.id.llBackground);
llBackground.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.setBackground(view.getResources().getDrawable(R.drawable.border_clicked));
break;
case MotionEvent.ACTION_UP:
v.setBackground(view.getResources().getDrawable(R.drawable.border));
v.performClick();
break;
case MotionEvent.ACTION_CANCEL:
v.setBackground(view.getResources().getDrawable(R.drawable.border));
break;
}
//Tried to use this but not working
view.onTouchEvent(event);
return true;
}
});
return convertView;
}
}
这是我的 setAdapter
和 onItemClickListener
用于ListView:
And this is my setAdapter
and onItemClickListener
for ListView:
lvTransaction = (ListView)findViewById(R.id.lvTransaction);
TransactionAdapter adapter = new TransactionAdapter(this, transactions);
lvTransaction.setAdapter(adapter);
lvTransaction.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final int positionSelected = position;
AlertDialog.Builder ad = new AlertDialog.Builder(HistoryActivity.this);
//Show the AlertDialog
}
});
推荐答案
您必须返回false才能传播触摸事件,因此点击侦听器可以捕获点击事件,因此:
you must return false to propagate the touch event so the click listener can capture the click event so:
llBackground.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.setBackground(view.getResources().getDrawable(R.drawable.border_clicked));
break;
case MotionEvent.ACTION_UP:
v.setBackground(view.getResources().getDrawable(R.drawable.border));
v.performClick();
break;
case MotionEvent.ACTION_CANCEL:
v.setBackground(view.getResources().getDrawable(R.drawable.border));
break;
}
return false;
}
});
这篇关于适配器ListView中的onTouchListener使onItemClickListener不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!