I created a RecyclerView for my application and trying to find a way to stop huge requests, I want to get the results 10 by 10 with a progress bar. My server will send the results 10 by 10 on every scrolldown to last item.
public class ProfileActivityFriendsList extends AppCompatActivity {
String[] names={"Ander Herera","David De Gea","Michael Carrick","Juan Mata","Diego Costa","Oscar"};
String[] positions={"Midfielder","GoalKeeper", "Midfielder","Playmaker","Striker","Playmaker"};
String[] urls={"Midfielder","GoalKeeper", "Midfielder","Playmaker","Striker","Playmaker"};
int[] images = {R.mipmap.ic_launcher,R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_friends_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//GET RECYCLER
RecyclerView rv = (RecyclerView) findViewById(R.id.myRecycler);
//SET LAYOUT
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setItemAnimator(new DefaultItemAnimator());
//ADAPTER
MyAdapter adapter = new MyAdapter(this, names, positions, urls, images);
rv.setAdapter(adapter);
}
}
Here is my adapter class.
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
Context c;
String[] names;
String[] positions;
String[] urls;
int[] img;
public MyAdapter(Context ctx, String[] names, String[] positions,String[] urls, int[] images){
this.c=ctx;
this.names = names;
this.positions = positions;
this.img = images;
this.urls = urls;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_list_model,null);
MyHolder holder = new MyHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyHolder holder, final int position) {
holder.imageView.setImageResource(img[position]);
holder.name.setText(names[position]);
holder.desc.setText(positions[position]);
holder.serItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
Snackbar.make(v,positions[position]+" "+positions[position],Snackbar.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return names.length;
}
}
and this it the holder.
public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CircleImageView imageView;
TextView name;
TextView desc;
ItemClickListener itemClickListener;
public MyHolder(View itemView) {
super(itemView);
imageView = (CircleImageView) itemView.findViewById(R.id.circleImageView1);
name = (TextView) itemView.findViewById(R.id.textName);
desc = (TextView) itemView.findViewById(R.id.textDesc);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v,getLayoutPosition());
}
public void serItemClickListener(ItemClickListener ic){
this.itemClickListener = ic;
}
}
I watched lots of tutorials and couldn't find a clear solution, I think there are some ways to do it but I don't know is my code available to add it?