开发者

custom android listview item not selectable on the whole area

I am using a ListView with a custom adapter for displaying items as pairs of title/subtitle TextViews.

Unfortunately, I can select an item just by clicking on its upper half, the one occupied by the title

Here is the code I am using:

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 for the list item's layout

<?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"
/>

Also the code for the adapter:

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.j开发者_如何学Pythonournal_content);
        return content;
    }
}

Code from the ListActivity's onCreate() method:

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());
}

Also, I am calling a method called updateList() in onResume()

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();
}

List item clicking is handled in the onListItemClick of my ListActivity, as follows:

@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);
}

Apparently, I am forgetting something important, and that's why this whole weird thing happens.

I have found a similar discussion here: http://groups.google.com/group/android-developers/browse_thread/thread/5636c8ea74033657 but it wasn't very helpful.


The problem may be from the clicking of the textviews. When you set the views of the listview items as clickable, they override the click of the listview item and the click will only work when the specific view of the item is clicked.

If this is the case, do the following for your two textviews in the getView() function:

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

Then set the listview to clickable: listView1.setClickable(true);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜