开发者

ListView item selection

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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜