【问题标题】:ListView onItemClick not working with custom adapterListView onItemClick 不适用于自定义适配器
【发布时间】:2014-10-10 17:33:07
【问题描述】:

我已经搜索了一个解决方案,但找不到任何可以让我找到解决方案的东西。

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

问题是 ListView onItemClick 不会触发。我玩了一点代码,由于我不明白的原因,有时会触发单击事件,但只有当我在每个列表项之间单击鼠标右键时才会触发。 我认为这要么是因为我的 TextView 处理了单击事件,要么是因为其中一种布局阻止了该事件。

编辑:

我想做的是:

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

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

我的自定义 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");
        }
});
...

【问题讨论】:

  • 我认为它可以工作,因为您的 List 项目有一个 onClick 监听器来吸引焦点。不过可能是错的。
  • 显示 aspect_list_answer.xml 的代码

标签: android listview onitemclick


【解决方案1】:

您遇到的问题是因为您在列表视图内的视图上设置了 OnClickListener,这里发生的情况是这会覆盖列表视图的默认 OnClickListener。最好的办法是使用它。

row.setOnClickListener(new OnClickListener(){

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

  });

【讨论】:

  • 如果单击子 TextView,这将不起作用。查看我的编辑。
  • 嗯,如果我理解正确,您是在尝试获取刚刚单击列表视图的 TextView 的文本并对其进行处理?如果你能进一步详细说明,我希望能帮助你。
  • 这个想法是用户从每个主题中选择关键字(主题 = 行/水平视图)。对于每个选择,我需要执行一个选择效果,并将关键字添加到稍后将发送到服务器的关键字列表中。所以我需要知道关键字(TextView)和被点击的行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多