我为我的应用程序创建了一个RecyclerView,并试图找到一种停止大量请求的方法,我想要使用进度条每次获取10个结果。我的服务器将在每次滚动到最后一个项目时,按照10个为一组的方式发送结果。
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);
}
}
这是我的适配器类。
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;
}
}
这是占位符。
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;
}
}
我看了很多教程,但是找不到清晰的解决方案。我认为有一些方法可以实现它,但我不知道我的代码是否可用于添加它。