onTouchListener in adapter ListView make onItemClickListener not running

﹥>﹥吖頭↗ 提交于 2020-01-16 05:01:04

问题


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

}

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

回答1:


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


来源:https://stackoverflow.com/questions/27576737/ontouchlistener-in-adapter-listview-make-onitemclicklistener-not-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!