Search in sources :

Example 26 with StaggeredGridLayoutManager

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

the class BasicActivity method initView.

private void initView() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mFullSpan1 = (CheckBox) findViewById(R.id.cb_head1);
    mFullSpan2 = (CheckBox) findViewById(R.id.cb_head2);
    mSpanSize1 = (EditText) findViewById(R.id.et_span_count1);
    mSpanSize2 = (EditText) findViewById(R.id.et_span_count2);
    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.addHeadLayout(R.layout.item_head1, false, 0);
    mAdapter.addHeadLayout(R.layout.item_head2, false, 0);
    // mAdapter.addFootLayout(R.layout.item_head1,false,0);
    // mAdapter.addFootLayout(R.layout.item_head2,false,0);
    mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(@NonNull View view, final int adapterPosition) {
            new AlertDialog.Builder(BasicActivity.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);
        }
    });
    mFullSpan1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            changeHead();
        }
    });
    mFullSpan2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            changeHead();
        }
    });
    mSpanSize1.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            changeHead();
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    mSpanSize2.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            changeHead();
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}
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) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) CompoundButton(android.widget.CompoundButton) Toolbar(androidx.appcompat.widget.Toolbar)

Example 27 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager 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(androidx.recyclerview.widget.SimpleItemAnimator) OnItemClickListener(xyz.zpayh.adapter.OnItemClickListener) ImageLabel(xyz.zpayh.myadapter.data.ImageLabel) IExpandable(xyz.zpayh.adapter.IExpandable) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.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(androidx.recyclerview.widget.GridLayoutManager) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) MyExpandableAdapter(xyz.zpayh.myadapter.adapter.MyExpandableAdapter) Toolbar(androidx.appcompat.widget.Toolbar)

Example 28 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager 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(androidx.recyclerview.widget.SimpleItemAnimator) ArrayList(java.util.ArrayList) SelectorCard(xyz.zpayh.myadapter.data.SelectorCard) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) NoSelectCard(xyz.zpayh.myadapter.data.NoSelectCard) OnItemCheckedChangeListener(xyz.zpayh.adapter.OnItemCheckedChangeListener) MyMultiSelectAdapter(xyz.zpayh.myadapter.adapter.MyMultiSelectAdapter) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) RecyclerView(androidx.recyclerview.widget.RecyclerView) Toolbar(androidx.appcompat.widget.Toolbar) IMultiSelectItem(xyz.zpayh.adapter.IMultiSelectItem)

Example 29 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager in project Moneycim by volkansahin45.

the class SpendingListFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_spending_list, container, false);
    unbinder = ButterKnife.bind(this, view);
    adapter = new SpendingRecyclerViewAdapter(getActivity(), spendingList, this);
    layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    spendingRecyclerView.setAdapter(adapter);
    spendingRecyclerView.setLayoutManager(layoutManager);
    return view;
}
Also used : StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) BindView(butterknife.BindView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) Nullable(androidx.annotation.Nullable)

Example 30 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager in project BGARefreshLayout-Android by bingoogolapple.

the class BGARefreshScrollingUtil method isRecyclerViewToTop.

public static boolean isRecyclerViewToTop(RecyclerView recyclerView) {
    if (recyclerView != null) {
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager == null) {
            return true;
        }
        if (manager.getItemCount() == 0) {
            return true;
        }
        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
            int firstChildTop = 0;
            if (recyclerView.getChildCount() > 0) {
                // 处理item高度超过一屏幕时的情况
                View firstVisibleChild = recyclerView.getChildAt(0);
                if (firstVisibleChild != null && firstVisibleChild.getMeasuredHeight() >= recyclerView.getMeasuredHeight()) {
                    if (android.os.Build.VERSION.SDK_INT < 14) {
                        return !(ViewCompat.canScrollVertically(recyclerView, -1) || recyclerView.getScrollY() > 0);
                    } else {
                        return !ViewCompat.canScrollVertically(recyclerView, -1);
                    }
                }
                // 如果RecyclerView的子控件数量不为0,获取第一个子控件的top
                // 解决item的topMargin不为0时不能触发下拉刷新
                View firstChild = recyclerView.getChildAt(0);
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) firstChild.getLayoutParams();
                firstChildTop = firstChild.getTop() - layoutParams.topMargin - getRecyclerViewItemTopInset(layoutParams) - recyclerView.getPaddingTop();
            }
            if (layoutManager.findFirstCompletelyVisibleItemPosition() < 1 && firstChildTop == 0) {
                return true;
            }
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;
            int[] out = layoutManager.findFirstCompletelyVisibleItemPositions(null);
            if (out[0] < 1) {
                return true;
            }
        }
    }
    return false;
}
Also used : RecyclerView(androidx.recyclerview.widget.RecyclerView) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) AbsListView(android.widget.AbsListView) ScrollView(android.widget.ScrollView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) WebView(android.webkit.WebView)

Aggregations

StaggeredGridLayoutManager (androidx.recyclerview.widget.StaggeredGridLayoutManager)36 RecyclerView (androidx.recyclerview.widget.RecyclerView)25 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)16 View (android.view.View)14 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)9 Toolbar (androidx.appcompat.widget.Toolbar)6 AbsListView (android.widget.AbsListView)5 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)4 WebView (android.webkit.WebView)3 ScrollView (android.widget.ScrollView)3 BindView (butterknife.BindView)3 OnItemLongClickListener (xyz.zpayh.adapter.OnItemLongClickListener)3 DialogInterface (android.content.DialogInterface)2 Adapter (android.widget.Adapter)2 FrameLayout (android.widget.FrameLayout)2 TextView (android.widget.TextView)2 Nullable (androidx.annotation.Nullable)2 ContentLoadingProgressBar (androidx.core.widget.ContentLoadingProgressBar)2 SimpleItemAnimator (androidx.recyclerview.widget.SimpleItemAnimator)2 FloatingActionButton (com.google.android.material.floatingactionbutton.FloatingActionButton)2