1

I have a two panes layout with a list view and a PagerView that shows the full contents of the selected item. I want a two way relationship between this two.

  • When new item selected from list view, item will be highlighted and ViewPager shows the corresponding content.
  • When the ViewPager scroll to new item. Coresponding Item will be selected in list view.

I have set background collor in single_row.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:background="@drawable/bg_key"

    >


<ImageView
        .../>
<TextView
        .../>
<TextView
        .../>
 </RelativeLayout>

Here is my drowable/bg_key.xml file

<?xml version="1.0" encoding="utf-8"?>

 <selector xmlns:android="http://schemas.android.com/apk/res/android">


<item android:state_selected="true"
      android:drawable="@color/pressed_color"
      />
<item
        android:drawable="@color/default_color" />
</selector>

Also i used singleChoise mode.

<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_gravity="center_horizontal"
        android:choiceMode="singleChoice" />

And here is my ListItemClicked Event handler.

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            communicator.indexChanged(position);
            view.setSelected(true);
        }
    });

And Everything works perfect!! except not everything. When i Click on a item it will get selected and highlighted and the ViewPager Shows the content. But when the ViewPager scroll to new item I can't highlight the corresponding item in list view.

In MyListFragment there is a method called hightlightItem wich will called when Content in PagerView changes.

 public void highlightItem(int position){
    //position is a index of item i want to hightlight using java. All code to here are tested!
    listView.setSelection(position); //? this will not select any item. Also it will remove the selected item as well
    listView.setChecked(position,true); //? doesn't work neither 
 }

So what should i do? I have a index of item and i want to highlight it inside this method.

Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
  • checkout this question http://stackoverflow.com/questions/10788688 – kiruwka Apr 01 '14 at 11:34
  • sorry, I haven't noticed you already supply `selector` for items. Please see my answer for you problem. – kiruwka Apr 01 '14 at 14:06
  • No i didn't. I did set it in xml file. you'r find it if you look again in code i posted. I don't know why that's not working but I've got the result i wanted. – Alireza Ahmadi Apr 03 '14 at 17:28
  • yep, that's what I meant. `` that you set in xml. I put that comment before answering the question 2 days ago : ) – kiruwka Apr 03 '14 at 18:16

3 Answers3

1

either add listView.requestFocusFromTouch() :

public void highlightItem(int position) {
    listView.requestFocusFromTouch();
    listView.setSelection(position);
 }

or

Alternative solution is to call listView.setItemChecked(position, true); without requesting focus or setting selection, for this to work add

<item android:state_activated="true" android:drawable="@color/pressed_color"/>

in your drowable/bg_key.xml

Important: : oh, and dont forget to set choice mode for you list, you need to do it once, i.e.:

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Hope that helps

kiruwka
  • 9,250
  • 4
  • 30
  • 41
  • Thank you for your answer. First solution didn't work but second works perfect. I tried this before but instead of using android:state_activated="true" i was using android:state_selected="true" witch leads to wrong effect. I also found another solution witch I will post as an answer. Thank you – Alireza Ahmadi Apr 02 '14 at 16:17
  • strange. For me both solutions work fine. Did you perhaps forget to `setChoiceMode` ? It is required for both of them to work. – kiruwka Apr 02 '14 at 16:36
  • First solution doesn't work for me with ListViews that have more than one item. The first item in the list is highlighted just fine, but when I navigate to other views they aren't highlighted. Selection works as it should, it's just the visual effect that's missing. – Graph Theory Apr 07 '16 at 00:18
1

kiruwka's second solution is totaly working and I'm using it in my project. However here is another (a little more complex) solution witch i found.

public void highlightItem(int position){
    int selectedIndex = listView.getSelectedItemPosition();
    if(position != selectedIndex)
    {

        listView.performItemClick(
                listView.getAdapter().getView(position, null, null),
                position,
                listView.getAdapter().getItemId(position));
    }
}
Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
  • 2
    +1 for the motivation to contribute. I think, however, that while this solution might be ok for you, generally it may not be desired to trigger extra `onItemClick` event for the list. For example it will adjust your ViewPager to specified position and show some animation, while this is already not actually needed(as ViewPager's position already set). Anyways, this is useful, thanks. – kiruwka Apr 02 '14 at 16:46
0

Try this :

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        for (int j = 0; j < adapterView.getChildCount(); j++)
            adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);

        // change the background color of the selected element
        view.setBackgroundColor(R.color.YOUR_COLOUR);
});
Virag Brahme
  • 2,062
  • 1
  • 20
  • 32