[英]Separately highlight nested TextView and ImageView in ListView row
(好吧,我已经做了很多研究,找到了一些答案,但对我来说并没有什么用。。所以我终于在这里开设了一个帐户。感谢社区早些时候提供的所有答案!)
我正在创建类别选择器(带有自定义适配器的ListView)-每个类别可以具有多个子类别,每个子类别可以具有多个子类别。 对于选择器,我使用带有自定义列表项的ListView-TextView(用于显示类别)和ImageView(向前箭头),以指示类别是否具有子类别-请参见此图像:
我希望用户执行以下操作
(1)单击类别名称以选择该类别
(2)单击前进箭头以查看所有子类别(它将使用所有子类别重新填充ListView)
我希望用户看到以下亮点/关注于用户是否执行了两个操作(1)和(2)
看到这张图片:
当用户单击类别名称/行时,突出显示该行。 这是自动发生的,因为我已经设置
listView.setOnItemClickListener()
看到这张图片:
我希望用户仅在前向箭头而非行上看到突出显示。 我可以在图像上注册点击
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;
}
这就是我所做的。 这也应该是可扩展的。
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;
}
使这项工作起作用的两件事
imageView.setOnTouchListener()
允许我设置高光(感谢Piyush Gupta指出setBackgroundColor()
方法 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一起工作,请在onTouchListener()
return false
。 这会将touch事件传递给onClickListener()
。 我得到了这个解决方案https://stackoverflow.com/a/10376887/3393044 通过评论薄
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.