在我的片段中,我有4個ListView(全部4個包含具有單選按鈕的自定義行),其中用戶需要爲每個ListView選擇一行。最初我無法得到單選按鈕進行檢查,只要用戶選擇一個特定的行,但我碰到這種解決方案,這對我運行良好https://stackoverflow.com/a/12823457/3739918在配置更改後保留ListView中的單選按鈕檢查
現在一切都很好,除了我無法保留在配置改變後檢查單選按鈕(例如,手機從縱向改變爲橫向或反之亦然)。我試圖使用setRetainInstance(true),它在配置更改之前保留選擇(我只是使用整數數組來跟蹤不同列表視圖中的用戶選擇),但是我一直未能成功使用這些信息在配置更改後重新檢查相應的單選按鈕。
這裏是包含列表視圖(減去無關行)我的片段代碼:
public class FourBandFrag extends Fragment implements
AdapterView.OnItemClickListener {
private ArrayList<ListView> lists = new ArrayList<ListView>();
private ArrayList<CustomAdapter> adapters = new ArrayList<CustomAdapter>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
View result = inflater.inflate(R.layout.fourbandlayoutfrag, container,
false);
return result;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lists.add((ListView) getActivity().findViewById(R.id.FourListView1));
lists.add((ListView) getActivity().findViewById(R.id.FourListView2));
lists.add((ListView) getActivity().findViewById(R.id.FourListView3));
lists.add((ListView) getActivity().findViewById(R.id.FourListView4));
adapters.add(new CustomAdapter(getActivity(), generateData(1)));
adapters.add(new CustomAdapter(getActivity(), generateData(1)));
adapters.add(new CustomAdapter(getActivity(), generateData(2)));
adapters.add(new CustomAdapter(getActivity(), generateData(3)));
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setAdapter(adapters.get(i));
}
for (int q = 0; q < lists.size(); q++) {
lists.get(q).setOnItemClickListener(this);
}
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setAdapter(adapters.get(i));
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//This checks the corresponding radio button for the corresponding listview
((CustomAdapter) parent.getAdapter()).setSelectedIndex(position);
((CustomAdapter) parent.getAdapter()).notifyDataSetChanged();
}
}
的CustomAdapter.java樣子(減去不相關的線):
public class CustomAdapter extends ArrayAdapter<Model> {
private final Context context;
private final ArrayList<Model> modelsArrayList;
private int selectedIndex = -1;
public CustomAdapter(Context context, ArrayList<Model> modelsArrayList) {
super(context, R.layout.listitem, modelsArrayList);
this.context = context;
this.modelsArrayList = modelsArrayList;
}
@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.listitem, parent, false);
RadioButton radiobutton = (RadioButton) rowView
.findViewById(R.id.radioButton);
// Check the correct radio button when person selects a listview item
if (selectedIndex == position) {
radiobutton.setChecked(true);
} else {
radiobutton.setChecked(false);
}
return rowView;
}
public void setSelectedIndex(int index) {
selectedIndex = index;
}
}
任何人有想法?
先謝謝
更新1
因爲當一個行的用戶點擊,我決定利用信息保留有關的用戶選擇,以模擬點擊一個單選按鈕,才檢查到配置更改。所以,現在我的片段看起來是這樣的:
public class FourBandFrag extends Fragment implements
AdapterView.OnItemClickListener {
private ArrayList<ListView> lists = new ArrayList<ListView>();
private ArrayList<CustomAdapter> adapters = new ArrayList<CustomAdapter>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
View result = inflater.inflate(R.layout.fourbandlayoutfrag, container,
false);
return result;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lists.add((ListView) getActivity().findViewById(R.id.FourListView1));
lists.add((ListView) getActivity().findViewById(R.id.FourListView2));
lists.add((ListView) getActivity().findViewById(R.id.FourListView3));
lists.add((ListView) getActivity().findViewById(R.id.FourListView4));
adapters.add(new CustomAdapter(getActivity(), generateData(1)));
adapters.add(new CustomAdapter(getActivity(), generateData(1)));
adapters.add(new CustomAdapter(getActivity(), generateData(2)));
adapters.add(new CustomAdapter(getActivity(), generateData(3)));
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setAdapter(adapters.get(i));
}
for (int q = 0; q < lists.size(); q++) {
lists.get(q).setOnItemClickListener(this);
}
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setAdapter(adapters.get(i));
}
// Retain radio button selections after configuration change
for (int w = 0; w < bandRowNumber.length; w++) {
if (bandRowNumber[w] != unselected) {
lists.get(w).performItemClick(
lists.get(w).getAdapter()
.getView(bandRowNumber[w], null, null),
bandRowNumber[w],
lists.get(w).getAdapter().getItemId(bandRowNumber[w]));
Log.d(TAG, "Rechecking radio button " + bandRowNumber[w] + " from ListView " + w);
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//This checks the corresponding radio button for the corresponding listview
((CustomAdapter) parent.getAdapter()).setSelectedIndex(position);
((CustomAdapter) parent.getAdapter()).notifyDataSetChanged();
}
}
這肯定不會嘗試重新檢查正確的單選按鈕,從我得到的logcat的判斷,但是仍然單選按鈕的配置變化後選中。下面是它的樣子: 在縱向模式下,我在每個列表視圖中進行選擇。
http://s17.postimg.org/nm4wyve4v/portrait.png
我電話更改爲橫向模式和單選按鈕選擇dissapear。
http://s10.postimg.org/459rrgc9l/landscape.png
從縱向改爲橫向後logcat的是:
07-02 12:56:14.407: D/FourBandFrag(30090): Rechecking radio button 0 from ListView 0
07-02 12:56:14.417: D/FourBandFrag(30090): Rechecking radio button 1 from ListView 1
07-02 12:56:14.427: D/FourBandFrag(30090): Rechecking radio button 2 from ListView 2
07-02 12:56:14.427: D/FourBandFrag(30090): Rechecking radio button 3 from ListView 3
用於修復有什麼建議?
感謝您的建議。我試圖添加這個,但它仍然有相同的問題。配置更改後,單選按鈕保持未選中狀態。 – coldbuffet