Search in sources :

Example 1 with DefaultItemDecoration

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

the class StaggeredActivity 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 StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
    mRecyclerView.addItemDecoration(new DefaultItemDecoration(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(StaggeredActivity.this, message, Toast.LENGTH_LONG).show();
            }
        }
    });
    mAdapter = new ExpandedAdapter(this);
    mRecyclerView.setAdapter(mAdapter);
    refresh();
}
Also used : DefaultItemDecoration(com.yanzhenjie.recyclerview.widget.DefaultItemDecoration) OnItemClickListener(com.yanzhenjie.recyclerview.OnItemClickListener) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) 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)

Example 2 with DefaultItemDecoration

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

the class MenuFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    mRecyclerView.addItemDecoration(new DefaultItemDecoration(ContextCompat.getColor(getContext(), R.color.divider_color)));
    mRecyclerView.setOnItemClickListener(mItemClickListener);
    mRecyclerView.setSwipeMenuCreator(mSwipeMenuCreator);
    mRecyclerView.setOnItemMenuClickListener(mItemMenuClickListener);
    MainAdapter menuAdapter = new MainAdapter(getContext());
    mRecyclerView.setAdapter(menuAdapter);
    List<String> dataList = new ArrayList<>();
    for (int i = 0; i < 30; i++) {
        dataList.add("我是第" + i + "个。");
    }
    menuAdapter.notifyDataSetChanged(dataList);
}
Also used : MainAdapter(com.yanzhenjie.recyclerview.sample.adapter.MainAdapter) DefaultItemDecoration(com.yanzhenjie.recyclerview.widget.DefaultItemDecoration) ArrayList(java.util.ArrayList) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Example 3 with DefaultItemDecoration

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

the class ListActivity 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 LinearLayoutManager(this));
    mRecyclerView.addItemDecoration(new DefaultItemDecoration(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
                final 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(ListActivity.this, message, Toast.LENGTH_LONG).show();
            }
        }
    });
    mAdapter = new ExpandedAdapter(this);
    mRecyclerView.setAdapter(mAdapter);
    refresh();
}
Also used : DefaultItemDecoration(com.yanzhenjie.recyclerview.widget.DefaultItemDecoration) OnItemClickListener(com.yanzhenjie.recyclerview.OnItemClickListener) ExpandedAdapter(com.yanzhenjie.recyclerview.sample.adapter.ExpandedAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SwipeRecyclerView(com.yanzhenjie.recyclerview.SwipeRecyclerView) View(android.view.View) ActionBar(androidx.appcompat.app.ActionBar) Toolbar(androidx.appcompat.widget.Toolbar)

Example 4 with DefaultItemDecoration

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

the class DefaultActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_refresh_loadmore);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    assert actionBar != null;
    actionBar.setDisplayHomeAsUpEnabled(true);
    mRefreshLayout = findViewById(R.id.refresh_layout);
    // 刷新监听。
    mRefreshLayout.setOnRefreshListener(mRefreshListener);
    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    mRecyclerView.addItemDecoration(new DefaultItemDecoration(ContextCompat.getColor(this, R.color.divider_color)));
    // RecyclerView Item点击监听。
    mRecyclerView.setOnItemClickListener(mItemClickListener);
    // 使用默认的加载更多的View。
    mRecyclerView.useDefaultLoadMore();
    // 加载更多的监听。
    mRecyclerView.setLoadMoreListener(mLoadMoreListener);
    mAdapter = new MainAdapter(this);
    mRecyclerView.setAdapter(mAdapter);
    // 请求服务器加载数据。
    loadData();
}
Also used : MainAdapter(com.yanzhenjie.recyclerview.sample.adapter.MainAdapter) GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) DefaultItemDecoration(com.yanzhenjie.recyclerview.widget.DefaultItemDecoration) ActionBar(androidx.appcompat.app.ActionBar) Toolbar(androidx.appcompat.widget.Toolbar)

Example 5 with DefaultItemDecoration

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

the class MenuActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_group_menu);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    assert actionBar != null;
    actionBar.setDisplayHomeAsUpEnabled(true);
    SwipeRecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.addItemDecoration(new DefaultItemDecoration(ContextCompat.getColor(this, R.color.divider_color)));
    recyclerView.setSwipeMenuCreator(mSwipeMenuCreator);
    mAdapter = new GroupAdapter();
    recyclerView.setAdapter(mAdapter);
    mAdapter.setListItems(createDataList());
}
Also used : DefaultItemDecoration(com.yanzhenjie.recyclerview.widget.DefaultItemDecoration) SwipeRecyclerView(com.yanzhenjie.recyclerview.SwipeRecyclerView) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ActionBar(androidx.appcompat.app.ActionBar) Toolbar(androidx.appcompat.widget.Toolbar)

Aggregations

DefaultItemDecoration (com.yanzhenjie.recyclerview.widget.DefaultItemDecoration)7 ActionBar (androidx.appcompat.app.ActionBar)6 Toolbar (androidx.appcompat.widget.Toolbar)6 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)4 SwipeRecyclerView (com.yanzhenjie.recyclerview.SwipeRecyclerView)4 MainAdapter (com.yanzhenjie.recyclerview.sample.adapter.MainAdapter)4 View (android.view.View)3 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)2 OnItemClickListener (com.yanzhenjie.recyclerview.OnItemClickListener)2 ExpandedAdapter (com.yanzhenjie.recyclerview.sample.adapter.ExpandedAdapter)2 StaggeredGridLayoutManager (androidx.recyclerview.widget.StaggeredGridLayoutManager)1 ArrayList (java.util.ArrayList)1