Listview - 单选模式在某些设备上不起作用

时间:2014-11-10 09:44:36

标签: android android-layout

我无法在ListView中实现单选模式。我花了差不多两天时间来解决这个问题。

问题是当我点击checkBox时它不会检查 - 它只是闪烁,但在某些设备上它可以工作。

下面的代码适用于三星galaxy s3(android 4.1.2),Vodafone 975N(android 4.1.1)和其他一些,但不适用于联想P770(4.1.2)和三星扬(android 4.4.2) )。 有人可以告诉我哪里可能是错误吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_calendars_for_locks_of_group);
    ...

    final ListView listView = getListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    LayoutInflater inflater = getLayoutInflater();
    CheckedTextView check = (CheckedTextView) inflater.inflate(
            R.layout.list_item_with_radiobutton, null).findViewById(
            R.id.listToggleableRadioButton);
    check.setText(getResources().getString(R.string.None));
    check.setTag(0L);

    listView.addHeaderView(check);

    reloadData()
    ...

    listView.setItemChecked(positionOfAlreadySelectedCalendar, true);

}

private void reloadData() {
    calendarsInLockCursor = db.getCalendarIdsForLockCursor(lockId);
    Cursor calendarsCursor = db
            .getCalendarsCursor(Database.CALENDAR_TYPE_ACCESS);

    RadioButtonSimpleCursorLocksAdapter adapter = new RadioButtonSimpleCursorLocksAdapter(
            this, android.R.layout.simple_list_item_single_choice,
            calendarsCursor, calendarsInLockCursor,
            new String[] { Database.COL_CALENDARS_NAME },
            new int[] { android.R.id.text1 }, 0);

    setListAdapter(adapter);
    positionOfAlreadySelectedCalendar = getPositionInCursor(
            calendarsCursor, calendarId) + 1;
}

public static class RadioButtonSimpleCursorLocksAdapter extends
        SimpleCursorAdapter {

    OnClickListener onClickListener = null;
    private Cursor calendarsInLock;

    public RadioButtonSimpleCursorLocksAdapter(Context context, int layout,
            Cursor calendarsTable, Cursor calendarsInLock, String[] from,
            int[] to, int flags) {
        super(context, layout, calendarsTable, from, to, flags);
        this.calendarsInLock = calendarsInLock;

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.list_item_with_radiobutton,
                parent, false).findViewById(R.id.listToggleableRadioButton);

        return view;
    }

    public void bindView(View view, Context context, Cursor c) {

        CheckedTextView check = (CheckedTextView) view
                .findViewById(R.id.listToggleableRadioButton);


        Long calId = c.getLong(c.getColumnIndex(Database.COL_CALENDARS_ID));

        check.setText(c.getString(c
                .getColumnIndex(Database.COL_CALENDARS_NAME)));
        check.setTag(calId);

    }


    public boolean isCalendarInLock(long calendarID) {
        return SelectCalendarForLockOfGroupActivity.isCalendarInLock(
                calendarsInLock, calendarID);
    }

}

在R.layout.list_item_with_radiobutton:

    <?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:binding="http://www.gueei.com/android-binding/"
android:id="@+id/listToggleableRadioButton"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/listItemMain"
binding:text="." />

1 个答案:

答案 0 :(得分:1)

缺陷在其他地方。在样式中,已重写检查TextViewStyle。我不得不删除clickable = true属性。天哪,真是一场斗争

    <style name="checkTextViewStyle" parent="@android:style/Widget">
    <item name="android:textAlignment">viewStart</item>
    <item name="android:paddingBottom">@dimen/activity_vertical_margin</item>
    <item name="android:paddingLeft">@dimen/activity_horizontal_margin</item>
    <item name="android:paddingRight">@dimen/activity_horizontal_margin</item>
    <item name="android:paddingTop">@dimen/activity_vertical_margin</item>
    <item name="android:gravity">left</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:clickable">true</item>  <- !!!This caused the problem!!! 
    </style>