This is a great tutorial on how to get a Single Choice List in Android工作,但我还需要一件事:我想要两行文本而不是一行。所以,它看起来像这样:
|-----------------------------|
| FIRST LINE OF TEXT (o) | <- this is a "RadioButton". Ideally,
| second line of text | it would be centered vertically.
|-----------------------------|
This SO question is related,但我是Android新手,所以这有点过头了。任何人都可以为我分解它,以便我可以在上面链接的教程的上下文中使用它吗?
答案 0 :(得分:1)
您需要为此创建自定义布局。
用于显示列表
这是您的布局文件
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
这是您的自定义列表视图样式
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text view 1"/>
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text view 2"/>
</LinearLayout>
<RadioButton android:id="@+id/radiobtn"
android:width="wrap_content" android:height="wrap_content" />
</LinearLayout>
现在你必须实现任何适配器,如BaseAdapter,ArrayAdapter等。
并在该
中使用此自定义列表视图 像这样private class CustomAdapter extends ArrayAdapter<Order> {
private ArrayList<Model> items;
public OrderAdapter(Context context, int textViewResourceId, ArrayList<Model> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom_list, null); // here you have to implement custom_list.xml file
}
Model m = items.get(position);
if (m != null) {
TextView text1 = (TextView) v.findViewById(R.id.text1);
TextView text2 = (TextView) v.findViewById(R.id.text2);
RadioButton rb = (RadioButton) v.findViewById(R.id.radiobtn);
text1.setText(m.text1);
text1.setText(m.text2);
rb.setChecked(m.isChecked);
}
return v;
}
}
这是列表项的Model类
private class Model{
String text1;
String text2;
boolean isChecked;
}
答案 1 :(得分:0)
阅读simple_list_item_single_choice.xml的源代码,我们可以弄清楚如何制作一个实现Checkable的自定义窗口小部件,如下所示:
档案simple_list_item_2_single_choice.xml
<?xml version="1.0" encoding="utf-8"?>
<customwidgets.CheckedLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckedTextView
android:id="@+id/text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:checkMark="?android:attr/listChoiceIndicatorSingle" />
</customwidgets.CheckedLinearLayout>
也就是说,我们根据需要添加尽可能多的TextView
,并将最后一个添加为CheckedTextView
。
然后,在我们的自定义CheckedLinearLayout
中,我们找到哪个是布局的Checkable
子项,并将我们实现的Checkable
的每个方法分发给该子项,如下所示:
档案CheckedLinearLayout.java
package customwidgets;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.LinearLayout;
/**
* Useful class inside a ListView that needs to have checkable items,
* such as radio buttons (single_choice) or check boxes (multiple_choice).
*/
public class CheckedLinearLayout extends LinearLayout implements Checkable {
private Checkable checkedView;
public CheckedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean isChecked() {
return checkedView == null ? false : checkedView.isChecked();
}
@Override
public void setChecked(boolean checked) {
if (checkedView != null) checkedView.setChecked(checked);
}
@Override
public void toggle() {
if (checkedView != null) checkedView.toggle();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
int count = getChildCount();
for (int i = count - 1; i >= 0; i--) {
View view = getChildAt(i);
if (view instanceof Checkable) {
checkedView = (Checkable) view;
break;
}
}
}
}
当然,在所需的布局xml文件中,我们将ListView
设置为单一选择:
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" >
</ListView>