2

I want to have a recyclerview to load more data on scrolling. It was working perfectly with loading more data. Then i extended code to show progressbar at the end to show loading. But it not working properly. It shows two many progressbar at a time and also my loading data is not proper. Here, i have implemented my code. Please help me.

MainActivity.java

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
MyAdapter adapter;
final ArrayList<String > data = new ArrayList<>();
int end=20;
int start=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    LinearLayoutManager lm = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(lm);

    adapter = new MyAdapter(data);
    recyclerView.setAdapter(adapter);

    load_data();

    recyclerView.setOnScrollListener(new EndLessScroll(lm) {
        @Override
        protected void loadMore(int current_page) {

                start = end + 1;
                end = end + 10;
                load_data();

        }
    });
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

private void load_data() {
    Handler handler = new Handler();
    data.add(null);
    adapter.notifyItemInserted(data.size()-1);
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            data.remove(data.size() - 1);
            adapter.notifyItemRemoved(data.size());
            //wait
            for (int i = start; i <= end; i++) {
                data.add("Item-" + i);
                adapter.notifyItemInserted(data.size());
            }
            EndLessScroll.setLoaded();
        }
    },2000);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
   }
}

MyAdapter.Java

/**
 * Created by nteam on 13/10/15.
 */
public class MyAdapter extends   RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<String> data;
final int VIEW_ITEM=1;
final int VIEW_PROG=0;

public  MyAdapter(ArrayList<String> data){
    this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder vh=null;
    if(viewType == VIEW_ITEM){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_card,parent,false);
        vh = new DataViewHolder(view);
    }else{
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_indicator,parent,false);
        vh = new ProgressViewHolder(view);
    }
    return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if(holder instanceof DataViewHolder) {
        ((DataViewHolder) holder).title.setText(data.get(position));
    }else{
        ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
    }
}

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

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

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView title;
    public ViewHolder(View itemView) {
        super(itemView);

        title = (TextView) itemView.findViewById(R.id.title);
    }
}

public class DataViewHolder extends RecyclerView.ViewHolder{
    public TextView title;
    public DataViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.title);
    }
}

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

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

}

EndlessScroll.java

/**
 * Created by nteam on 13/10/15.
 */
public abstract class EndLessScroll extends RecyclerView.OnScrollListener {
    public static String TAG = EndLessScroll.class.getSimpleName();

    private int prevTotal = 0;
    public static boolean isLoading = true;
    private int visibleThresold = 10;
    int firstVisisbleItem,visibleItemCount,totalItemCount;

    private int current_page=1;
    private LinearLayoutManager linearLayoutManager;

    public EndLessScroll(LinearLayoutManager linearLayoutManager){
        this.linearLayoutManager = linearLayoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = linearLayoutManager.getItemCount();
        firstVisisbleItem = linearLayoutManager.findFirstVisibleItemPosition();

        if(isLoading){
            if(totalItemCount>prevTotal){
                isLoading=false;
                prevTotal = totalItemCount;
            }

        }else if(!isLoading && (totalItemCount-visibleItemCount)<= (firstVisisbleItem+visibleThresold)) {
            //end reached

            current_page++;
            loadMore(current_page);
            isLoading = true;
        }
    }

    public static void setLoaded(){
        isLoading=false;
    }
    protected abstract void loadMore(int current_page);
}
Luciano van der Veekens
  • 5,586
  • 4
  • 21
  • 29
H Raval
  • 1,955
  • 15
  • 36

1 Answers1

2

I haven't actually run the code, but at least this lines:

current_page++;
loadMore(current_page);
isLoading = true;

should be:

isLoading = true;
current_page++;
loadMore(current_page);

Certainly this must mess up the progress bar and the items since several loads can be launch concurrently.

T.Gounelle
  • 5,603
  • 1
  • 19
  • 31