gpt4 book ai didi

java - 适配器 ListView 中的 onTouchListener 使 onItemClickListener 未运行

转载 作者:行者123 更新时间:2023-11-29 05:13:08 25 4
gpt4 key购买 nike

我为我的 ListView 创建自定义适配器。在 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;
}

}

这是我的 setAdapteronItemClickListener 用于 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 才能传播触摸事件,这样点击监听器才能捕获点击事件:

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

关于java - 适配器 ListView 中的 onTouchListener 使 onItemClickListener 未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27576737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com