简体   繁体   English

ListView项目选择

[英]ListView item selection

I have a ListView with custom list items. 我有一个带有自定义列表项的ListView。 Each list item consists of 2 linear layouts one next to other. 每个列表项由2个线性布局组成,一个接一个。 LinearLayout 1 | LinearLayout 1 | LinearLayout 2 | LinearLayout 2 |

I've declared state list drawables for both LinearLayouts where in state_pressed I'm changing the background of the LinearLayout. 我已经声明了两个LinearLayout的状态列表可绘制对象,其中在state_pressed中,我正在更改LinearLayout的背景。

And here comes the issue - When the user taps on the LinearLayout2 only the background of LinearLayout2 should be changed, the background of LinearLayout1 should remain unchanged. 问题来了-当用户点击LinearLayout2时,仅应更改LinearLayout2的背景,而LinearLayout1的背景应保持不变。 On the other hand, when the user taps on LinearLayout1, only the background of LinearLayout1 should be changed. 另一方面,当用户点击LinearLayout1时,仅应更改LinearLayout1的背景。 But now when the user taps on either of both LinearLayouts, both of them change their background. 但是现在,当用户点击两个LinearLayouts之一时,它们两个都改变了背景。

The behaviour on tap on LinearLayout2 should be as onListItemClick() while when the user taps on LinearLayout1 a Dialog should appear (if this matters). 当用户点击LinearLayout1时,应在LinearLayout2上点击的行为应为onListItemClick(),而对话框应出现(如果这很重要)。

Any ideas how could I solve the background change issue? 有什么想法可以解决背景变更问题吗? I've tried playing with focusable and clickable options. 我尝试过使用可聚焦和可单击的选项。 If i set clickable=true to both LinearLayouts, the children (TextViews) of LinearLayout2 do not change their colour (the TextViews should change their text colour). 如果我对两个LinearLayouts都设置clickable = true,则LinearLayout2的子级(TextViews)不会更改其颜色(TextViews应该更改其文本颜色)。

Thank you! 谢谢!

This is because when using a list view you have to change some tags in XML to make the background transparent so that it will correctly work with your back ground. 这是因为使用列表视图时,您必须更改XML中的一些标签以使背景透明,以便背景可以正确使用。

Add this to your ListView XML code. 将此添加到您的ListView XML代码。

android:cacheColorHint="#00000000"

To set the ListView's background to transparent. 将ListView的背景设置为透明。

Well I think a single solution if you are using BaseAdapter as extends 好吧,如果您使用BaseAdapter作为扩展,我认为有一个解决方案

First give unique Id to both those Layouts in you xml file and add 首先为您的xml文件中的这两个布局提供唯一的ID,然后添加

android:clickable="true"

In your method 用你的方法

 public View getView(int position, View convertView, ViewGroup parent) {

when your are getting those views like 当您获得诸如

 holder.layout1_name=(LinearLayout)view.findViewById(R.id.layout1);
 holder.layout1_name.setOnClickListener( clicklayout1);
 holder.layout2_name=(LinearLayout)view.findViewById(R.id.layout2);
holder.layout2_name.setOnClickListener( clicklayout2);

Add click listener on them 在其上添加点击监听器

private OnClickListener clicklayout1 = new OnClickListener() {
    public void onClick(View v) {

         //Do what you want to do here
    }
};


private OnClickListener clicklayout2 = new OnClickListener() {
    public void onClick(View v) {

         //Do what you want to do here
    }
};

May be this may help you 可能对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM