Search in sources :

Example 1 with BorderItemDecoration

use of com.yanzhenjie.recyclerview.widget.BorderItemDecoration in project SwipeRecyclerView by yanzhenjie.

the class GridActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scroll);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    assert actionBar != null;
    actionBar.setDisplayHomeAsUpEnabled(true);
    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
    mRecyclerView.addItemDecoration(new BorderItemDecoration(ContextCompat.getColor(this, R.color.divider_color)));
    mRecyclerView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(View itemView, int position) {
            // 根据原position判断该item是否是parent item
            if (mAdapter.isParentItem(position)) {
                // 换取parent position
                int parentPosition = mAdapter.parentItemPosition(position);
                // 判断parent是否打开了二级菜单
                if (mAdapter.isExpanded(parentPosition)) {
                    mDataList.get(parentPosition).setExpanded(false);
                    mAdapter.notifyParentChanged(parentPosition);
                    // 关闭该parent下的二级菜单
                    mAdapter.collapseParent(parentPosition);
                } else {
                    mDataList.get(parentPosition).setExpanded(true);
                    mAdapter.notifyParentChanged(parentPosition);
                    // 打开该parent下的二级菜单
                    mAdapter.expandParent(parentPosition);
                }
            } else {
                // 换取parent position
                int parentPosition = mAdapter.parentItemPosition(position);
                // 换取child position
                int childPosition = mAdapter.childItemPosition(position);
                String message = String.format("我是%1$d爸爸的%2$d儿子", parentPosition, childPosition);
                Toast.makeText(GridActivity.this, message, Toast.LENGTH_LONG).show();
            }
        }
    });
    mAdapter = new ExpandedAdapter(this);
    mRecyclerView.setAdapter(mAdapter);
    refresh();
}
Also used : GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) OnItemClickListener(com.yanzhenjie.recyclerview.OnItemClickListener) ExpandedAdapter(com.yanzhenjie.recyclerview.sample.adapter.ExpandedAdapter) SwipeRecyclerView(com.yanzhenjie.recyclerview.SwipeRecyclerView) View(android.view.View) ActionBar(androidx.appcompat.app.ActionBar) Toolbar(androidx.appcompat.widget.Toolbar) BorderItemDecoration(com.yanzhenjie.recyclerview.widget.BorderItemDecoration)

Aggregations

View (android.view.View)1 ActionBar (androidx.appcompat.app.ActionBar)1 Toolbar (androidx.appcompat.widget.Toolbar)1 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)1 OnItemClickListener (com.yanzhenjie.recyclerview.OnItemClickListener)1 SwipeRecyclerView (com.yanzhenjie.recyclerview.SwipeRecyclerView)1 ExpandedAdapter (com.yanzhenjie.recyclerview.sample.adapter.ExpandedAdapter)1 BorderItemDecoration (com.yanzhenjie.recyclerview.widget.BorderItemDecoration)1