0

I have a ListView with custom list items. Each list item consists of 2 linear layouts one next to other. LinearLayout 1 | LinearLayout 2 |

I've declared state list drawables for both LinearLayouts where in state_pressed I'm changing the background of the 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. On the other hand, when the user taps on LinearLayout1, only the background of LinearLayout1 should be changed. But now when the user taps on either of both LinearLayouts, both of them change their background.

The behaviour on tap on LinearLayout2 should be as onListItemClick() while when the user taps on LinearLayout1 a Dialog should appear (if this matters).

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).

Thank you!

Zheko
  • 673
  • 6
  • 16
  • Please elaborate more , Does your row of listview contains two layout or some thing else – Abhinav Singh Maurya Oct 04 '11 at 13:53
  • LinearLayout1 contains only an ImageView. LinearLayout2 contains several TextViews and ImageViews. Both LinearLayout1 and 2 are put in a parent LinearLayout with orientation=horizontal. – Zheko Oct 04 '11 at 13:56

2 Answers2

0

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.

Add this to your ListView XML code.

android:cacheColorHint="#00000000"

To set the ListView's background to transparent.

coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
  • It does not work. Moreover, the backgrounds of the LinearLayouts do change, but they both change, not only for one of them. – Zheko Oct 04 '11 at 13:58
  • What does your code look like for your onItemSelected? Or what your using to change the color's? It seems like your changing the color for both linearlayouts. – coder_For_Life22 Oct 04 '11 at 14:00
  • I have 2 state list drawables - one for the first LinearLayout and one for the second LinearLayout. Both state list drawables have android:state_pressed with the new background set as drawable. – Zheko Oct 04 '11 at 14:08
  • Well if you dont want the background for linearlayout1 or 2 to change..Why are you setting its pressed state? Of course it will change colors. – coder_For_Life22 Oct 04 '11 at 14:10
0

Well I think a single solution if you are using BaseAdapter as extends

First give unique Id to both those Layouts in you xml file and add

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

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
  • Yeah, doing the background change via java code shouldn't be a hard thing to do, I've just wanted to do it via XML/state list drawables and things like this, it's more elegant. – Zheko Oct 04 '11 at 18:40
  • Well if you want dynamic you have to do in java but if you want static you can do in xml. But my friend be precautive while using in java because if you dont release resource you may be caught up in great memory issue – Abhinav Singh Maurya Oct 05 '11 at 06:08