自定义android listview项目在整个区域无法选择

时间:2011-07-18 07:58:30

标签: android listview layout adapter

我使用带有自定义适配器的ListView将项目显示为标题/副标题TextViews对。

不幸的是,我可以通过点击它的上半部分来选择一个项目,即标题占用的项目

以下是我正在使用的代码:

journal_list.xml

<?xml version="1.0" encoding="utf-8"?>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:background="#fff"
    android:cacheColorHint="#fff"
    android:paddingBottom="6dp"
/>

列表项布局的journal_list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:descendantFocusability="blocksDescendants"
          >
<TextView
    android:id="@+id/journal_entry_date"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textColor="#000"
    android:textSize="18sp"

    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
/>
<TextView
    android:id="@+id/journal_content"
    android:paddingLeft="28dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="16sp"
    android:textColor="#555"
    android:inputType="textMultiLine"
    android:maxLines="2"
    android:minLines="1"
    android:layout_below="@id/journal_entry_date"
    android:layout_alignParentLeft="true"
/>

适配器的代码:

private class JournalAdapter extends ArrayAdapter<JournalEntry> {
    Context ctxt;
    public JournalAdapter(Context ctxt) {
        super(ctxt, R.layout.journal_list_item);
        this.ctxt = ctxt;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        JournalHolder holder;
        if (row == null) {
            row = ((LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).
                    inflate(R.layout.journal_list_item, parent, false);
            holder = new JournalHolder(row);
            row.setTag(holder);
        } else {
            holder = (JournalHolder) row.getTag();
        }
        JournalEntry crt = getItem(position);
        holder.getDate().setText(crt.dateWritten);
        holder.getContent().setText(crt.content);

        return row;
    }
}

private static class JournalHolder {
    private TextView date;
    private TextView content;
    private View base;

    public JournalHolder(View base) {
        this.base = base;
    }
    public TextView getDate() {
        if (date == null)
            date = (TextView) base.findViewById(R.id.journal_entry_date);
        return date;
    }
    public TextView getContent() {
        if (content == null)
            content = (TextView) base.findViewById(R.id.journal_content);
        return content;
    }
}

ListActivity的onCreate()方法代码:

private JournalAdapter adapter;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.journal_list);
    adapter = new JournalAdapter(this);
    setListAdapter(adapter);
    registerForContextMenu(getListView());
}

另外,我在onResume()

中调用一个名为updateList()的方法
private void updateList() {
    adapter.clear();
    Cursor cursor = helper.listJournal(start, end);
    cursor.moveToFirst();
    JournalEntry crt;
    while (!cursor.isAfterLast()) {
        crt = new JournalEntry();
        crt.dateWritten = cursor.getString(cursor.getColumnIndex("date_written"));
        crt.content = cursor.getString(cursor.getColumnIndex("content"));
        crt.id = cursor.getInt(cursor.getColumnIndex("entry_id"));
        adapter.add(crt);
        cursor.moveToNext();
    }
    cursor.close();
    adapter.notifyDataSetChanged();
}

单击列表项在我的ListActivity的onListItemClick中处理,如下所示:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Intent intent = new Intent(this, JournalEditor.class);
    intent.setAction(Intent.ACTION_EDIT);
    intent.putExtra("entry_id", adapter.getItem(position).id);
    startActivity(intent);
}

显然,我忘记了重要的事情,这就是为什么会发生这种奇怪的事情。

我在这里找到了类似的讨论:http://groups.google.com/group/android-developers/browse_thread/thread/5636c8ea74033657但是它没有用处。

1 个答案:

答案 0 :(得分:5)

问题可能来自单击textviews。将listview项目的视图设置为可单击时,它们会覆盖listview项目的单击,并且单击仅在单击项目的特定视图时才起作用。

如果是这种情况,请在getView()函数中对两个textview执行以下操作:

TextView tv;
tv.setFocusable(false);
tv.setClickable(false);

然后将listview设置为clickable:listView1.setClickable(true);