Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load more data in RecyclerView when Scrolling on Android

Tags:

json

android

I want develop android application for one website. I read website posts from json and show its in RecyclerView every 10 posts and when user scrolling on RecyclerView show more 10 post and go to end!
I am amateur and i write below codes, but I don't know to to read more data when scrolling and go to next page! (I want show any 10 posts in any pages).
Json link : JSON LINK

AsyncTask codes:

public class MainDataInfo {
    private Context mContext;
    private String ServerAddress = ServerIP.getIP();

    public void getMainDataInfo(Context context) {
        mContext = context;
        new getInfo().execute(ServerAddress + "page=1");
    }

    private class getInfo extends AsyncTask<String, Void, String> {
        EventBus bus = EventBus.getDefault();
        private String ou_response;
        private List<MainDataModel> infoModels;

        @Override
        protected void onPreExecute() {
            CustomProcessDialog.createAndShow(mContext);
            infoModels = new ArrayList<>();
        }

        @Override
        protected String doInBackground(String... params) {
            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder()
                    .url(ServerAddress + "page=1")
                    .cacheControl(CacheControl.FORCE_NETWORK)
                    .build();

            Response response;
            try {
                response = client.newCall(request).execute();
                ou_response = response.body().string();
                response.body().close();
                if (ou_response != null) {
                    try {
                        JSONObject postObj = new JSONObject(ou_response);
                        JSONArray postsArray = postObj.optJSONArray("posts");
                        infoModels = new ArrayList<>();

                        for (int i = 0; i <= infoModels.size(); i++) {
                            JSONObject postObject = (JSONObject) postsArray.get(i);
                            JSONObject images=postObject.optJSONObject("thumbnail_images");
                            JSONObject imagesPair=images.optJSONObject("medium");

                            int id = postObject.getInt("id");
                            String title = postObject.getString("title");
                            String content = postObject.getString("content");
                            String thumbnail = imagesPair.getString("url");
                            Log.d("Data", "Post id: " + id);
                            Log.d("Data", "Post title: " + title);
                            Log.d("Data", "Post title: " + thumbnail);

                            //Use the title and id as per your requirement
                            infoModels.add(new MainDataModel(id, title, content, thumbnail));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ou_response;
        }

        @Override
        protected void onPostExecute(String result) {
            CustomProcessDialog.dissmis();
            if (result != null) {
                bus.post(infoModels);
            }
        }
    }
}

I use this code for show any pages posts : ServerAddress + "page=1"

Adapter codes:

public class MainAdapter2 extends RecyclerView.Adapter {

    private List<MainDataModel> mDateSet;
    private Context mContext;

    private final int VIEW_ITEM = 1;
    private final int VIEW_PROG = 0;

    // The minimum amount of items to have below your current scroll position
    // before loading more.
    private int visibleThreshold = 5;
    private int lastVisibleItem, totalItemCount;
    private boolean loading;
    private OnLoadMoreListener onLoadMoreListener;

    public MainAdapter2(Context context, RecyclerView recyclerView, List<MainDataModel> dataSet) {
        this.mContext = context;
        this.mDateSet = dataSet;

        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {

            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
                    .getLayoutManager();
            recyclerView
                    .addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView recyclerView,
                                               int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                            totalItemCount = linearLayoutManager.getItemCount();
                            lastVisibleItem = linearLayoutManager
                                    .findLastVisibleItemPosition();
                            if (!loading
                                    && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                                // End has been reached
                                // Do something
                                if (onLoadMoreListener != null) {
                                    onLoadMoreListener.onLoadMore();
                                }
                                loading = true;
                            }
                        }
                    });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mDateSet.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder vh;
        if (viewType == VIEW_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.post_card_layout, parent, false);

            vh = new DataViewHolder(v);
        } else {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.progressbar_item, parent, false);

            vh = new ProgressViewHolder(v);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof DataViewHolder) {
            ((DataViewHolder) holder).main_post_title.setText(mDateSet.get(position).getTitle());
            Glide.with(mContext)
                    .load(mDateSet.get(position).getThumbnail())
                    .placeholder(R.drawable.post_image)
                    .crossFade()
                    .into(((DataViewHolder) holder).main_post_image);
            ((DataViewHolder) holder).main_post_content.setText(Html.fromHtml(mDateSet.get(position).getContent()));

        } else {
            ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
        }
    }

    public void setLoaded() {
        loading = false;
    }

    @Override
    public int getItemCount() {
        return mDateSet.size();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public void remove(int position) {
        mDateSet.remove(position);
        notifyItemRemoved(position);
    }

    public void clear() {
        mDateSet.clear();
        notifyDataSetChanged();
    }


    public void add(List<MainDataModel> models) {
        mDateSet.addAll(models);
        notifyDataSetChanged();
    }

    public void update(List<MainDataModel> models) {
        mDateSet.clear();
        mDateSet.addAll(models);
        notifyDataSetChanged();
    }

    public class DataViewHolder extends RecyclerView.ViewHolder {

        private TextView main_post_title, main_post_content;
        private ImageView main_post_image;

        public DataViewHolder(View itemView) {
            super(itemView);

            main_post_title = (TextView) itemView.findViewById(R.id.post_content_title);
            main_post_image = (ImageView) itemView.findViewById(R.id.post_picture_image);
            main_post_content = (TextView) itemView.findViewById(R.id.post_content_text);
        }
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public ProgressViewHolder(View v) {
            super(v);
            progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
        }
    }
}

MainActivity codes:

public class Main_page extends AppCompatActivity {

    private static final long RIPPLE_DURATION = 250;
    private Toolbar toolbar;
    private RelativeLayout root;
    private ImageView menu_image;
    private RecyclerView main_recyclerView;
    private MainAdapter2 mAdaper;
    private List<MainDataModel> dataModels = new ArrayList<MainDataModel>();
    private List<MainDataModel> dataModelsArray;
    private Context context;
    protected Handler handler;
    private RelativeLayout loadLayout;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        handler = new Handler();
        context = getApplicationContext();
        loadLayout = (RelativeLayout) findViewById(R.id.main_empty_layout);
        toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(null);
        }
        LoadData();
        mLayoutManager = new LinearLayoutManager(this);
        // Menu
        root = (RelativeLayout) findViewById(R.id.main_root);
        View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.menu_layout, null);
        root.addView(guillotineMenu);
        menu_image = (ImageView) toolbar.findViewById(R.id.toolbar_logo);
        new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.menu_layout_image), menu_image)
                .setStartDelay(RIPPLE_DURATION)
                .setActionBarViewForAnimation(toolbar)
                .setClosedOnStart(true)
                .build();
        // RecyclerView and setData
        main_recyclerView = (RecyclerView) findViewById(R.id.main_recycler);
        main_recyclerView.setHasFixedSize(true);
        main_recyclerView.setLayoutManager(mLayoutManager);

        mAdaper = new MainAdapter2(this, main_recyclerView, dataModels);
        main_recyclerView.setAdapter(mAdaper);

        mAdaper.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {

            }
        });


    }


    @Subscribe
    public void onEvent(List<MainDataModel> mainInfoModels) {
        mAdaper.add(mainInfoModels);

        if (dataModels.isEmpty()) {
            main_recyclerView.setVisibility(View.GONE);
            loadLayout.setVisibility(View.VISIBLE);

        } else {
            main_recyclerView.setVisibility(View.VISIBLE);
            loadLayout.setVisibility(View.GONE);
        }
    }

    private void LoadData() {
        final MainDataInfo dataInfo = new MainDataInfo();
        dataInfo.getMainDataInfo(this);
    }

for load more data i use Interface class : OnLoadMoreListener

Attention : I know the name of this method is endless! but i am amateur and i don't know to write code in mAdapter.setOnMoreListener to load more data! Please don't give me negative points, help me and i really need you helps! thanks all <3

like image 316
F_Feed Avatar asked Apr 27 '16 06:04

F_Feed


People also ask

How implement load more RecyclerView in android?

How is this implemented? Typically in a simple RecyclerView, we load elements to the adapter from a Data Structure. In order to show the loading icon view at the bottom of the RecyclerView, we need to first add a NULL element to the end of the Data Structure.

How can show all items in RecyclerView without scrolling?

How can show all items in RecyclerView without scrolling? In RecyclerView use android:nestedSrollingEnabled="false" and use NestedScrollView as a parent Scroll View.

Is RecyclerView scrollable android?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .


1 Answers

Add onLoadMoreListener to your adapter,

  adapter.setOnLoadMoreListener(onLoadMoreListener);

Then Implement below code in onLoadMore():

OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
    @Override
    public void onLoadMore() {
        //Here adding null object to last position,check the condition in getItemViewType() method,if object is null then display progress
        dataModels.add(null);
        adapter.notifyItemInserted(dataModels.size() - 1);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dataModels.remove(dataModels.size() - 1);
                adapter.notifyItemRemoved(dataModels.size());

                // Call you API, then update the result into dataModels, then call adapter.notifyDataSetChanged(). 
                //Update the new data into list object
                loadData();
                adapter.notifyDataSetChanged();
                loading = false;
            }
        }, 1000);
    }
};

Your method:

      private void LoadData() {
             MainDataInfo dataInfo = new MainDataInfo();
            // here getMainDataInfo() should return the server response
             dataInfo.getMainDataInfo(this);
      }
like image 73
Saritha G Avatar answered Dec 22 '22 01:12

Saritha G