在我的片段中,我有4个ListView(所有4个包含带有单选按钮的自定义行),用户需要为每个ListView选择一行。最初我无法在用户选择特定行时检查单选按钮,但是我遇到了这个适合我的解决方案https://stackoverflow.com/a/12823457/3739918
现在一切都很好,除了我在配置更改后无法保留已检查的单选按钮(例如,手机从纵向更改为横向,反之亦然)。我曾尝试使用setRetainInstance(true),它可以在配置更改之前保留选择(我只使用整数数组来跟踪不同列表视图中的用户选择),但是我没有成功使用这些信息在配置更改后重新检查相应的单选按钮。
以下是包含ListViews(减去不相关的行)的片段代码:
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
我将手机更改为横向模式,单选按钮选择消失。
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
有关修复的任何建议吗?
答案 0 :(得分:0)
根据您的情况使用
View rowView = null;
if (convertView == null) {
View rowView = inflater.inflate(R.layout.listitem, parent, false);;
} else {
rowView = convertView;
}