Reputation: 1447
I am developing an app which uses two fragments. One of the fragments uses a ListView to display items and when clicked, the second fragment updates itself (it is a dictionary app). Once an item is selected in the ListView, I want it to be highlighted. I achieved this by using the following properties in XML file for ListView item.
android:choiceMode="singleChoice"
android:listSelector="@android:color/darker_gray"
This works fine as far as selection of the item is concerned. But the problem is when I scroll the list, the selection sometimes stays on the at the bottom or top of the list (as the case may be) even though the selected item is not present in the list. Here are screenshots to explain my problem.
The first pic shows no selection; in second pic, I selected 101 and hence second fragment was updated. In the third pic, even though 101 is not shown, a part of the ListView is highlighted.
What's the issue ?
Thanks.
EDIT Here is the OnItemClick() method.
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
String selectedWord = words.get(arg2); //words in an ArrayList which holds all the words displayed in this fragment.
Meaning meaningFragment = (Meaning) getSupportFragmentManager().
findFragmentById(R.id.fragmentMeaning);
meaningFragment.searchMeaning(selectedWord); //this method updates the other fragment.
}
EDIT:
This problem is not resolved yet. I have uploaded the project on github if anyone wants to have a look.
Upvotes: 1
Views: 805
Reputation: 8281
In Eclipse you can create a Master-Detail Flow which is an example of what you are trying to do.
For this create a "New Android Application" and in the third or forth screen which is called "Create Activity", three possibilities are offered to you, select the one called "Master/Detail".
This does not provide an answer to your question but is just pointing you to a working example where you can elaborate from.
Now for the answer to your question, I looked at your code and found the mistake.
Here is the working code for the onCreateView()
. I commented out the wrong lines of code.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_one, container);
listView = (ListView) v.findViewById(R.id.listView);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//listView.setAdapter(new ArrayAdapter<String>(getActivity(),
// android.R.layout.simple_dropdown_item_1line, array));
//listView.setSelector(android.R.color.holo_blue_dark);
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, array));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
listView.setItemChecked(position, true);
FragmentTwo frag = (FragmentTwo) getActivity()
.getFragmentManager()
.findFragmentById(R.id.fragmentTwo);
frag.updateTextView(array[position]);
}
});
return v;
}
In your code the 2 following lines are wrong
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_dropdown_item_1line, array));
listView.setSelector(android.R.color.holo_blue_dark);
what you do here is set a simple_dropdown_item_1line
which is not what you want and you manually set the selector
.
You have to use simple_list_item_activated_1
and not set the selector manually.
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, array));
//listView.setSelector(android.R.color.holo_blue_dark);
If you want a custom selector color, you can create a custom selector by copying the simple_list_item_activated_1
and editing it to your needs and calling the edited layout.
However I recommend using the default colors so the day Android decides to change its color scheme you will be compliant to the new one.
In summary, to have the ListView.CHOICE_MODE_SINGLE
working, you have to use simple_list_item_activated_1
and not manually set the selector
.
Upvotes: 1