'Load More RecyclerView and Bottom ProgressBar

How to implement setOnScrollListener in RecyclerView

Hi I am new in android , i have a query regarding lode more on infinite scroll , Please give me some ways to resolve my Query .

I have this function mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() and i want to put some integer like 1 , 2 and after that next 20 data will automatically shown form server .

My code is shown below :-

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.latest_upload_ten, container, false);
        handler = new Handler();
        recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        progressBar = (ProgressBar) rootView.findViewById(R.id.progress_bar);
        return rootView;
    }


    public static void setAdapter(final Context context) {
        if (recyclerView != null)
            recyclerView.setVisibility(View.VISIBLE);
        Settings.loading = false;
        Utils.e("MessageFragment 106", "setAdapter ok");
        mAdapter = new TopRankedAdapter(adapterList, recyclerView, R.layout.video_items, 1);
        Utils.e("MessageFragment 109", "setAdapter ok");
        recyclerView.setAdapter(mAdapter);
        Utils.e("MessageFragment 109", "setAdapter ok");

        mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                //add null , so the adapter will check view_type and show progress bar at bottom
                adapterList.add(null);
                mAdapter.notifyItemInserted(adapterList.size() - 1);
                ++pageNumber;
                Log.e("ee","dd"+pageNumber);

           }

        });


    }

    public static void loadData(Context context, List<HashMap<String, Object>> viewList, String mode) {
        Utils.e("TopRankedFragment144", "loadData");
        Utils.e("mode= ", "www "+mode);
        if (viewList != null && viewList.size() > 0) {
            try {
                if (adapterList == null) {
                    Utils.e("TopRankedFragment148", "TopRankedFragmentList else");
                    adapterList = new ArrayList<OfficeData>();

                } else {
                    adapterList.clear();
                }
                for (int i = 0; i < viewList.size(); i++) {
                    HashMap<String, Object> mp = new HashMap<String, Object>();
                    mp = viewList.get(i);
                    Utils.e("TopRankedFragment157", "i " + i);
                    if (!adapterList.contains(mp))
                        adapterList.add(new OfficeData(mp, 1));
                }
            } catch (Exception e) {
                Utils.e("TopRankedFragment 166", "Exception======================Exception======================Exception");
                e.printStackTrace();
            } finally {
                setAdapter(context);
        }}
    }

Thanks for your help :)



Solution 1:[1]

Source :gist and CodePath

   /**
     *  A child class shall subclass this Adapter and 
     *  implement method getDataRow(int position, View convertView, ViewGroup parent),
     *  which supplies a View present data in a ListRow.
     *  
     *  This parent Adapter takes care of displaying ProgressBar in a row or 
     *  indicating that it has reached the last row.
     * 
     */
    public abstract class GenericAdapter<T> extends BaseAdapter {
        
        // the main data list to save loaded data
        protected List<T> dataList;
        
        protected Activity mActivity;
        
        // the serverListSize is the total number of items on the server side,
        // which should be returned from the web request results
        protected int serverListSize = -1;
        
        // Two view types which will be used to determine whether a row should be displaying 
        // data or a Progressbar
        public static final int VIEW_TYPE_LOADING = 0;
        public static final int VIEW_TYPE_ACTIVITY = 1;
        
        
        public GenericAdapter(Activity activity, List<T> list) {
            mActivity = activity;
            dataList = list;
        }
        
        
        public void setServerListSize(int serverListSize){
            this.serverListSize = serverListSize;
        }
    
    
    /**
         * disable click events on indicating rows
         */
        @Override
        public boolean isEnabled(int position) {
             
            return getItemViewType(position) == VIEW_TYPE_ACTIVITY;
        }
    
        /**
         * One type is normal data row, the other type is Progressbar
         */
        @Override
        public int getViewTypeCount() {
            return 2;
        }
        
        
        /**
         * the size of the List plus one, the one is the last row, which displays a Progressbar
         */
        @Override
        public int getCount() {
            return dataList.size() + 1;
        }
    
        
        /**
         * return the type of the row, 
         * the last row indicates the user that the ListView is loading more data
         */
        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return (position >= dataList.size()) ? VIEW_TYPE_LOADING
                    : VIEW_TYPE_ACTIVITY;
        }
    
        @Override
        public T getItem(int position) {
            // TODO Auto-generated method stub
            return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? dataList
                    .get(position) : null;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return (getItemViewType(position) == VIEW_TYPE_ACTIVITY) ? position
                    : -1;
        }
    
        /**
         *  returns the correct view 
         */
        @Override
        public  View getView(int position, View convertView, ViewGroup parent){
            if (getItemViewType(position) == VIEW_TYPE_LOADING) {
                // display the last row
                return getFooterView(position, convertView, parent);
            }
            View dataRow = convertView;
            dataRow = getDataRow(position, convertView, parent);
            
            return dataRow;
        };
        
        /**
         * A subclass should override this method to supply the data row.
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        public abstract View getDataRow(int position, View convertView, ViewGroup parent);
    
        
        /**
         * returns a View to be displayed in the last row.
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        public View getFooterView(int position, View convertView,
                ViewGroup parent) {
            if (position >= serverListSize && serverListSize > 0) {
                // the ListView has reached the last row
                TextView tvLastRow = new TextView(mActivity);
                tvLastRow.setHint("Reached the last row.");
                tvLastRow.setGravity(Gravity.CENTER);
                return tvLastRow;
            }
            
            View row = convertView;
            if (row == null) {
                row = mActivity.getLayoutInflater().inflate(
                        R.layout.progress, parent, false);
            }
    
            return row;
        }
    
    }

or You can try this one

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel