0

I have a customised listview in an activity. The listview row item has a custom layout, with two text assets and one button.

The list is populated correctly and I am able to click the buttons without activating the actual item row, using '"android:descendantFocusability="blocksDescendants"' in the item layout.

But when I an tapping the different rows, I get weird hightlighting.

The list has two entries. Tapping on the main row for item #1 hightlights the entire row, fine. Tapping the button highlights the button, again fine.

But tapping the main row for item #2, the button in that row is hightlighted, not the row item itself. Tapping the button also hightlights the button, as normal.

So why isnt the #2 row being highlighted when tapped?

Activity (there is a parent RelativeView):

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/screenListView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

ListView Item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"
    android:id="@+id/name"
    android:layout_margin="5dp"
    android:layout_alignParentLeft="true"
    android:textSize="16sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"
    android:id="@+id/playlist"
    android:textSize="12sp"
    android:layout_margin="5dp"
    android:layout_below="@+id/name"
    android:layout_alignParentLeft="true" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/playButton"
    android:src="@drawable/ic_action_play"
    android:layout_alignParentRight="true" />

</RelativeLayout>

Adapter:

public class ScreenListViewAdapter extends ArrayAdapter<ScreenModel> {

private final Context context;
private final ArrayList<ScreenModel> optionsArrayList;

public ScreenListViewAdapter(Context context, ArrayList<ScreenModel> optionsArrayList) {

    super(context, R.layout.playlist_list_item, optionsArrayList);

    this.context = context;
    this.optionsArrayList = optionsArrayList;
}

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.screen_list_item, parent, false);

    if (position % 2 == 1) {
        rowView.setBackgroundColor(context.getResources().getColor(R.color.list_row_colour_1));
    }

    rowView.setTag(optionsArrayList.get(position).getPlaylistId());

    TextView name = (TextView) rowView.findViewById(R.id.name);
    TextView playlistName = (TextView) rowView.findViewById(R.id.playlist);
    ImageButton playBtn = (ImageButton) rowView.findViewById(R.id.playButton);

    playBtn.setTag(optionsArrayList.get(position).getPlaylistId());

    name.setText(optionsArrayList.get(position).getName());
    playlistName.setText(optionsArrayList.get(position).getPlaylistName());

    // handle "play" button
    playBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // do stuff
        }
    });

    return rowView;
}
Mr Pablo
  • 4,109
  • 8
  • 51
  • 104
  • Can you post adapter class? – sandeepmaaram Jan 26 '15 at 15:42
  • Aha, it seems changing the background color was the cause! Any idea why it does this? And how I can have a different color, yet fix the highlighting? – Mr Pablo Jan 26 '15 at 15:45
  • You wrote the condition like if (position % 2 == 1) { rowView.setBackgroundColor(context.getResources().getColor(R.color.list_row_colour_1)); }. It means only position value odd number then only goes into if condition – sandeepmaaram Jan 26 '15 at 16:05
  • @SandeepMaram I know... The issue is about the highlight color breaking. It has nothing to do with the conditional statement, that could even be fully removed and just have the background color set on all rows. – Mr Pablo Jan 26 '15 at 17:17

0 Answers0