自定义列表项不响应选择器中的state_checked

Gro*_*ppe 13 android listview android-adapter

我首先要说的是,我已经详细阅读了所有关于自定义可检查列表项和选择器的问题.他们中的许多人都有类似的问题,但没有一个答案可以解决我的问题.

在我的应用程序的某一点,我提出了一个自定义列表活动.创建时,它从调用它的intent中检索一组静态数据,并将该数据传递给它的自定义数组适配器.每个列表项都是一个简单的RelativeLayout,它实现了Checkable接口.默认情况下,如果单击其中一个项目,则会显示一个新活动,其中显示有关所选联系人的详细信息.但是,如果长按单击列表中的项目,则会启动ActionMode.此时单击列表中的项目不会显示详细信息活动,它只是将项目设置为选中.然后,如果用户选择动作模式项之一,则它对所检查的项执行动作.

需要了解的一点是,在两个选择"模式"中,单击列表项会将其设置为选中状态.

我上面描述的所有内容都非常有效.我唯一的问题是,即使使用默认选择器,列表项的背景在设置为选中时也不会突出显示.

我想要做的是,有两个选择器:每个选择模式一个.在第一种情况下,检查项目时背景不会改变,而在第二种情况下,背景不会改变.我尝试过实现自定义选择器,但即使在那些state_checked中也会被忽略!选择器的其他部分工作正常,但不是state_checked.

我对CheckableListItem的实现结合了很多不同的例子,所以如果我做错了,或者有更好的方法让我知道!

注意:有趣的一点是,如果我将results_list_item.xml中列表项的背景设置为我的选择器,而不是ListView的listSelector属性,则在选中项时更改背景.但是,这样做会导致我的选择器中的长按转换不起作用.

ResultsActivity.java:

public class ResultsActivity extends ListActivity implements OnItemLongClickListener {

    private ListView listView;          // Reference to the list belonging to this activity
    private ActionMode mActionMode;     // Reference to the action mode that can be started
    private boolean selectionMode;      // Detail mode or check mode

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);

        // When the home icon is pressed, go back
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        // Get a reference to the list
        listView = getListView();

        // Initially in detail mode
        selectionMode = true;

        // Get the contacts from the intent data and pass them to the contact adapter
        @SuppressWarnings("unchecked")
        ArrayList<Contact> results = ((ArrayList<Contact>)getIntent().getSerializableExtra("results"));
        Contact[] contacts = new Contact[results.size()];
        ContactArrayAdapter adapter = new ContactArrayAdapter(this, results.toArray(contacts));
        setListAdapter(adapter);

        // We will decide what happens when an item is long-clicked
        listView.setOnItemLongClickListener(this);
    }

    /**
     * If we are in detail mode, when an item in the list is clicked
     * create an instance of the detail activity and pass it the
     * chosen contact
     */
    public void onListItemClick(ListView l, View v, int position, long id) {
        if (selectionMode) {
            Intent displayContact = new Intent(this, ContactActivity.class);
            displayContact.putExtra("contact", (Contact)l.getAdapter().getItem(position));
            startActivity(displayContact);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * If the home button is pressed, go back to the
     * search activity
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, SearchActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * When an item is long-pressed, switch selection modes 
     * and start the action mode 
     */
    public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long i) {
        if (mActionMode != null)
            return false;

        if (selectionMode) {
            toggleSelectionMode();
            listView.startActionMode(new ListActionMode(this, getListView()));
            return true;
        }
        return false;
    }

    /**
     * Clear the list's checked items and switch selection modes
     */
    public void toggleSelectionMode() {
        listView.clearChoices();
        ((ContactArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
        if (selectionMode) {
            selectionMode = false;
        } else {
            selectionMode = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

activity_results.xml:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="multipleChoice" 
    android:listSelector="@drawable/list_selector" />
Run Code Online (Sandbox Code Playgroud)

list_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/blue_transition" />
    <item android:state_checked="true" android:drawable="@drawable/blue" />
</selector>
Run Code Online (Sandbox Code Playgroud)

TwoLineArrayAdapter:

public abstract class TwoLineArrayAdapter extends ArrayAdapter<Contact> {

    private int mListItemLayoutResId;

    public TwoLineArrayAdapter(Context context, Contact[] results) {
        this(context, R.layout.results_list_item, results);
    }

    public TwoLineArrayAdapter(Context context, int listItemLayoutResourceId, Contact[] results) {
        super(context, listItemLayoutResourceId, results);
        mListItemLayoutResId = listItemLayoutResourceId;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View listItemView = convertView;
        if (convertView == null) {
            listItemView = inflater.inflate(mListItemLayoutResId, parent, false);
        }

        // Get the text views within the layout
        TextView lineOneView = (TextView)listItemView.findViewById(R.id.results_list_item_textview1);
        TextView lineTwoView = (TextView)listItemView.findViewById(R.id.results_list_item_textview2);

        Contact c = (Contact)getItem(position);

        lineOneView.setText(lineOneText(c));
        lineTwoView.setText(lineTwoText(c));

        return listItemView;
    }

    public abstract String lineOneText(Contact c);

    public abstract String lineTwoText(Contact c);

}
Run Code Online (Sandbox Code Playgroud)

ContactArrayAdapter:

public class ContactArrayAdapter extends TwoLineArrayAdapter {

    public ContactArrayAdapter(Context context, Contact[] contacts) {
        super(context, contacts);
    }

    public String lineOneText(Contact c) {
        return (c.getLastName() + ", " + c.getFirstName());
    }

    public String lineTwoText(Contact c) {
        return c.getDepartment();
    }

}
Run Code Online (Sandbox Code Playgroud)

CheckableListItem.java:

public class CheckableListItem extends RelativeLayout implements Checkable {

    private boolean isChecked;
    private List<Checkable> checkableViews;

    public CheckableListItem(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initialise(attrs);
    }

    public CheckableListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialise(attrs);
    }

    public CheckableListItem(Context context, int checkableId) {
        super(context);
        initialise(null);
    }

    private void initialise(AttributeSet attrs) {
        this.isChecked = false;
        this.checkableViews = new ArrayList<Checkable>(5);
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean check) {
        isChecked = check;
        for (Checkable c : checkableViews) {
            c.setChecked(check);
        }
        refreshDrawableState();
    }

    public void toggle() {
        isChecked = !isChecked;
        for (Checkable c : checkableViews) {
            c.toggle();
        }
    }

    private static final int[] CheckedStateSet = {
        android.R.attr.state_checked
    };

    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CheckedStateSet);
        }
        return drawableState;
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            findCheckableChildren(this.getChildAt(i));
        }
    }

    private void findCheckableChildren(View v) {
        if (v instanceof Checkable) {
            this.checkableViews.add((Checkable) v);
        }
        if (v instanceof ViewGroup) {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; i++) {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

results_list_item.xml:

<com.test.mycompany.Widgets.CheckableListItem
    android:id="@+id/results_list_item"
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp" >

    <TextView android:id="@+id/results_list_item_textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textSize="20sp"
        android:textColor="#000000"
        android:focusable="false" />

    <TextView android:id="@+id/results_list_item_textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/results_list_item_textview1"
        android:textSize="16sp"
        android:textColor="@android:color/darker_gray" 
        android:focusable="false" />

</com.test.mycompany.Widgets.CheckableListItem>
Run Code Online (Sandbox Code Playgroud)

Jas*_*son 0

我更改并添加了这些方法,CheckedListItem它对我有用:

@Override
public boolean onTouchEvent( MotionEvent event ) {

    int action = event.getAction() & MotionEvent.ACTION_MASK;
    if ( action == MotionEvent.ACTION_UP ) {
        toggle();
    }

    return true;
}

public void toggle() {

    setChecked( !isChecked() );
}

private static final int[] CheckedStateSet = { android.R.attr.state_checked };
Run Code Online (Sandbox Code Playgroud)

问题似乎是在单击时,您从未处理过切换视图的选中状态。