gpt4 book ai didi

java - android中recyclerview的分页

转载 作者:行者123 更新时间:2023-12-02 03:28:16 26 4
gpt4 key购买 nike

我正在尝试在 recyclerview 中实现分页。以下是我的 java 类:

MyAdapter.java

    package com.example.opinion;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


/**
* Created by manish on 7/18/2016.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<String> movieName = new ArrayList();


public MyAdapter(ArrayList film)
{
this.movieName = film;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items, null);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.movieText.setText(movieName.get(position));



}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount()
{
System.out.print(movieName.size());
return movieName.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView movieText;

public ViewHolder(View v) {
super(v);
movieText = (TextView) v.findViewById(R.id.movieName);
}
}

}

viewfragment.java:在这里我正在获取本地网络上的数据。在此文件中,loadMore(current_page) 函数一次仅获取 3 个值。第一次调用时,这三个值仅在 recyclerview 内的cardview中可见。我不知道问题出在哪里!

package com.example.opinion;



import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Text;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Intent;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;


public class ViewFragment extends Fragment {

private static final String PREFS_NAME = "Prefname";



public int j =0;
static String quesText;


//public static final String BASE_URL = "http://192.168.0.104/";

public interface PostquesApi{
@GET("ques/{i}")
Call<quesGetResponse> quesget(@Path("i") String i);
}

static class quesGetResponse {

String ques1;
String ques2;
String ques3;
String quid1;
String quid2;
String quid3;

public quesGetResponse(String ques1, String ques2, String ques3, String u1, String u2,String u3){
this.ques1 = ques1;
this.ques2 = ques2;
this.ques3 = ques3;
this.quid1 = u1;
this.quid2 = u2;
this.quid3 = u3;
}
}
//View rootView;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;

private ArrayList movieList = new ArrayList();
private static int current_page = 1;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

final View rootView = inflater.inflate(R.layout.fragment_view, container, false);

for(int k=0; k<2; k++) {
loadData(current_page);
}
System.out.println("contents of movie list: "+ movieList);

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

// use a linear layout manager
LinearLayoutManager feedLayoutManager = new LinearLayoutManager(getActivity());
mAdapter = new MyAdapter(movieList);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(feedLayoutManager);

// specify an adapter (see also next example)

mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(
feedLayoutManager) {
@Override
public void onLoadMore(int current_page) {
// do somthing...

loadMoreData(current_page);

}

});

/*qt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent comm = new Intent(getActivity(), Comments.class);
quesText = qt2.getText().toString();

//comm.putExtra(quesText, Text );
startActivity(comm);
}
});
qt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent comm = new Intent(getActivity(), Comments.class);
quesText = qt3.getText().toString();

//comm.putExtra(quesText, Text );
startActivity(comm);
}
});*/

return rootView;

}
public void loadData(int page) {
// Send an API request to retrieve appropriate data using the offset value as a parameter.
// --> Deserialize API response and then construct new objects to append to the adapter
// --> Notify the adapter of the changes
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Login.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

PostquesApi apiService = retrofit.create(PostquesApi.class);
Call<quesGetResponse> call = apiService.quesget(Integer.toString(j));
call.enqueue(new Callback<quesGetResponse>() {
@Override
public void onResponse(Call<quesGetResponse> call, Response<quesGetResponse> response) {
quesGetResponse decodedResponse = response.body();
if (decodedResponse != null) {
if (decodedResponse.ques1 != null && decodedResponse.quid1.compareTo(Login.user_id) != 0)
{
movieList.add(decodedResponse.ques1);
}
if (decodedResponse.ques2 != null && decodedResponse.quid2.compareTo(Login.user_id) != 0)
{
movieList.add(decodedResponse.ques2);
}
if (decodedResponse.ques3 != null && decodedResponse.quid3.compareTo(Login.user_id) != 0)
{
movieList.add(decodedResponse.ques3);
}
}

if (decodedResponse.ques1 == null || decodedResponse.ques2 == null || decodedResponse.ques3 == null) {
Context context = getActivity().getApplicationContext();
CharSequence text = "Sorry!! no more questions in the database...";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
}

j = j + 3;
}

@Override
public void onFailure(Call<quesGetResponse> call, Throwable t) {

}
});
}
private void loadMoreData(int current_page) {

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Login.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

PostquesApi apiService = retrofit.create(PostquesApi.class);
Call<quesGetResponse> call = apiService.quesget(Integer.toString(j));
call.enqueue(new Callback<quesGetResponse>() {
@Override
public void onResponse(Call<quesGetResponse> call, Response<quesGetResponse> response) {
quesGetResponse decodedResponse = response.body();
if (decodedResponse != null) {
if (decodedResponse.ques1 != null && decodedResponse.quid1.compareTo(Login.user_id) != 0)
{
movieList.add(decodedResponse.ques1);
}
if (decodedResponse.ques2 != null && decodedResponse.quid2.compareTo(Login.user_id) != 0)
{
movieList.add(decodedResponse.ques2);
}
if (decodedResponse.ques3 != null && decodedResponse.quid3.compareTo(Login.user_id) != 0)
{
movieList.add(decodedResponse.ques3);
}
}

if (decodedResponse.ques1 == null || decodedResponse.ques2 == null || decodedResponse.ques3 == null) {
Context context = getActivity().getApplicationContext();
CharSequence text = "Sorry!! no more questions in the database...";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
}

j = j + 3;
}

@Override
public void onFailure(Call<quesGetResponse> call, Throwable t) {

}
});



mAdapter.notifyDataSetChanged();

}

}

EndlessRecyclerOnScrollListener.java:实现分页逻辑的文件。

package com.example.opinion;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
* Created by manish on 7/20/2016.
*/

public abstract class EndlessRecyclerOnScrollListener extends
RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class
.getSimpleName();

private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 3;
int firstVisibleItem, visibleItemCount, totalItemCount;

private int current_page = 1;

private LinearLayoutManager mLinearLayoutManager;

public EndlessRecyclerOnScrollListener(
LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading
&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached

// Do something
current_page++;

onLoadMore(current_page);

loading = true;
}
}

public abstract void onLoadMore(int current_page);
}

如果有人可以审阅它并告诉我上述文件中需要的更正,或者可以指导我找到一些资源,我将非常感激。

最佳答案

我建议您应该在 MyAdapter 类中观看,而不是使用 EndlessRecyclerOnScrollListener 类并监听 Recyclerview 的滚动事件。

1。首先添加一个接口(interface)

public interface GetNewDataEvents {
void getNewAdapterData();
}

2。在 MyAdapter 类中初始化该接口(interface),通过实现该接口(interface)来添加数据。

3。您可以轻松地在 bindViewHolder 中找到当前正在加载的项目使用

if(position==(movieName.size()-1)

在其中您可以轻松调用该接口(interface)方法并在其中加载新数据。

这表示当前正在加载倒数第二个项目,您应该添加新数据(如果有)。

如果您获得新数据,您可以轻松地将其加载到适配器中,并使用它调用 notifyItemRangeChanged 来获得性能并实现最佳实践,而不是加载仅更改了少数项目的整个适配器。

想要引用可以去here

关于java - android中recyclerview的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38480182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com