Search in sources :

Example 1 with IMultiItem

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

the class ExpandableActivity method refreshData.

private void refreshData() {
    List<IMultiItem> oldData = mAdapter.getData();
    List<IMultiItem> data = new ArrayList<>();
    data.addAll(oldData);
    Collections.reverse(data);
    DefaultExpandable expandable = (DefaultExpandable) data.get(1);
    Collections.reverse(expandable.getSubItems());
    expandable = (DefaultExpandable) data.get(2);
    Collections.reverse(expandable.getSubItems());
    mAdapter.setData(data);
}
Also used : ArrayList(java.util.ArrayList) DefaultExpandable(xyz.zpayh.adapter.DefaultExpandable) IMultiItem(xyz.zpayh.adapter.IMultiItem)

Example 2 with IMultiItem

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

the class ExpandableActivity method initData.

private void initData() {
    int width = getResources().getDisplayMetrics().widthPixels / mStaggeredGridLayoutManager.getSpanCount();
    List<IMultiItem> data = new ArrayList<>();
    for (int i = 0; i < mLabels.length; i++) {
        ImageLabel label = new ImageLabel(mLabels[i]);
        data.add(label);
    }
    // Add animals
    List<IMultiItem> animalData = new ArrayList<>(2);
    for (int i = 0; i < mAnimalIds.length; i++) {
        ImageCard cardData = new ImageCard(mAnimalIds[i], mAnimalTitles[i]);
        Card card = new Card(width, cardData);
        animalData.add(card);
    }
    ((DefaultExpandable) data.get(0)).addSubData(animalData);
    // Add beauties
    List<IMultiItem> beautyData = new ArrayList<>(mBeautyIds.length);
    for (int i = 0; i < mBeautyIds.length; i++) {
        ImageCard cardData = new ImageCard(mBeautyIds[i], mBeautyTitles[i]);
        Card card = new Card(width, cardData);
        beautyData.add(card);
    }
    ((DefaultExpandable) data.get(1)).addSubData(beautyData);
    // Add scenery
    List<IMultiItem> sceneryData = new ArrayList<>(mSceneryIds.length);
    for (int i = 0; i < mSceneryIds.length; i++) {
        ImageCard cardData = new ImageCard(mSceneryIds[i], mSceneryTitles[i]);
        Card card = new Card(width, cardData);
        sceneryData.add(card);
    }
    ((DefaultExpandable) data.get(2)).addSubData(sceneryData);
    // Add tranquil
    List<IMultiItem> tranquilData = new ArrayList<>(mTranquilIds.length);
    for (int i = 0; i < mTranquilIds.length; i++) {
        ImageCard cardData = new ImageCard(mTranquilIds[i], mTranquilTitles[i]);
        Card card = new Card(width, cardData);
        tranquilData.add(card);
    }
    ((DefaultExpandable) data.get(3)).addSubData(tranquilData);
    mAdapter.setData(data);
}
Also used : ImageLabel(xyz.zpayh.myadapter.data.ImageLabel) ArrayList(java.util.ArrayList) ImageCard(xyz.zpayh.myadapter.data.ImageCard) DefaultExpandable(xyz.zpayh.adapter.DefaultExpandable) IMultiItem(xyz.zpayh.adapter.IMultiItem) Card(xyz.zpayh.myadapter.data.Card) ImageCard(xyz.zpayh.myadapter.data.ImageCard)

Example 3 with IMultiItem

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

the class ExpandableActivity method initView.

private void initView() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mRecyclerView = (RecyclerView) findViewById(R.id.list);
    mGridLayoutManager = new GridLayoutManager(this, 2);
    mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
    mAdapter = new MyExpandableAdapter();
    // mAdapter.setAlwaysShowFoot(true);
    // mAdapter.setAlwaysShowHead(true);
    mAdapter.addHeadLayout(R.layout.item_head);
    mAdapter.addFootLayout(R.layout.item_foot2);
    // 设置DiffUtil效果
    mAdapter.setCallback(new DiffUtilCallback<IMultiItem>() {

        @Override
        public boolean areItemsTheSame(IMultiItem oldItem, IMultiItem newItem) {
            if (oldItem instanceof ImageLabel && newItem instanceof ImageLabel) {
                return TextUtils.equals(((ImageLabel) oldItem).getData(), ((ImageLabel) newItem).getData());
            }
            if (oldItem instanceof Card && newItem instanceof Card) {
                return ((Card) oldItem).getData().mImageResId == ((Card) newItem).getData().mImageResId;
            }
            return false;
        }

        @Override
        public boolean areContentsTheSame(IMultiItem oldItem, IMultiItem newItem) {
            if (oldItem instanceof ImageLabel && newItem instanceof ImageLabel) {
                return true;
            }
            if (oldItem instanceof Card && newItem instanceof Card) {
                return TextUtils.equals(((Card) oldItem).getData().mImageTitle, ((Card) newItem).getData().mImageTitle);
            }
            return false;
        }
    });
    mRecyclerView.setAdapter(mAdapter);
    // 屏障默认的Change动画
    ((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    // 点击展开/折叠事件
    mAdapter.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(@NonNull View view, int adapterPosition) {
            if (view.getId() == R.id.label_root) {
                IMultiItem item = mAdapter.getData(adapterPosition);
                if (item instanceof IExpandable) {
                    IExpandable expandable = (IExpandable) item;
                    if (expandable.isExpandable()) {
                        // 修复issue#2
                        // 这个支持包的bug: https://issuetracker.google.com/issues/37034096
                        mStaggeredGridLayoutManager.invalidateSpanAssignments();
                        mAdapter.collapseAll(adapterPosition);
                    } else {
                        mAdapter.expandAll(adapterPosition);
                    }
                }
            }
        }
    });
    mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(@NonNull View view, int adapterPosition) {
            mAdapter.removeData(adapterPosition);
            return true;
        }
    });
    final SwipeRefreshLayout refresh = (SwipeRefreshLayout) findViewById(R.id.refresh);
    refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            refreshData();
            refresh.setRefreshing(false);
        }
    });
    findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            refreshData();
        }
    });
    initData();
}
Also used : SimpleItemAnimator(android.support.v7.widget.SimpleItemAnimator) OnItemClickListener(xyz.zpayh.adapter.OnItemClickListener) ImageLabel(xyz.zpayh.myadapter.data.ImageLabel) IExpandable(xyz.zpayh.adapter.IExpandable) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) IMultiItem(xyz.zpayh.adapter.IMultiItem) OnItemLongClickListener(xyz.zpayh.adapter.OnItemLongClickListener) Card(xyz.zpayh.myadapter.data.Card) ImageCard(xyz.zpayh.myadapter.data.ImageCard) GridLayoutManager(android.support.v7.widget.GridLayoutManager) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) MyExpandableAdapter(xyz.zpayh.myadapter.adapter.MyExpandableAdapter) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

IMultiItem (xyz.zpayh.adapter.IMultiItem)3 ArrayList (java.util.ArrayList)2 DefaultExpandable (xyz.zpayh.adapter.DefaultExpandable)2 Card (xyz.zpayh.myadapter.data.Card)2 ImageCard (xyz.zpayh.myadapter.data.ImageCard)2 ImageLabel (xyz.zpayh.myadapter.data.ImageLabel)2 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)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 IExpandable (xyz.zpayh.adapter.IExpandable)1 OnItemClickListener (xyz.zpayh.adapter.OnItemClickListener)1 OnItemLongClickListener (xyz.zpayh.adapter.OnItemLongClickListener)1 MyExpandableAdapter (xyz.zpayh.myadapter.adapter.MyExpandableAdapter)1