Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hua.rcvtest;
- import android.support.annotation.NonNull;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.view.View;
- import java.util.ArrayList;
- import java.util.List;
- public abstract class GroupAdapter<R,C> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
- public static final int TYPE_HEADER = 0;
- private List<R> items;
- private ArrayList<GroupHeader<C>> headers;
- protected ArrayList<Object> itemWithHeaders;
- private LinearLayoutManager linearLayoutManager;
- private ActionChange<C> actionChange;
- interface ActionChange<C>{
- void onChanged(C obj);
- }
- public void setActionChange(ActionChange<C> actionChange) {
- this.actionChange = actionChange;
- }
- public GroupAdapter(List<R> items) {
- this.items = items;
- initHeader();
- }
- public void setupRecyclerView(RecyclerView rcv, final View placeHolder){
- linearLayoutManager = new LinearLayoutManager(rcv.getContext());
- rcv.setLayoutManager(linearLayoutManager);
- rcv.setAdapter(this);
- if (actionChange!=null && !headers.isEmpty()){
- actionChange.onChanged(headers.get(0).groupObj);
- }
- rcv.addOnScrollListener(new RecyclerView.OnScrollListener() {
- @Override
- public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- int firstHeaderIndex = findFirstHeader(linearLayoutManager.findFirstVisibleItemPosition(),
- linearLayoutManager.findLastVisibleItemPosition());
- if (firstHeaderIndex == -1){
- return;
- }
- View firstHederView = linearLayoutManager.findViewByPosition(firstHeaderIndex);
- // Log.i("fuck", "view="+firstHederView.getY());
- if (firstHederView == null){
- return;
- }
- float ty = firstHederView.getY();
- float h = placeHolder.getHeight();
- float perY = placeHolder.getY();
- if (ty<=0){
- placeHolder.setY(0);
- }else if(ty <= h){
- placeHolder.setY(ty-h);
- }else {
- placeHolder.setY(0);
- }
- if (perY!=0 && placeHolder.getY() ==0){
- // Log.e("fuck", "onShow: ");
- GroupHeader<C> header= getNowHeader(linearLayoutManager.findFirstVisibleItemPosition());
- if (actionChange!=null && header!=null){
- actionChange.onChanged(header.groupObj);
- }
- }
- }
- });
- }
- public void replaceData(List<R> items){
- this.items.clear();
- this.items.addAll(items);
- initHeader();
- notifyDataSetChanged();
- }
- public void addDatas(List<R> items){
- this.items.addAll(items);
- C perDate = headers.get(headers.size()-1).groupObj;
- for (int i = 0; i < items.size(); i++) {
- R temp = items.get(i);
- if (!compareGroupObj(perDate,temp)){
- perDate = objGetGroupVar(temp);
- GroupHeader<C> header = new GroupHeader<>(perDate, i + headers.size());
- headers.add(header);
- itemWithHeaders.add(header);
- }
- itemWithHeaders.add(temp);
- }
- }
- private void initHeader() {
- headers = new ArrayList<>();
- itemWithHeaders = new ArrayList<>();
- C perDate = null;
- for (int i = 0; i < items.size(); i++) {
- R temp = items.get(i);
- if (!compareGroupObj(perDate,temp)){
- perDate = objGetGroupVar(temp);
- GroupHeader<C> header = new GroupHeader<>(perDate, i + headers.size());
- headers.add(header);
- itemWithHeaders.add(header);
- }
- itemWithHeaders.add(temp);
- }
- }
- private int findFirstHeader(int start,int end){
- for (int i = start; i <= end; i++) {
- Object temp = itemWithHeaders.get(i);
- if (temp instanceof GroupHeader){
- return i;
- }
- }
- return -1;
- }
- private GroupHeader<C> getNowHeader(int start){
- for (int i = headers.size() - 1; i >= 0; i--) {
- GroupHeader temp = headers.get(i);
- if (temp.getStartIndex() <= start){
- return headers.get(i);
- }
- }
- return null;
- }
- /**
- * 比较分组 ,数据是否属于该分组
- * @param that
- * @param obj
- * @return
- */
- abstract boolean compareGroupObj(C that,R obj);
- /**
- * 从数据获取比较用的变量
- * @param obj
- * @return
- */
- abstract C objGetGroupVar(R obj);
- @Override
- public int getItemViewType(int position) {
- Object temp =itemWithHeaders.get(position);
- if (temp instanceof GroupHeader){
- return TYPE_HEADER;
- }
- return 1;
- }
- @Override
- public int getItemCount() {
- return itemWithHeaders.size();
- }
- public static class GroupHeader<C>{
- private C groupObj;
- private int startIndex;
- public GroupHeader(C groupObj, int startIndex) {
- this.groupObj = groupObj;
- this.startIndex = startIndex;
- }
- public C getGroupObj() {
- return groupObj;
- }
- public GroupHeader<C> setGroupObj(C groupObj) {
- this.groupObj = groupObj;
- return this;
- }
- public int getStartIndex() {
- return startIndex;
- }
- public GroupHeader<C> setStartIndex(int startIndex) {
- this.startIndex = startIndex;
- return this;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement