我用单选按钮制作了一个列表视图,但是这些项目的行为不像普通的单选按钮,因为我认为没有将它们结合在一起的 radiougroup。
我可以以编程方式将它们添加到不在 xml 中的无线电组吗?当用户按下不在列表视图中的按钮时,我希望能够找到选择了哪个项目。
我试过这个但失败了,有人可以帮助我吗?
这是我的片段:
public class DietTypeFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener {
Button Next;
Button Back;
private InteractionListener listener;
ListView list;
private static String[] DietList = {"Low Fat", "Low Carb", "Bodybuilder", "Balanced", "Diabetes", "Gluten Free", "Vegeterian"};
private static String[] DietListSubTxt = {"For weight loss and the less active", "For weight loss and general fitness",
"For Strength building", "For all around fitness and nutrition",
"Carbohydrates monitored diet", "Diet excluding foods containing gluten", "Plant-based diet"};
List<NameValuePair> DietType = new ArrayList<NameValuePair>();
public DietTypeFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Next = (Button) getActivity().findViewById(R.id.next_btn);
Next.setOnClickListener(this);
Back = (Button) getActivity().findViewById(R.id.back_btn);
Back.setOnClickListener(this);
listener = (InteractionListener) getActivity();
list = (ListView) getActivity().findViewById(R.id.list_diet_type);
// If you call this fragment more than once (press the back button) then you dont want to add the string again
if (DietType.isEmpty()) {
for(int i=0; i< DietList.length; i++) {
DietType.add(new BasicNameValuePair(DietList[i], DietListSubTxt[i]));
}
}
ArrayAdapter adapter = new ArrayAdapter (getActivity(), R.layout.simple_list_item_2_single_choice, android.R.id.text1, DietType) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
NameValuePair data = DietType.get(position);
text1.setText(data.getName());
text2.setText(data.getValue());
return view;
}
};
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_diet_type, container, false);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("DietTypeFrag",Integer.toString(view.getId()));
}
public void onStop() {
super.onDetach();
//listener = null;
}
@Override
public void onClick(View view) {
listener.DietTypeFragmentChangeScreen(view.getId());
Log.d("DietTypeFrag","next or back clicked?");
}
public interface InteractionListener {
public void DietTypeFragmentChangeScreen(int id);
}
}
这是片段调用的 xml 布局:simple_list_item_2_single_choice
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:paddingStart="16dip"
android:paddingEnd="12dip"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="?android:attr/textColorAlertDialogListItem"
android:gravity="center_vertical|start"
android:singleLine="true"
android:ellipsize="marquee" />
<TextView android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorAlertDialogListItem"
android:gravity="center_vertical|start"
android:singleLine="true"
android:ellipsize="marquee" />
</LinearLayout>
<RadioButton
android:id="@+id/rdbtn_sc"
android:layout_width="35dip"
android:layout_height="wrap_content"
android:paddingEnd="12dip"
android:gravity="center_vertical"
android:focusable="false"
android:clickable="false" />
</LinearLayout>
然后还有另一个带有自己的 xml 的片段,其中包含后退和下一个按钮,但这不是问题的一部分。