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.