Phi*_*hil 34 user-interface android listview custom-view
我见过ApiDemos的com.example.android.apis.view.List11示例.在该示例中,每行采用视图android.R.simple_list_item_multiple_choice.每个这样的视图都有a TextView
和a CheckBox
.
现在我希望每个视图都有2 TextView
s和1 CheckBox
,有点类似于List3示例.我尝试像这样创建一个自定义布局文件row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="@+id/checkbox"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/text_name"
android:textSize="13px"
android:textStyle="bold"
android:layout_toLeftOf="@id/checkbox"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_phone"
android:textSize="9px"
android:layout_toLeftOf="@id/checkbox"
android:layout_below="@id/text_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
然后在Activity
s中onCreate()
,我喜欢这样:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Query the contacts
mCursor = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row,
mCursor,
new String[] { Phones.NAME, Phones.NUMBER},
new int[] { R.id.text_name, R.id.text_phone });
setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
Run Code Online (Sandbox Code Playgroud)
结果看起来像我想要的,但看起来列表不知道它的哪个项被选中.另外,我需要完全点击CheckBox
.在List11示例中,我只需要单击项目行.
那么我需要做什么才能为每行创建一个包含自定义视图的多选列表?非常感谢.
Fer*_*ego 17
你必须创建自己的RelativeLayout
实现Checkable
接口并引用CheckBox
或CheckedTextView
(或如果它是多选模式的列表).
看看这篇文章:http: //www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/
如果您希望根据模型数据检查某些行,那么Rahul Garg的答案在第一次加载列表时是好的,但之后您必须自己处理检查/取消选中事件.
您可以覆盖onListItemCLick()
的ListActivity
检查/取消选中的行
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ViewGroup row = (ViewGroup)v;
CheckBox check = (CheckBox) row.findViewById(R.id.checkbox);
check.toggle();
}
Run Code Online (Sandbox Code Playgroud)
如果你这样做,不要设置ListView
为CHOICE_MODE_MULTIPLE
,因为它在调用函数时会出现奇怪的事情.
要检索检查行的名单,你必须自己实现的方法,呼吁getCheckItemIds()
对ListView
不工作:
ListView l = getListView();
int count = l.getCount();
for(int i=0; i<count; ++i) {
ViewGroup row = (ViewGroup)l.getChildAt(i);
CheckBox check = (Checked) row.findViewById(R.id.ck1);
if( check.isChecked() ) {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
每个这样的视图都有一个TextView和一个CheckBox.
不,它没有.它有一个CheckedTextView
.
那么我需要做什么才能为每行创建一个包含自定义视图的多选列表?
尝试将CheckBox
android:id
值设为是"@android:id/text1"
,看看是否有帮助.即对于所使用的由Android的ID CheckedTextView
在simple_list_item_multiple_choice
.
解决方案是创建实现Clickable接口的自定义View.
public class OneLineCheckableListItem extends LinearLayout implements Checkable {
public OneLineCheckableListItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
private boolean checked;
@Override
public boolean isChecked() {
return checked;
}
@Override
public void setChecked(boolean checked) {
this.checked = checked;
ImageView iv = (ImageView) findViewById(R.id.SelectImageView);
iv.setImageResource(checked ? R.drawable.button_up : R.drawable.button_down);
}
@Override
public void toggle() {
this.checked = !this.checked;
}
}
Run Code Online (Sandbox Code Playgroud)
并使用新窗口小部件为列表项创建自定义布局.
<?xml version="1.0" encoding="utf-8"?>
<ax.wordster.OneLineCheckableListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:background="@drawable/selector_listitem"
android:orientation="horizontal" >
<ImageView
android:id="@+id/SelectImageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/button_friends_down" />
<TextView
android:id="@+id/ItemTextView"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="@string/___"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/text_item" />
</ax.wordster.OneLineCheckableListItem>
Run Code Online (Sandbox Code Playgroud)
然后使用上面的布局创建一个新的自定义适配器.
归档时间: |
|
查看次数: |
48845 次 |
最近记录: |