2

我正在尝试在ListView不使用上下文操作栏的情况下实现多项选择CheckBox。当我使用ArrayAdapterwithCHOICE_MODE_MULTIPLE_MODAL时没有问题,但是当我切换到扩展的 CustomAdapter 时ArrayAdapter,似乎长按它什么也没做。

public class MainActivity extends Activity implements PullToRefreshAttacher.OnRefreshListener {

    private PullToRefreshAttacher mPullToRefreshAttacher;

    private static final int SIMULATED_REFRESH_LENGHT = 5000;

    private ArrayList<String> mItems = new ArrayList<String>();
    private SelectionAdapter mAdapter;

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

        for (int i = 0; i < 24; i++) {
            mItems.add("Name" + i);
        }

        mAdapter = new SelectionAdapter(this, R.layout.single_item, R.id.item, mItems);
        final ListView listView = (ListView) findViewById(R.id.list);
        listView.setLongClickable(true);
        listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                listView.setItemChecked(i, !mAdapter.isPositionChecked(i));
            }
        });
        listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

            private int nr = 0;

            @Override
            public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
                if (b)
                    nr++;
                else
                    nr--;
                actionMode.setTitle(nr + " Selected");
            }

            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                MenuInflater inflater = actionMode.getMenuInflater();
                inflater.inflate(R.menu.main, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                StringBuilder sb = new StringBuilder();
                Set<Integer> positions = mAdapter.getCurrentCheckedPosition();
                for (Integer pos : positions) {
                    sb.append(" " + pos + ",");
                }
                Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
                actionMode.finish();
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {
                mAdapter.clearSelection();
            }
        });

        listView.setAdapter(new SlideExpandableListAdapter(mAdapter, R.id.expandable_toggle_button, R.id.expandable));

        mPullToRefreshAttacher = PullToRefreshAttacher.get(this);
        mPullToRefreshAttacher.addRefreshableView(listView, this);
    }

    @Override
    public void onRefreshStarted(View view) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(SIMULATED_REFRESH_LENGHT);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                mPullToRefreshAttacher.setRefreshComplete();
            }
        }.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private class SelectionAdapter extends ArrayAdapter<String> {

        private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();

        public SelectionAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
            super(context, resource, textViewResourceId, objects);
        }

        public void setNewSelection(int position, boolean value) {
            mSelection.put(position, value);
            notifyDataSetChanged();
        }

        public boolean isPositionChecked(int position) {
            Boolean result = mSelection.get(position);
            return result == null ? false : result;
        }

        public Set<Integer> getCurrentCheckedPosition() {
            return mSelection.keySet();
        }

        public void removeSelection(int position) {
            mSelection.remove(position);
            notifyDataSetChanged();
        }

        public void clearSelection() {
            mSelection = new HashMap<Integer, Boolean>();
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            v.setBackground(getResources().getDrawable(R.drawable.card_bg_r4));
            if (mSelection.get(position) != null) {
                v.setBackground(getResources().getDrawable(R.drawable.card_bg_r4));
            }
            return v;
        }

    }

}

这是行的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="@string/hello_world"
          android:id="@+id/item" />
    <Button
        android:id="@+id/expandable_toggle_button"
        android:text="More"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignBottom="@+id/item"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/item"/>
</RelativeLayout>

<LinearLayout android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal"
              android:id="@+id/expandable"
              android:background="#000000">

    <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="Action A" />

    <Button
            android:id="@+id/details"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="Action B"/>
</LinearLayout>
</LinearLayout>
4

1 回答 1

0

您将要添加android:background="?androind:activatedBackgroundIndicator"到您的 parent LinearLayout。这将在选择时产生突出显示的背景。

于 2013-08-14T18:54:57.517 回答