Java 适配器ListView中的onTouchListener使onItemClickListener未运行
我正在为我的列表视图创建自定义适配器。在Java 适配器ListView中的onTouchListener使onItemClickListener未运行,java,android,android-listview,custom-adapter,Java,Android,Android Listview,Custom Adapter,我正在为我的列表视图创建自定义适配器。在getView方法中,我在自定义适配器中为LinearLayout设置onTouchListener。由于某些原因,我的listview中的onItemClickListener无法运行。 这是我的自定义适配器TransactionAdapter方法的代码: public class TransactionAdapter extends BaseAdapter { private LayoutInflater inflater; priva
getView
方法中,我在自定义适配器中为LinearLayout
设置onTouchListener
。由于某些原因,我的listview中的onItemClickListener无法运行。
这是我的自定义适配器TransactionAdapter
方法的代码:
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;
}
}
您必须返回false以传播触摸事件,以便单击侦听器可以捕获单击事件,以便:
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;
}
});
让我们重新思考一下你的逻辑。您将onTouchListener设置为您的LinearLayout,我相信您拥有listview。你想知道为什么监听器不工作??我相信这是因为您在根元素(LinearLayout)上设置了onTouchListener,它会影响listview。很抱歉没有给出完整的代码,我的线性布局是自定义适配器布局,不是我的listview所在的布局。那么为什么不在listview上使用touchlistener来更改线性布局背景呢?我无法从listview中的onTouchListener获取被触摸项目的位置。。
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;
}
});