单独突出显示ListView行中嵌套的TextView和ImageView

时间:2014-03-07 16:55:00

标签: android listview android-listview imageview highlight

(好吧,我已经研究了很多并且找到了一些答案,但没有什么对我有用..所以我终于开了一个帐户来发布这里。感谢社区早些时候提供所有答案!)

上下文

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

image


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

问题从这里开始

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

突出显示操作(1)

看到这张图片:

image


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

listView.setOnItemClickListener()

突出显示操作(2)

看到这张图片:

image


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

在图像上注册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;
}

1 个答案:

答案 0 :(得分:0)

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

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 ...
    }
    };
    
  3. 要使亮点和点击一起使用return false onTouchListener()。这会将触摸事件传递给onClickListener()。我通过https://stackoverflow.com/a/10376887/3393044

    the thin 评论获得了此解决方案