简体   繁体   中英

ListView onItemClick not working with custom adapter

I've searched for a solution and couldn't find anything that would point me to the solution.

I'm using a ListView where each item has a TextView and a HorizontalScrollView. The HorizontalScrollView is filled with a few TextViews on runtime. When the user clicks one of these TextViews, I toggle a background on the view, for which I extended TextView class.

The problem is that ListView onItemClick won't fire. I played with the code a bit, and for a reason I don't understand, the click event would sometimes fire but only when i click right in between each list item. I assume it's either because my TextView handles the click event or because one of the layouts is preventing the event.

Edit:

What i'm trying to do is:

  1. When TextView is clicked, toggle some visual effect on it.

  2. add its value (text) to data structure.

My custom TextView handles the click event fine, but I can't grab that click event in the adapter. I need it because I need to know the position in the ListView.

public class KeywordListAdapter extends ArrayAdapter<String> implements OnClickListener {

    private final Context context;
    private final String[] values;
    private AspectManager aspectManager;

    static class ViewHolder {
            protected TextView aspectName;
            protected HorizontalScrollView horizContainer;
            protected LinearLayout keyWrapper;
            protected KeywordTextView[] keyword;
    }

    public KeywordListAdapter(Context context, String[] values) {
            super(context, R.layout.aspect_list_answer, values);
            this.context = context;
            this.values = values;
            aspectManager = AspectManager.getInstance();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            Aspect aspect = aspectManager.getAspectByName(values[position]);
            View row = convertView;

            // reuse views
        if (row == null) {
            Log.w(Constants.TAG, "row == null");
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.aspect_list_answer, parent, false);

                    ViewHolder viewHolder = new ViewHolder();

                    viewHolder.horizContainer = (HorizontalScrollView) row.findViewById(R.id.horizontalRow);
                    viewHolder.horizContainer.setHorizontalScrollBarEnabled(false);

                    viewHolder.keyWrapper =  new LinearLayout(context);
                    viewHolder.keyWrapper.setOrientation(LinearLayout.HORIZONTAL);
                    viewHolder.keyWrapper.setId(0);

                    viewHolder.keyword = new KeywordTextView[aspect.getKeywordCount()];
                    viewHolder.aspectName = (TextView) row.findViewById(R.id.lblAspect);

                    String[] keywords = aspect.getAspectKeys();
                    for (int i=0; i< keywords.length; i++){
                            viewHolder.keyword[i] = new KeywordTextView(context);
                            viewHolder.keyword[i].setOnClickListener(this);
                            viewHolder.keyWrapper.addView(viewHolder.keyword[i]);
                    }

                    viewHolder.horizContainer.addView(viewHolder.keyWrapper);
                    row.setTag(viewHolder);
        }

        //Set values
        ViewHolder holder = (ViewHolder) row.getTag();

            //Fill aspect name
            holder.aspectName.setText(aspect.getAspectName());
            holder.aspectName.setTextColor(Color.WHITE);

            //Fill keywords
            String[] keywords = aspect.getAspectKeys();
            for (int i=0; i< keywords.length; i++){
                    holder.keyword[i].setKeyword(keywords[i]);
                    holder.keyword[i].setColor(baseColor);

            //set keyword layout
                    LinearLayout.LayoutParams llp =  new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    llp.gravity = Gravity.CENTER_VERTICAL;
                    llp.setMargins(20, 15, 20, 20);
                    holder.keyword[i].setLayoutParams(llp);
            }

            return row;
    }

    @Override
    public void onClick(View v) {
            ((KeywordTextView)v).toggle();
    }

    public class KeywordTextView extends TextView {
            String keyword;
            String color;
            boolean selected;
            private GradientDrawable gd;

        public KeywordTextView(Context context) {
            super(context);
            gd = new GradientDrawable();
            selected = false;
        }

        public void setKeyword(String keyword){
            this.keyword = keyword;
            setText(keyword);
        }

        public String getKeyword(){
            return keyword;
        }

        public void setColor(String color){
            this.color = color;
            setText(keyword);
        }

        public boolean isKeywordSelected(){
            return this.selected;
        }

        protected void onDraw (Canvas canvas) {
            super.onDraw(canvas);

            //Set style
            ...
        }

        protected void toggle(){
            selected = !selected;
        }
    }

}

Relevent part of the activity:

...
aspectList = (ListView)findViewById(R.id.lvAspectList);
aspectList.setAdapter(new KeywordListAdapter(this, aspects.split(", ")));
aspectList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Log.w(TAG, "aspectList onItemClick");
        }
});
...

The problem you're having is because you've set an OnClickListener on views inside of the listview,what happens here is this overrides the default OnClickListener for listviews. Your best bet would be to use this.

row.setOnClickListener(new OnClickListener(){

      @Override
      public void onClick(View v) {
         //Enter Your Code Here
      }

  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM