繁体   English   中英

自定义 android 列表视图项在整个区域上不可选

[英]custom android listview item not selectable on the whole area

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

不幸的是,我可以 select 一个项目只需点击它的上半部分,被标题占据的那个

这是我正在使用的代码:

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但它不是很有帮助。

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

如果是这种情况,请对 getView() function 中的两个文本视图执行以下操作:

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

然后将listview设置为可点击: listView1.setClickable(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM