मुझे पता है कि इस सवाल को कई बार पहले पूछा गया था, लेकिन मैं उलझन में हूं कि कभी-कभी डेटा लोड हो जाता है और कभी-कभी डेटा सूची के अंत तक डेटा लोड नहीं होता है। साथ ही जब मैं सूची के माध्यम से तेजी से स्क्रॉलिंग करता हूं, और नया डेटा लोड हो गया है, लेकिन तुरंत यह मुझे सूची में पहले आइटम पर लौटाता है और सर्वर से अगले पृष्ठ से सभी नए लोड किए गए आइटम हटा देता है। तो यह दूसरी समस्या है और तीसरी समस्या यह है कि जब मैं SwipeRefreshLayout का उपयोग करके आइटम लोड करता हूं, तो मुझे सूची के अंत तक पहुंचने पर भी नए आइटम नहीं मिल रहे हैं।इन्फिनिटिव रीसाइक्लर व्यू मेरे लिए काम नहीं करता
मैं अपने प्रोजेक्ट में इस को लागू किया है: https://gist.github.com/ssinss/e06f12ef66c51252563e
list.setLayoutManager(manager);
list.setEmptyView(emptyView);
list.setItemAnimator(new DefaultItemAnimator());
list.setAdapter(mAdapter);
loadJokes(1);
list.addOnScrollListener(new EndlessRecyclerOnScrollListener(manager) {
@Override
public void onLoadMore(final int current_page) {
loadMoreJokes(current_page);
}
});
यहाँ विधि मैं कहाँ सर्वर से अधिक आइटम लोड कर रहा हूँ है: यहाँ
private void loadMoreJokes(int current_page) {
StringRequest request = new StringRequest(Request.Method.GET, AppConfig.URL_GET_ALL_JOKES + current_page,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hideDialog();
try {
JSONObject object = new JSONObject(response);
boolean error = object.getBoolean("error");
JSONArray jokes = object.getJSONArray("jokes");
if (!error) {
for (int i = 0; i < jokes.length(); i++) {
JSONObject object1 = jokes.getJSONObject(i);
Joke joke = new Joke();
joke.setId(object1.optInt("id"));
joke.setLikes(object1.optInt("likes"));
joke.setComments(object1.optInt("comments"));
joke.setJoke(object1.optString("joke"));
joke.setCreatedAt(object1.optString("created_at"));
joke.setName(object1.optString("user_name"));
joke.setImagePath(object1.optString("image_path"));
joke.setFacebookUserId(object1.optString("facebook_user_id"));
joke.setCategory(object1.optString("category"));
mJokes.add(joke);
}
menu.showMenu(true);
}
// Notify adapter that data has changed
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideDialog();
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance().addToRequestQueue(request);
}
और विधि मैं कहाँ हूँ है जब कोई ऐप लॉन्च करता है तो पहले दृश्यमान आइटम लोड करना:
private void loadJokes(int page) {
pDialog.setMessage("Loading..");
showDialog();
StringRequest request = new StringRequest(Request.Method.GET, AppConfig.URL_GET_ALL_JOKES + page,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mJokes.clear();
hideDialog();
try {
JSONObject object = new JSONObject(response);
boolean error = object.getBoolean("error");
JSONArray jokes = object.getJSONArray("jokes");
if (!error) {
for (int i = 0; i < jokes.length(); i++) {
JSONObject object1 = jokes.getJSONObject(i);
Joke joke = new Joke();
joke.setId(object1.optInt("id"));
joke.setLikes(object1.optInt("likes"));
joke.setComments(object1.optInt("comments"));
joke.setJoke(object1.optString("joke"));
joke.setCreatedAt(object1.optString("created_at"));
joke.setName(object1.optString("user_name"));
joke.setImagePath(object1.optString("image_path"));
joke.setFacebookUserId(object1.optString("facebook_user_id"));
joke.setCategory(object1.optString("category"));
mJokes.add(joke);
}
menu.showMenu(true);
}
// Notify adapter that data has changed
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideDialog();
menu.showMenu(true);
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance().addToRequestQueue(request);
}
और यह 0 हैविधि:
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
refreshItems();
}
}, 5000);
}
private void refreshItems() {
loadJokes(1);
mSwipeRefreshLayout.setRefreshing(false);
}
तो मैं अधिक कोड पोस्ट करने के लिए, मुझे पता है की जरूरत है। मुझे जितनी जल्दी हो सके इस समस्या को हल करने की ज़रूरत है। तो फिर, समस्याओं निम्नलिखित हैं:
तेजी से सूची स्क्रॉल करते हैं, नए आइटम लोड किया जा रहा है, लेकिन तुरंत बाद कि यह मुझे सूची की शुरुआत करने के लिए वापस आता है और जब मैं के अंत में जाना फिर से सूची, अधिक लोड जवाब नहीं देता है।
SwipRefreshLayout
के साथ सूची को रीफ्रेश करने के बाद, स्क्रॉलिंग अंत में प्रतिक्रिया नहीं देती है।
नोट: स्क्रॉलिंग और लोडिंग नए आइटम केवल तभी काम कर रहे हैं जब मैं सूची के माध्यम से धीरे-धीरे जाता हूं और यदि मैं सूची को रीफ्रेश करने के लिए स्वाइप नहीं करता हूं।
संपादित करें:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_jokes, container, false);
mContext = getActivity();
mView = (CoordinatorLayout) view.findViewById(R.id.coordinatorLayout);
TextView tvEmptyText = (TextView) view.findViewById(R.id.tv_empty);
ImageView ivSignal = (ImageView) view.findViewById(R.id.iv_signal);
if (!ConnectionDetector.getInstance(getActivity()).isOnline() && mAdapter == null) {
tvEmptyText.setVisibility(View.VISIBLE);
ivSignal.setVisibility(View.VISIBLE);
showNoInternetSnackbar();
}
// INITIALIZE RECYCLER VIEW
EmptyRecyclerView list = (EmptyRecyclerView) view.findViewById(R.id.list);
mJokes = new ArrayList<>();
mAdapter = new RecyclerJokesAdapter(getActivity(), mJokes, JokesFragment.this, null);
// Progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
showDialog();
View emptyView = inflater.inflate(R.layout.layout_empty_view, container, false);
FloatingActionButton fab1 = (FloatingActionButton) view.findViewById(R.id.fab_funny);
FloatingActionButton fab2 = (FloatingActionButton) view.findViewById(R.id.fab_good_morning);
FloatingActionButton fab3 = (FloatingActionButton) view.findViewById(R.id.fab_good_night);
FloatingActionButton fab4 = (FloatingActionButton) view.findViewById(R.id.fab_all);
menu = (FloatingActionMenu) view.findViewById(R.id.menu_sort_jokes);
fab1.setOnClickListener(this);
fab2.setOnClickListener(this);
fab3.setOnClickListener(this);
fab4.setOnClickListener(this);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(
R.color.refresh_progress_1,
R.color.refresh_progress_2,
R.color.refresh_progress_3);
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
list.setLayoutManager(manager);
list.setEmptyView(emptyView);
list.setItemAnimator(new DefaultItemAnimator());
list.setAdapter(mAdapter);
if (ConnectionDetector.getInstance(mContext).isOnline()) {
loadJokes(1);
} else {
showNoInternetSnackbar();
hideDialog();
}
list.addOnScrollListener(new EndlessRecyclerOnScrollListener(manager) {
@Override
public void onLoadMore(final int current_page) {
loadMoreJokes(current_page);
}
});
return view;
}
अरे .. क्या आप कोड जोड़ सकते हैं जहां आपने 'mAdapter' फ़ील्ड शुरू किया था। – Jickson
हां। एक मिनट में आओ –
क्या आपने इस http://stackoverflow.com/a/30691092/855843 – Vilen