Search in sources :

Example 6 with GridLayoutManager

use of androidx.recyclerview.widget.GridLayoutManager in project SherlockAdapter by EvilBT.

the class MultiItemActivity method initView.

private void initView() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mGridLayoutManager = new GridLayoutManager(this, 3);
    mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    final SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);
    mRecyclerView = (RecyclerView) findViewById(R.id.list);
    mRecyclerView.setLayoutManager(mGridLayoutManager);
    mIsGrid = true;
    mAdapter = new MyMultiAdapter();
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(@NonNull View view, final int adapterPosition) {
            new AlertDialog.Builder(MultiItemActivity.this).setTitle("是否删除第" + adapterPosition + "项").setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mAdapter.removeData(adapterPosition);
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).create().show();
            return true;
        }
    });
    mData = new ArrayList<>();
    initGridData();
    refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            refreshLayout.postDelayed(new Runnable() {

                @Override
                public void run() {
                    mAdapter.setData(mData);
                    refreshLayout.setRefreshing(false);
                }
            }, 500);
        }
    });
}
Also used : DialogInterface(android.content.DialogInterface) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) OnItemLongClickListener(xyz.zpayh.adapter.OnItemLongClickListener) GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) MyMultiAdapter(xyz.zpayh.myadapter.adapter.MyMultiAdapter) Toolbar(androidx.appcompat.widget.Toolbar)

Example 7 with GridLayoutManager

use of androidx.recyclerview.widget.GridLayoutManager in project SherlockAdapter by EvilBT.

the class BaseAdapter method onAttachedToRecyclerView.

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    mRecyclerView = recyclerView;
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    if (manager == null || !(manager instanceof GridLayoutManager))
        return;
    final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

        @Override
        public int getSpanSize(int position) {
            if (mShowErrorView) {
                if (mAlwaysShowHead && position < getHeadSize()) {
                    // 显示头部
                    if (mHeadLayoutAndSpanStates[position * 3 + 2] > 0) {
                        int spanCount = gridLayoutManager.getSpanCount();
                        return mHeadLayoutAndSpanStates[position * 3 + 2] > spanCount ? spanCount : mHeadLayoutAndSpanStates[position * 3 + 2];
                    }
                    if (mHeadLayoutAndSpanStates[position * 3 + 1] == 0) {
                        return 1;
                    }
                    return gridLayoutManager.getSpanCount();
                }
                if (mAlwaysShowFoot) {
                    int index = position - (mAlwaysShowHead ? (getHeadSize() + 1) : 1);
                    if (index >= 0 && index < getFootSize()) {
                        // 显示尾部
                        if (mFootLayoutAndSpanStates[index * 3 + 2] > 0) {
                            int spanCount = gridLayoutManager.getSpanCount();
                            return mFootLayoutAndSpanStates[index * 3 + 2] > spanCount ? spanCount : mFootLayoutAndSpanStates[index * 3 + 2];
                        }
                        if (mFootLayoutAndSpanStates[index * 3 + 1] == 0) {
                            return 1;
                        }
                        return gridLayoutManager.getSpanCount();
                    }
                }
                // 当数据异常时,Error界面都FullSpan
                return gridLayoutManager.getSpanCount();
            }
            if (mData.isEmpty()) {
                if (mAlwaysShowHead && position < getHeadSize()) {
                    // 显示头部
                    if (mHeadLayoutAndSpanStates[position * 3 + 2] > 0) {
                        int spanCount = gridLayoutManager.getSpanCount();
                        return mHeadLayoutAndSpanStates[position * 3 + 2] > spanCount ? spanCount : mHeadLayoutAndSpanStates[position * 3 + 2];
                    }
                    if (mHeadLayoutAndSpanStates[position * 3 + 1] == 0) {
                        return 1;
                    }
                    return gridLayoutManager.getSpanCount();
                }
                if (mAlwaysShowFoot) {
                    int index = position - (mAlwaysShowHead ? (getHeadSize() + 1) : 1);
                    if (index >= 0 && index < getFootSize()) {
                        // 显示尾部
                        if (mFootLayoutAndSpanStates[index * 3 + 2] > 0) {
                            int spanCount = gridLayoutManager.getSpanCount();
                            return mFootLayoutAndSpanStates[index * 3 + 2] > spanCount ? spanCount : mFootLayoutAndSpanStates[index * 3 + 2];
                        }
                        if (mFootLayoutAndSpanStates[index * 3 + 1] == 0) {
                            return 1;
                        }
                        return gridLayoutManager.getSpanCount();
                    }
                }
                // 当数据为空时,Empty界面都FullSpan
                return gridLayoutManager.getSpanCount();
            }
            if (position < getHeadSize()) {
                // 显示头部
                if (mHeadLayoutAndSpanStates[position * 3 + 2] > 0) {
                    int spanCount = gridLayoutManager.getSpanCount();
                    return mHeadLayoutAndSpanStates[position * 3 + 2] > spanCount ? spanCount : mHeadLayoutAndSpanStates[position * 3 + 2];
                }
                if (mHeadLayoutAndSpanStates[position * 3 + 1] == 0) {
                    return 1;
                }
                return gridLayoutManager.getSpanCount();
            }
            int index = position - (getHeadSize() + mData.size());
            if (index >= 0 && index < getFootSize()) {
                // 显示尾部
                if (mFootLayoutAndSpanStates[index * 3 + 2] > 0) {
                    int spanCount = gridLayoutManager.getSpanCount();
                    return mFootLayoutAndSpanStates[index * 3 + 2] > spanCount ? spanCount : mFootLayoutAndSpanStates[index * 3 + 2];
                }
                if (mFootLayoutAndSpanStates[index * 3 + 1] == 0) {
                    return 1;
                }
                return gridLayoutManager.getSpanCount();
            }
            if (index >= 0) {
                // 显示加载更多
                return gridLayoutManager.getSpanCount();
            }
            final T data = getData(position);
            if (data != null && data instanceof IMultiItem) {
                int spanSize = ((IMultiItem) data).getSpanSize();
                return spanSize <= 0 ? 1 : spanSize > gridLayoutManager.getSpanCount() ? gridLayoutManager.getSpanCount() : spanSize;
            }
            return 1;
        }
    });
}
Also used : GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Example 8 with GridLayoutManager

use of androidx.recyclerview.widget.GridLayoutManager in project mopub-android-mediation by mopub.

the class NativeRecyclerViewFragment method toggleRecyclerLayout.

void toggleRecyclerLayout() {
    if (mLayoutType == LayoutType.LINEAR) {
        mLayoutType = LayoutType.GRID;
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    } else {
        mLayoutType = LayoutType.LINEAR;
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }
}
Also used : GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Example 9 with GridLayoutManager

use of androidx.recyclerview.widget.GridLayoutManager in project BaseProject by fly803.

the class AutoFitRecyclerView method init.

private void init() {
    int mode = (int) SharedPreferencesUtils.getInstance().get("sp_key_switch_mode", 0);
    if (mode == MODE_LIST) {
        mSpanCount = 1;
    } else {
        mSpanCount = 2;
    }
    manager = new GridLayoutManager(getContext(), mSpanCount);
    setLayoutManager(manager);
    mChildrenHeights = new SparseIntArray();
}
Also used : GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) SparseIntArray(android.util.SparseIntArray)

Example 10 with GridLayoutManager

use of androidx.recyclerview.widget.GridLayoutManager in project BaseProject by fly803.

the class GridRecyclerView method init.

private void init() {
    int spanCount = 2;
    GridLayoutManager manager = new GridLayoutManager(getContext(), spanCount);
    setLayoutManager(manager);
}
Also used : GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager)

Aggregations

GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)122 RecyclerView (androidx.recyclerview.widget.RecyclerView)63 View (android.view.View)45 TextView (android.widget.TextView)19 StaggeredGridLayoutManager (androidx.recyclerview.widget.StaggeredGridLayoutManager)16 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)13 ImageView (android.widget.ImageView)12 Nullable (androidx.annotation.Nullable)11 Toolbar (androidx.appcompat.widget.Toolbar)10 ViewGroup (android.view.ViewGroup)7 SmoothScrollGridLayoutManager (eu.davidea.flexibleadapter.common.SmoothScrollGridLayoutManager)7 SuppressLint (android.annotation.SuppressLint)6 Context (android.content.Context)6 ArrayList (java.util.ArrayList)6 NonNull (androidx.annotation.NonNull)5 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)5 List (java.util.List)5 RefreshingListenerAdapter (me.dkzwm.widget.srl.RefreshingListenerAdapter)5 Drawable (android.graphics.drawable.Drawable)4 Handler (android.os.Handler)3