0

Trying to apply the onLoadmore functionality, after I update the adapter on each request Recyclerview takes to the first element, where I want to continue to view from the from the last added list

MainActivity Code:

    public class Inbox extends Fragment {
private  RecyclerView mRecycler;
private  InboxAdapter mAdapter;
SwipeRefreshLayout swipeContainer;
TextView txtnomail;
static String strtoken;
static int inboxcount;
ImageView Img;
List<MailDataModel> data = new ArrayList<>();
private  Context context = null;
int page;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview=inflater.inflate(R.layout.fragment_inbox, container, false);
    Img=rootview.findViewById(R.id.gifloader);
    mRecycler = (RecyclerView)rootview.findViewById(R.id.inbox_recycler);
    txtnomail = (TextView) rootview.findViewById(R.id.nomail_inbox);
    swipeContainer=(SwipeRefreshLayout)rootview.findViewById(R.id.swipeContainer_inbox);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    SharedPreferences.Editor editor = prefs.edit();
    inboxcount = prefs.getInt("inboxcount", 0);
    editor.apply();
    MainActivity activity = (MainActivity) getActivity();
    assert activity != null;
    strtoken = activity.getMyData();
    RetrieveInboxMails retrieveInboxMails=new RetrieveInboxMails();
    retrieveInboxMails.execute(String.valueOf(page));
    context=getActivity();

    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            RetrieveInboxMails retrieveInbox=new RetrieveInboxMails();
            retrieveInbox.execute(String.valueOf(page));
        }
    });
    // Configure the refreshing colors
    swipeContainer.setColorSchemeResources(
            android.R.color.holo_blue_light,
            android.R.color.holo_blue_light,
            android.R.color.holo_blue_light,
            android.R.color.holo_blue_dark);
    mAdapter = new InboxAdapter(mRecycler, context, data);
    mRecycler.setAdapter(mAdapter);
    mAdapter.notifyItemInserted(data.size());
    return rootview;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("Inbox");
}


public  class RetrieveInboxMails extends AsyncTask<String, String, String> {


    List<MailDataModel> tempdata = new ArrayList<>();
    final MailDataModel health = new MailDataModel();

    @Override
    protected void onPreExecute() {


        //  Img.setVisibility(View.VISIBLE);
        //  txtnomail.setVisibility(View.GONE);



    }
    @Override
    protected String doInBackground(String... strings) {
        try {


            String pages=strings[0];
            // Creating & connection Connection with url and required Header.
            URL url = new URL("http://api- 
         telemed.3cubehealth.com/api/fetchLocalEmails? 
        category=inbox&limit="+10+"&page="+pages);
            // URL url = new URL("http://api- 
           telemed.3cubehealth.com/api/fetchLocalEmails? 
             category=inbox&limit=10&page=0");
            HttpURLConnection urlConnection = (HttpURLConnection) 
              url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setRequestProperty("Content-Type",  
             "application/json");
            urlConnection.setRequestProperty("Authorization", "Bearer " + 
           strtoken);
            urlConnection.setDoInput(true);
            //POST or GET
            urlConnection.connect();
            int statusCode = urlConnection.getResponseCode();
            // Connection success. Proceed to fetch the response.
            if (statusCode == 200) {
                InputStream it = new 
              BufferedInputStream(urlConnection.getInputStream());
                InputStreamReader read = new InputStreamReader(it);
                BufferedReader buff = new BufferedReader(read);
                StringBuilder dta = new StringBuilder();
                String chunks;
                while ((chunks = buff.readLine()) != null) {
                    dta.append(chunks);
                }
                chunks = dta.toString();
                return chunks;
            }
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strtoken;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        swipeContainer.setRefreshing(false);
        Img.setVisibility(View.GONE);
        try {
            JSONArray jsonArray = new JSONArray(result);
            // Extract data from json and store into ArrayList as class 
             objects
            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject json_data = jsonArray.getJSONObject(i);
                health.setVessalName(json_data.getString("from_name"));
                health.setEmailId(json_data.getString("from_email"));
                health.setId(json_data.getInt("id"));
                health.setSubject(json_data.getString("subject"));
                health.setTransId(json_data.getInt("trans_id"));
                String date = json_data.getString("message_date");
                String message = json_data.getString("message_html");
                health.setStatus(json_data.getString("email_status"));
                if (context != null) {
                    SharedPreferences prefs_file = 
                PreferenceManager.getDefaultSharedPreferences(context);
                    SharedPreferences.Editor editor_file = prefs_file.edit();
                    editor_file.putString("result", result);
                    editor_file.apply();
                }

                health.setMessage(message);
                DateFormat inputFormatter1 = new SimpleDateFormat("yyyy-MM- 
                                                                  dd");
                Date date1 = inputFormatter1.parse(date);

                DateFormat outputFormatter1 = new SimpleDateFormat("d MMM");
                String output1 = outputFormatter1.format(date1);
                health.setDate(output1);
                data.add(health);

                if (context != null) {
                    mRecycler.setLayoutManager(new 
                LinearLayoutManager(context));




                }
            }
            mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
                    if (data.size() <=inboxcount) {
                        data.add(null);
                        mAdapter.notifyItemInserted(data.size());
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                data.remove(data.size() -1);
                                mAdapter.notifyItemRemoved(data.size());

                                page++;
                                new 
                   RetrieveInboxMails().execute(String.valueOf(page));
                                mAdapter.notifyDataSetChanged();
                                mAdapter.setLoaded();
                            }
                        }, 2000);
                    } else {
                        Toast.makeText(context, "Loading data completed", 
              Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
        catch(JSONException e){
            e.printStackTrace();
        } catch(ParseException e){
            e.printStackTrace();
        }
    }
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
    MenuItem mSearchMenuItem = menu.findItem(R.id.option_menu_search);
    SearchView searchView = (SearchView) mSearchMenuItem.getActionView();
    searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            mAdapter.getFilter().filter(newText);
            return false;
        }
    });


}




    }

Adapter Code:

    public class InboxAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {
Context mcontext;
LayoutInflater inflater;
List<MailDataModel> mailDataModelList;
List<MailDataModel> exampleListFull;
final int VIEW_TYPE_ITEM = 0;
final int VIEW_TYPE_LOADING = 1;
boolean isLoading;
private int visibleThreshold = 8;
private int lastVisibleItem, totalItemCount;
OnLoadMoreListener onLoadMoreListener;

public InboxAdapter(RecyclerView recyclerView, Context context,List<MailDataModel> mailDataModelList) {
    this.mcontext = context;
    // this.inflater = LayoutInflater.from(context);
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mailDataModelList = mailDataModelList;
    exampleListFull=new ArrayList<>(mailDataModelList);
    notifyItemRangeChanged(0, mailDataModelList.size()-1);

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            totalItemCount = linearLayoutManager.getItemCount();
            lastVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();

// lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();

            if (!isLoading&&totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                if (onLoadMoreListener != null) {
                    onLoadMoreListener.onLoadMore();
                }
                isLoading = true;
            }

        }
    });

}

@Override
public int getItemViewType(int position) {
    return mailDataModelList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == VIEW_TYPE_ITEM) {
        View view = LayoutInflater.from(mcontext).inflate(R.layout.layout_unread, parent, false);
        return new InboxMailsHolder(view);
    } else if (viewType == VIEW_TYPE_LOADING) {
        View view = LayoutInflater.from(mcontext).inflate(R.layout.item_loading, parent, false);
        return new LoadingViewHolder(view);
    }
    return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof InboxMailsHolder) {
        final MailDataModel data = mailDataModelList.get(position);
        final InboxMailsHolder inboxdMailsHolder = (InboxMailsHolder) holder;
        inboxdMailsHolder.date.setText(data.getDate());
        inboxdMailsHolder.subject.setText(data.getSubject());
        switch (data.getStatus()) {
            case "Read":
                inboxdMailsHolder.cardView.setBackgroundColor(Color.WHITE);
                break;
            case "Sent":
                inboxdMailsHolder.cardView.setBackgroundColor(Color.WHITE);
                break;

// case "Unread": // inboxdMailsHolder.cardView.setBackgroundColor(Color.parseColor("#EBEBEB")); default: inboxdMailsHolder.cardView.setBackgroundColor(Color.parseColor("#EBEBEB")); break; } if (data.getVessalName().equals("null")) { inboxdMailsHolder.emailid.setText(data.getEmailId());

        } else {
            inboxdMailsHolder.emailid.setText(data.getVessalName());
        }

        inboxdMailsHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new UnReadAdapter.MailStatusAsync(mcontext).execute(String.valueOf(data.getId()));
                inboxdMailsHolder.subject.setTypeface(null, Typeface.NORMAL);
                inboxdMailsHolder.cardView.setBackgroundColor(Color.WHITE);
                Intent email_intent = new Intent(view.getContext(), MailDetails.class);
                email_intent.putExtra("Date", data.getDate());
                email_intent.putExtra("TransId", data.getTransId());
                email_intent.putExtra("Id", data.getId());
                email_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mcontext.startActivity(email_intent);
            }
        });
    }
    else if (holder instanceof LoadingViewHolder) {
        LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
        loadingViewHolder.progressBar.setIndeterminate(false);
    }
}


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

public void setLoaded() {
    isLoading = false;
}
private class LoadingViewHolder extends RecyclerView.ViewHolder {
    public ProgressBar progressBar;

    public LoadingViewHolder(View view) {
        super(view);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
    }
}
public class InboxMailsHolder extends RecyclerView.ViewHolder{
    TextView emailid,subject,date;
    CardView cardView;
    public InboxMailsHolder(@NonNull View itemView) {
        super(itemView);
        emailid=itemView.findViewById(R.id.email_id);
        subject=itemView.findViewById(R.id.subject);
        date=itemView.findViewById(R.id.date);
        cardView=itemView.findViewById(R.id.mailcard);
    }
}
@Override
public Filter getFilter() {
    return exampleFilter;
}

private Filter exampleFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        List<MailDataModel> filteredList = new ArrayList<>();

        if (constraint == null || constraint.length() == 0) {

            filteredList.addAll(exampleListFull);
        } else {
            String filterPattern = constraint.toString().toLowerCase().trim();

            for (MailDataModel item : exampleListFull) {
                if (item.getEmailId().toLowerCase().contains(filterPattern)||item.getSubject().toLowerCase().contains(filterPattern)||item.getMessage().toLowerCase().contains(filterPattern)|| item.getStatus().toLowerCase().contains(filterPattern)) {
                    filteredList.add(item);
                }
            }
        }

        FilterResults results = new FilterResults();
        results.values = filteredList;

        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mailDataModelList.clear();
        mailDataModelList.addAll((List) results.values);
        notifyDataSetChanged();
    }
};
public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
    this.onLoadMoreListener = mOnLoadMoreListener;
}

}

RT16
  • 55
  • 2
  • 11
  • Please add code also. – Abhinav Suman Jan 21 '19 at 11:36
  • please check now – RT16 Jan 21 '19 at 11:38
  • use google's [paging](https://developer.android.com/topic/libraries/architecture/paging.html) support library – pskink Jan 21 '19 at 11:42
  • but this is almost done now ,data is loaded page by page only issue is after the adapter is appended with new data after a new request the recycler view the page is refreshed and it shows the first data – RT16 Jan 21 '19 at 11:47
  • when you add the items to your list called `notifyDataSetChanged()` method instead of this `notifyItemInserted(data.size());` update your list and then call this method `notifyDataSetChanged()`. – Tanveer Munir Jan 21 '19 at 12:01
  • it's not working @TanveerMunir it still takes me to the top, Its not showing me the latest data i have to again scroll down to the bottom to see the newly loaded data – RT16 Jan 21 '19 at 12:16
  • use `paging` library and your problems will be solved in half an hour, otherwise you will fight with your adapter for another couple of days – pskink Jan 21 '19 at 12:18
  • ok will try ..@pskink – RT16 Jan 21 '19 at 12:18
  • @user10390792 where you can add the items in your list after getting new items?? – Tanveer Munir Jan 21 '19 at 12:26
  • Also added my adapter code and also updated the Mainactivity code – RT16 Jan 21 '19 at 12:34
  • I think you are removing the data from the list and then reset the data in the list again that's it shows you from the top. if you set the data firstly don't remove it just add new data in previous list and update adapter. @user10390792 – Tanveer Munir Jan 21 '19 at 12:34
  • Can u please show how to update the adapter .....i tried today morning but it was replacing the adapter data and showing only 10 items per scroll. – RT16 Jan 21 '19 at 12:38
  • @user10390792 I post the answer how can you update your list and the update your adapter. – Tanveer Munir Jan 21 '19 at 13:04

2 Answers2

0

You can update your adapter whenever you get new items lists called this method getting list of MailDataModel from response pass to this method.

UPDATE ADAPTER

public void updateAdapter(List<MailDataModel> mailDataModelList){
 for (MailDataModel mailDataModel : mailDataModelList) {
                data.add(mailDataModel);
            }
            mAdapter.setLoaded();
            mAdapter.notifyDataSetChanged();
}
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
  • Getting java.util.ConcurrentModificationException which is pointing out to the line for (MailDataModel mailDataModel : mailDataModelList) { – RT16 Jan 22 '19 at 05:48
  • @user10390792 `ConcurrentModificationException` occurs when you modify the list (by adding or removing elements) while traversing a list with `Iterator` so when you get the response assigns to a new ArrayList and send to this method [please have look at it](https://stackoverflow.com/questions/6866238/concurrent-modification-exception-adding-to-an-arraylist) – Tanveer Munir Jan 22 '19 at 07:00
0

Please try below code, might it help

public void updateAdapter(List<MailDataModel> mailDataModelList){
    data.clear();
    for (int i = mailDataModelList.size(); i <= 0 ; i--) {
        data.add(mailDataModel);  
    }
    mAdapter.setLoaded();
    mAdapter.notifyDataSetChanged();
}

I have just claen previous data and adding new data in reverse order so that latest one get on top.

SAURABH_12
  • 2,262
  • 1
  • 19
  • 19