بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
کمک در ساخت لیست بی پایان
با سلام دوستان من می خوام لیست بی پایان بسازم یه مقداری از راه رو رفتم ولی نمی دونم درسته یا نه لطفا راهنمایی کنید؟! من کدها ر می زارم :
getComment();
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
int lastFirstVisiblePosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPosition(lastFirstVisiblePosition);
loadMore();
}
});
public void getComment() {
counter = 0;
G.apiService.getListComment(new ApiService.onCommentReceived() {
@Override
public void onReceived(List<Comment> comments) {
adapterComment = new AdapterComment(comments);
recyclerView.setAdapter(adapterComment);
recyclerView.setLayoutManager(linearLayoutManager);
}
}, id);
}
public void loadMore(){
counter = counter + 20;
G.apiService.getListComment(new ApiService.onCommentReceived() {
@Override
public void onReceived(List<Comment> comments) {
adapterComment = new AdapterComment(comments);
recyclerView.setAdapter(adapterComment);
recyclerView.setLayoutManager(linearLayoutManager);
}
}, id);
}
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
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);
}
من هر بار که اجرا می کنم دوباره همونای قبلی رو از سرور می گیره!!!!!
0
0
چند ماه پیش برا یه برنامه ای می خواستم یه لیست بی پایان درست کنم که اطلاعات رو قسمت به قسمت از سرور بگیره. باید بگم چون اولین باری بود که این لیست رو درست میکنم به معنی واقعی سرویس شدم اما در آخر درست شد. کدهایی که خودم نوشتم یه مقدار فرق میکنه. اون قسمت دوم کدتون کلاس EndlessRecyclerOnScrollListener رو من توی خود Adapter نوشتم و شبیه همین کد ها هم بود. اینم که همون مقدار های قبلی رو میاره فک کنم بخاطر این هست که توی متد loadMore() باید از Adapter.notifyDataSetChanged() استفاده کنید تا فقط تغییرات اعمال بشن. (9 سال پیش)
0
0
اگه مشکل حل نشد بگید دو تا فایل از کدای خودمو براتون بزارم (9 سال پیش)
برای این سوال 2 پاسخ وجود دارد.
مشاهده پاسخ صحیح
پاسخ به سوال
Shadow
9 سال پیش
0
0
خوب تا اینجا پیش رفتم و درست کار می کنه ولی دوتا مشکل داره اونم اینکه :
1 - ریسایکلر توی همه آیتم هاش فقط یه چیز رو نشون میده
2- وقتی از اکتیویتی خارج می شم و دوباره شروع می کنم هیچی نشون نمیده
کدها رو می زارم لطفا راهنمایی کنید؟!
public class ApiService {
int counter=0;
public ApiService() {
}
AdapterComment adapterComment;
List<Comment> comments = new ArrayList<>();
public void getListComment(final onCommentReceived onCommentReceived, int id, final RecyclerView recyclerView) {
Map<String, String> map = new HashMap<>();
final VolleyRequestHelper helper = new VolleyRequestHelper(Request.Method.POST, G.URL_ADDRESS2+counter, map, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
counter++;
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++) {
Comment comment = new Comment();
JSONObject jsonObjec = (JSONObject) jsonArray.get(i);
//int id = jsonObjec.getInt("id");
String name = jsonObjec.getString("name");
String body = jsonObjec.getString("body");
comment.setName(name);
comment.setBody(body);
if(comments.size()>0){
adapterComment.setData(comments);
adapterComment.notifyDataSetChanged();
recyclerView.setAdapter(adapterComment);
}else {
adapterComment = new AdapterComment();
comments=new ArrayList<>();
comments.add(comment);
}
}
onCommentReceived.onReceived(comments);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(G.context).add(helper);
}
public class AdapterComment extends RecyclerView.Adapter<AdapterComment.CommentViewHolder> {
private List<Comment> comments = new ArrayList<>();
Comment comment;
public AdapterComment(){
}
public void setData(List<Comment> comments){
this.comments.addAll(comments);
notifyDataSetChanged();
}
InfiniteScrollProvider infiniteScrollProvider = new InfiniteScrollProvider();
infiniteScrollProvider.attach(recyclerView, new OnLoadMoreListener() {
@Override
public void onLoadMore() {
counter += 1;
progressBar.setVisibility(View.VISIBLE);
getComment();
//progressBar.setVisibility(View.GONE);
}
});
public void getComment() {
G.apiService.getListComment(new ApiService.onCommentReceived() {
@Override
public void onReceived(List<Comment> comments) {
}
}, id, recyclerView);
}
پاسخ به سوال
Shadow
9 سال پیش
0
0
پاسخ صحیح
دوستان حل شد کدا ها رو برای استفاده دوستان می زارم بعدا ...
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .