【问题标题】:Separately highlight nested TextView and ImageView in ListView row在 ListView 行中分别突出显示嵌套的 TextView 和 ImageView
【发布时间】:2014-04-10 22:48:09
【问题描述】:

(好吧,我研究了很多并找到了一些答案,但没有什么对我真正有用..所以我终于开了一个帐户在这里发帖。感谢社区之前所有的答案!)

上下文

我正在创建一个类别选择器(带有自定义适配器的 ListView)- 每个类别可以有多个子类别,每个子类别可以有多个子类别。对于选择器,我使用了一个带有自定义列表项的 ListView - TextView(显示类别)和一个 ImageView(向前箭头)来指示该类别是否有子类别 - 请参见此图片:


我希望用户执行以下操作
(1) 点击类别名称选择该类别
(2) 点击前进箭头查看所有子类别(它将用所有子类别重新填充 ListView)

问题从这里开始

我希望用户看到以下亮点/关注用户执行大约两个操作 (1) 和 (2)

强调行动 (1)

看这张图片:


当用户点击类别名称/行时突出显示该行。 这会自动发生,因为我已经设置了

listView.setOnItemClickListener()

动作亮点 (2)

看这张图片:


我希望用户在前进箭头上而不是行上看到突出显示。我可以使用

在图像上注册 CLICK
imageView.setOnClickListener()

在我的自定义适配器中,但我无法在前进箭头上突出显示

如何在 ImageView 上获得此高亮显示?

代码

    Activity's onCreate() {
      LayoutInflater inflater = getActivity().getLayoutInflater();
      View categorySelectView = inflater.inflate(R.layout.cat_select, null);
      LinearLayout categorySelectLayout = (LinearLayout) categorySelectView;
      ListView categoryListView = (ListView) categorySelectLayout.findViewById(R.id.list_cat_select);
      CategoryArrayAdapter categoryArrayAdapter = new CategoryArrayAdapter(this.getActivity(), CategoryAdapter.getAllCategories());
      categoryListView.setAdapter(categoryArrayAdapter);
      categoryListView.setOnItemClickListener(categorySelectClickHandler);
    }

CategoryARrayAdapter's getView(int position, View covertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.cat_item, parent, false);

    TextView textView = (TextView) rowView.findViewById(R.id.cat_name);
    Category catToDisplay = categories.get(position);
    textView.setText(catToDisplay.getName());

    if(catToDisplay.isParent()) {
        ImageView imageView = (ImageView) rowView.findViewById(R.id.cstrow_cat_expand);
        imageView.setImageResource(R.drawable.cat_expand);
        imageView.setOnClickListener(categoryExpandClickHandler);
        imageView.setFocusable(false); // to make sure the highlight on row shows
        imageView.setFocusableInTouchMode(false); // to make sure the highlight on row shows
    }

    return rowView;
}

【问题讨论】:

  • 点击按钮更改背景。否则为 xml 文件中的按钮设置选择器。
  • 部分使用imageView.SetBackgroudColor(#color) 可以解决问题。我将不得不手动将颜色重置为透明。我如何找到(和我们)默认系统突出显示颜色android.R.drawable.listSelector 提到它是默认颜色,但在使用 getActivity().getResource().getDrawable(android.R.drawable.listSelector) 时出现 ResourceNotFound 异常@

标签: android listview android-listview imageview highlight


【解决方案1】:

这就是我所做的。这也应该是可扩展的。

CategoryArrayAdapter 有以下getView()

    public View getView(int position, View covertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.cat_select, parent, false);

        TextView textView = (TextView) rowView.findViewById(R.id.cat_name);
        Category catToDisplay = categories.get(position);
        textView.setText(catToDisplay.getName());

        if(catToDisplay.isParent()) {
            ImageView imageView = (ImageView) rowView.findViewById(R.id.cstrow_cat_expand);
            imageView.setImageResource(R.drawable.cat_expand);
            imageView.setOnClickListener(categoryExpandClickHandler);
            imageView.setOnTouchListener(categoryExpandTouchHandler);
            imageView.setFocusable(false); // to make sure the highlight on the item shows
            imageView.setFocusableInTouchMode(false); // to make sure the highlight on the item shows
            imageView.setTag(catToDisplay);
        }

        return rowView;
    }

让这项工作成功的两件事

  1. imageView.setOnTouchListener() 允许我设置突出显示(感谢 Piyush Gupta 指出 setBackgroundColor() 方法
  2. imageView.setOnClickListener() 允许我在用户点击箭头时执行操作

    /**
     * This method listens to the TOUCH CLICK on the IMAGEVIEW to give it button like feeling
     */
    private View.OnTouchListener categoryExpandTouchHandler = new View.OnTouchListener() {
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                v.setBackgroundColor(getContext().getResources().getColor(R.color.highlight_cat_expand));
                break;
            }
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP: {
                v.setBackgroundColor(getContext().getResources().getColor(android.R.color.transparent));
                break;
            }
        }
        return false;
    }
    };
    
    /**
     * This method listens to EXPAND IMAGEVIEW CLICK in the LISTVIEW of categories
     */
    private View.OnClickListener categoryExpandClickHandler = new View.OnClickListener() {
    
    @Override
    public void onClick(View view) {
        // Expanding this category
        ... expand code here ...
    }
    };
    

要让 HIGHLIGHT 和 CLICK 一起工作,return false in onTouchListener()。这会将触摸事件传递给onClickListener()。我从https://stackoverflow.com/a/10376887/3393044 the thin 的评论中得到了这个解决方案

【讨论】:

    猜你喜欢
    • 2022-11-28
    • 2019-02-14
    • 2012-01-22
    • 2019-05-18
    • 2021-10-19
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多