gpt4 book ai didi

android - recyclerView.setOnScrollChangeListener 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:04 24 4
gpt4 key购买 nike

这是我的 fragment 代码。我正在使用这段代码用于无尽的回收器 View ,它是在滚动回收器 View 项时从服务器获取数据的。但代码不工作。在此代码中,recyclerView.setOnScrollChangeListener() 不起作用,它显示错误 View 无法应用于 android.content .context。滚动列表时,如何使用fragment中的setOnScrollChangeListener从服务器加载数据。

    public class Fragment1 extends Fragment implements 
RecyclerView.OnScrollChangeListener{
//Creating a List of superheroes
private List<SuperHero> listSuperHeroes;

//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public ProgressBar progressBar;

//Volley Request Queue
private RequestQueue requestQueue;

//The request counter to send ?page=1, ?page=2 requests
private int requestCount = 1;

public Fragment1() {}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.activity_main, container,
false);
//Initializing Views
recyclerView.setLayoutManager(layoutManager);

//Initializing our superheroes list
listSuperHeroes = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getContext());

//Calling method to get data to fetch data
getData();

//Adding an scroll change listener to recyclerview
recyclerView.setOnScrollChangeListener(getActivity());

//initializing our adapter
adapter = new CardAdapter(listSuperHeroes, getActivity());

//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
return view;
}

//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
//Initializing ProgressBar


//Displaying Progressbar
// progressBar.setVisibility(View.VISIBLE);
//setProgressBarIndeterminateVisibility(true);

//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response

Log.e("response",response.toString());
parseData(response);
//Hiding the progressbar
progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});

//Returning the request
return jsonArrayRequest;
}

//This method will get data from the web api
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}

//This method will parse json data
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
//Creating the superhero object
//int a= array.length();
//String s=String.valueOf(a);
//Log.e("array",s);
SuperHero superHero = new SuperHero();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);

//Adding data to the superhero object
superHero.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
//superHero.setName(json.getString(Config.TAG_NAME));
//superHero.setPublisher(json.getString(Config.TAG_PUBLISHER));
superHero.setMglId(json.getString(Config.TAG_MGLID));
superHero.setAgeHeight(json.getString(Config.TAG_AGEHEIGHT));
superHero.setCommunity(json.getString(Config.TAG_COMMUNITY));
superHero.setOccupation(json.getString(Config.TAG_OCCUPATION));
superHero.setIncome(json.getString(Config.TAG_INCOME));
} catch (JSONException e) {
e.printStackTrace();
}
//Adding the superhero object to the list
listSuperHeroes.add(superHero);
}

//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}

//This method would check that the recyclerview scroll has reached the bottom or not
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
}

//Overriden method to detect scrolling
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//Ifscrolled at last then
if (isLastItemDisplaying(recyclerView)) {
//Calling the method getdata again
getData();
}
}

最佳答案

使用这个

recyclerView.setOnScrollChangeListener(Fragment1.this);

而不是这个

recyclerView.setOnScrollChangeListener(getActivity());

编辑

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isLastItemDisplaying(recyclerView)) {
//Calling the method getdata again
getData();
}
}
});

关于android - recyclerView.setOnScrollChangeListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47264110/

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