Search in sources :

Example 1 with IMultiSelectItem

use of xyz.zpayh.adapter.IMultiSelectItem in project SherlockAdapter by EvilBT.

the class MultiSelectItemActivity method onCreate.

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_multi_select_item);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            getSelectedList();
        }
    });
    findViewById(R.id.selectAll).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // 全选
            adapter.selectAll();
        }
    });
    findViewById(R.id.unselectAll).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // 全不选
            adapter.clearSelectAll();
        }
    });
    adapter = new MyMultiSelectAdapter();
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
    recyclerView.setAdapter(adapter);
    // 屏障默认的Change动画,默认的Change动画会闪烁一下
    ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    adapter.setOnItemCheckedChangeListener(new OnItemCheckedChangeListener() {

        @Override
        public void onItemCheck(@NonNull View view, boolean isChecked, int adapterPosition) {
            final int id = view.getId();
            if (id == R.id.checkbox) {
                IMultiSelectItem selectorItem = adapter.getData(adapterPosition);
                if (selectorItem != null) {
                    final int selectedSize = adapter.getSelectedItems().size();
                    if (isChecked && selectedSize > 9) {
                        Toast.makeText(MultiSelectItemActivity.this, "您最多只能选择9个", Toast.LENGTH_SHORT).show();
                        selectorItem.setChecked(false);
                        adapter.notifyItemChanged(adapterPosition);
                    }
                }
            }
        }
    });
    List<IMultiSelectItem> data = new ArrayList<>(20);
    for (int i = 0; i < 40; i++) {
        SelectorCard card = new SelectorCard(i);
        data.add(card);
    }
    // 添加一个不可选中的数据在列表上
    NoSelectCard card = new NoSelectCard("我跟他们不一样");
    data.add(0, card);
    adapter.setData(data);
}
Also used : SimpleItemAnimator(android.support.v7.widget.SimpleItemAnimator) ArrayList(java.util.ArrayList) SelectorCard(xyz.zpayh.myadapter.data.SelectorCard) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) NoSelectCard(xyz.zpayh.myadapter.data.NoSelectCard) OnItemCheckedChangeListener(xyz.zpayh.adapter.OnItemCheckedChangeListener) MyMultiSelectAdapter(xyz.zpayh.myadapter.adapter.MyMultiSelectAdapter) FloatingActionButton(android.support.design.widget.FloatingActionButton) RecyclerView(android.support.v7.widget.RecyclerView) Toolbar(android.support.v7.widget.Toolbar) IMultiSelectItem(xyz.zpayh.adapter.IMultiSelectItem)

Aggregations

FloatingActionButton (android.support.design.widget.FloatingActionButton)1 RecyclerView (android.support.v7.widget.RecyclerView)1 SimpleItemAnimator (android.support.v7.widget.SimpleItemAnimator)1 StaggeredGridLayoutManager (android.support.v7.widget.StaggeredGridLayoutManager)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 ArrayList (java.util.ArrayList)1 IMultiSelectItem (xyz.zpayh.adapter.IMultiSelectItem)1 OnItemCheckedChangeListener (xyz.zpayh.adapter.OnItemCheckedChangeListener)1 MyMultiSelectAdapter (xyz.zpayh.myadapter.adapter.MyMultiSelectAdapter)1 NoSelectCard (xyz.zpayh.myadapter.data.NoSelectCard)1 SelectorCard (xyz.zpayh.myadapter.data.SelectorCard)1