[英]Clickable ListView
我現在正在尋找幾天以尋求ListView中可點擊項的解決方案。
首先,我遇到了這個問題: developer.android.com/resources/articles/touch-mode.html ,發現它沒有“正常”的onListItemClick()行為。
然后我遇到了這段代碼 : http : //www.androidsnippets.org/snippets/125/
// LINE 296-321
@Override
protected ViewHolder createHolder(View v) {
// createHolder will be called only as long, as the ListView is not filled
// entirely. That is, where we gain our performance:
// We use the relatively costly findViewById() methods and
// bind the view's reference to the holder objects.
TextView text = (TextView) v.findViewById(R.id.listitem_text);
ImageView icon = (ImageView) v.findViewById(R.id.listitem_icon);
ViewHolder mvh = new MyViewHolder(text, icon);
// Additionally, we make some icons clickable
// Mind, that item becomes clickable, when adding a click listener (see API)
// so, it is not necessary to use the android:clickable attribute in XML
icon.setOnClickListener(new ClickableListAdapter.OnClickListener(mvh) {
public void onClick(View v, ViewHolder viewHolder) {
// we toggle the enabled state and also switch the icon
MyViewHolder mvh = (MyViewHolder) viewHolder;
MyData mo = (MyData) mvh.data;
mo.enable = !mo.enable; // toggle
ImageView icon = (ImageView) v;
icon.setImageBitmap(
mo.enable ? ClickableListItemActivity.this.mIconEnabled
: ClickableListItemActivity.this.mIconDisabled);
}
});
在調試時,我注意到參數View v是TextView而不是“普通”視圖,然后當然:
TextView text = (TextView) v.findViewById(R.id.listitem_text);
返回null ,我得到一個NullPointerException ...
有什么想法嗎? 我該如何解決呢?
提前致謝! :)
如何創建ClickableListAdapter
實例?
創建列表適配器時,必須傳遞資源ID viewId
,這應該是一個layout
,稍后將進行誇大。
public ClickableListAdapter(Context context, int viewid, List objects) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
mDataObjects = objects;
mViewId = viewid;
下面,代碼對傳遞給構造函數的xml布局進行膨脹,並調用createHolder
。
view = mInflater.inflate(mViewId, null);
// call the user's implementation
holder = createHolder(view);
因此,請確保在實例化ClickableListAdapter
,傳遞layout
而不是id
編輯您必須使用以下內容創建一個xml布局,該布局取自提供的鏈接:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<TextView android:text="Text" android:id="@+id/listitem_text"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></TextView>
<ImageView android:id="@+id/listitem_icon"
android:src="@drawable/globe2_32x32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="32px"
android:maxHeight="32px"
>
</ImageView>
</LinearLayout>
如果在布局目錄mylistrow.xml
其mylistrow.xml
,則將適配器構造為:
adapter = new MyClickableChannelListAdapter(this, R.layout.mylistrow, channelList);
setListAdapter(adapter);
列表項應立即可用。 您可以通過查看ApiDemos
項目代碼來檢查列表的編碼方式。 它應該存在於您的本地計算機上,因為它是SDK的一部分。 我在<root_sdk_folder>\\platforms\\android-2.0.1\\samples\\ApiDemos
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.