在垂直RecyclerView内的水平RecyclerView上分页时,notifyDataSetChange不起作用

时间:2019-03-19 05:24:09

标签: java android android-recyclerview pagination recycler-adapter

在这里,我要在垂直回收器视图内的水平回收器视图中对项目进行分页。这样就可以完成每个水平回收站视图列表项的延迟加载。在这里,我在下面附加了布局和适配器:

主布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ProgressBar
    android:id="@+id/progress"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="visible" />

RecyclerView项

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
    android:id="@+id/horizontal_relative"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingStart="10dp">

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textSize="15sp"
        android:textStyle="bold" />

    <View
        android:layout_width="40dp"
        android:layout_height="2dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/header"
        android:layout_marginTop="10dp"/>

    <TextView
        android:id="@+id/viewall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"/>

</RelativeLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/horizontal_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/movie_horizontal_relative"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="25dp" />

主RecyclerView适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements ApiInterface.MoreView {

MorePresenterImpl datasMorePresenter;
int id;
String AUTHORIZATION, kunbataako;
int currentPage, newCurrentpage, oldCurrentPage = -1;
Realm realm;
HorizontalRecyclerViewAdapter horizontalRecyclerViewAdapter;
ViewHolder mainholder;
private Context mContext;
private HashMap<String, Pagination> dataHash;
private List<String> keyHash;
private List<> data;
private List datasList;
private ArrayList<List> datasListArrayList;
private String from;

public RecyclerViewAdapter(Context context, List<String> keyHash, HashMap<String, Pagination> dataHash, String from) {
    this.mContext = context;
    this.keyHash = keyHash;
    this.dataHash = dataHash;
    this.from = from;

    datasMorePresenter = new MorePresenterImpl(RecyclerViewAdapter.this);

    realm = Realm.getDefaultInstance();
    UserData user = RealmRead.findUser(realm);
    id = user.getUserId();
    AUTHORIZATION = "Bearer " + user.getToken();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_datas_horizontal_list, null);
    ViewHolder mh = new ViewHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    final String sectionName = keyHash.get(i);
    mainholder = viewHolder;
    data = dataHash.get(sectionName).gets();

    viewHolder.itemTitle.setText(sectionName);

    horizontalRecyclerViewAdapter = new HorizontalRecyclerViewAdapter(mContext, data, data, from);

    viewHolder.recycler_view_list.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
    viewHolder.recycler_view_list.setLayoutManager(linearLayoutManager);
    viewHolder.recycler_view_list.setAdapter(horizontalRecyclerViewAdapter);

    viewHolder.recycler_view_list.setNestedScrollingEnabled(false);

    viewHolder.recycler_view_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            currentPage = dataHash.get(sectionName).getCurrentPage();

            if (currentPage != oldCurrentPage) {
                if (currentPage <= dataHash.get(sectionName).getLastPage())
                    newCurrentpage = currentPage + 1;
                    oldCurrentPage = currentPage;

                    try {
                        datasMorePresenter.getMoreData(AUTHORIZATION, sectionName, newCurrentpage);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
}

@Override
public int getItemCount() {
    return (null != dataHash ? dataHash.size() : 0);
}

@Override
public void setMoreData(Pagination datasSort, String realCategory) {
    data = new ArrayList<>();
    data.addAll(dataHash.get(realCategory).gets());

    for ( datas : datasSort.gets()) {
        dataHash.get(realCategory).gets().add(datas);

        data.add(datas);
    }

        horizontalRecyclerViewAdapter.sendNotifyData(data, data);
        horizontalRecyclerViewAdapter.notifyDataSetChanged();
}

@Override
public void onError(String error) {
    try {
        Snackbar.make(mContext, error, Snackbar.LENGTH_SHORT).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView itemTitle;
    TextView itemViewall;
    RecyclerView recycler_view_list;

    public ViewHolder(View view) {
        super(view);

        this.itemTitle = (TextView) view.findViewById(R.id.header);
        this.itemViewall = (TextView) view.findViewById(R.id.viewall);
        this.recycler_view_list = (RecyclerView) view.findViewById(R.id.horizontal_recycler_view);

        itemViewall.setOnClickListener(this);
        itemViewall.setClickable(true);
    }

    @Override
    public void onClick(View view) {
    }
}

}

在这里,从API成功获取数据后,RecyclerView适配器尝试notifyDataSetChanged到水平的Recycler View适配器。我该如何实现?请帮帮我。

0 个答案:

没有答案