ListView onItemClick无法使用自定义适配器

时间:2014-10-10 17:33:07

标签: android listview onitemclick

我搜索了一个解决方案,找不到任何可以指向解决方案的内容。

我正在使用ListView,其中每个项目都有TextView和Horizo​​ntalScrollView。 Horizo​​ntalScrollView在运行时填充了一些TextView。当用户单击其中一个TextView时,我在视图上切换背景,为此扩展了TextView类。

问题是ListView onItemClick不会触发。我玩了一些代码,由于一个我不明白的原因,点击事件有时会触发,但只有当我点击每个列表项之间时。 我假设它是因为我的TextView处理click事件或者因为其中一个布局阻止了事件。

修改

我要做的是:

  1. 单击TextView时,会在其上切换一些视觉效果。

  2. 将其值(文本)添加到数据结构中。

  3. 我的自定义TextView处理点击事件很好,但我无法在适配器中获取该点击事件。我需要它,因为我需要知道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;
            }
        }
    

    }

    取消部分活动:

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

1 个答案:

答案 0 :(得分:0)

您遇到的问题是因为您在listview内部的视图上设置了OnClickListener,这里发生的事情是覆盖了listview的默认OnClickListener。你最好的选择就是使用它。

row.setOnClickListener(new OnClickListener(){

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

  });