但我没有得到我想要的结果.我需要得到这个:
你看,我已经把这个职业分组了,所有职业都有它自己的偶像.所以我现在将包含我的recyclerView代码.我怎么能在图像中做到?如果我做错了什么,请告诉我,不要给予downvote.
这是项目类.
public class Rec_Items {
private int image;
private String fullName;
private String profession;
public Rec_Items(int image, String fullName, String profession) {
this.image = image;
this.fullName = fullName;
this.profession = profession;
}
public int getImage() {
return image;
}
public String getFullName() {
return fullName;
}
public String getProfession() {
return profession;
}
}
现在这里的适配器是它的ViewHolder.
public class SupportersAdapter extends RecyclerView.Adapter<SupportersAdapter.MyViewHolder> {
Context context;
List<Supporters_item> list = new ArrayList<>();
public SupportersAdapter(Context context, List<Supporters_item> list) {
this.context = context;
this.list = list;
}
@Override
public SupportersAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(context).inflate(R.layout.supporters_view_item, parent, false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(SupportersAdapter.MyViewHolder holder, int position) {
holder.imageView.setImageResource(list.get(position).getImage());
holder.fullName.setText(list.get(position).getFullName());
holder.profession.setText(list.get(position).getProfession());
if (list.get(position).getProfession().equals("Actress")) {
holder.icon.setImageResource(R.drawable.medical);
}
if (list.get(position).getProfession().equals("Builder")) {
holder.icon.setImageResource(R.drawable.builderkey);
}
if (list.get(position).getProfession().equals("Designer")) {
holder.icon.setImageResource(R.drawable.shopping);
}
if (list.get(position).getProfession().equals("Programmer")) {
holder.icon.setImageResource(R.drawable.education);
}
if (position > 0) {
if (!list.get(position).getProfession().substring(0,1).equals(list.get(position-1).getProfession().substring(0, 1))) {
holder.view.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.VISIBLE);
}
}
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView, icon;
TextView fullName, profession;
View view;
public MyViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.support_picture);
fullName = (TextView) itemView.findViewById(R.id.fName);
profession = (TextView) itemView.findViewById(R.id.profession);
icon = (ImageView) itemView.findViewById(R.id.icon);
view = itemView.findViewById(R.id.bottomLine);
}
}
}
我已经在onBindViewHolder()中完成了组部分,所以在那里观看,我得到的不是我想要的结果,我想获得图像结果.
这是MainActivity类.
public class MainActivity extends AppCompatActivity {
private List<Rec_Items> list;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.supporters_recycler);
add();
Adapter adapter = new Adapter(this, list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
public void add(){
list = new ArrayList<>();
list.add(new Rec_Items(R.drawable.pfl_img, "Name Surname", "Actress"));
list.add(new Rec_Items(R.drawable.pfl_img, "Name Surname", "Programmer"));
list.add(new Rec_Items(R.drawable.pfl_img, "Name Surname", "Builder"));
list.add(new Rec_Items(R.drawable.pfl_img, "Name Surname", "Designer"));
list.add(new Rec_Items(R.drawable.pfl_img, "Name Surname", "Actress"));
list.add(new Rec_Items(R.drawable.pfl_img, "Name Surname", "Programmer"));
list.add(new Rec_Items(R.drawable.pfl_img, "Name Surname", "Builder"));
list.add(new Rec_Items(R.drawable.pfl_img, "Name Surname", "Designer"));
}
}
如何修改我的代码,如何更改以获取分组的recyclerView?
最佳答案 在设置适配器之前,您可以按专业字母顺序对列表进行排序:
List<Rec_Items> sortedList = new ArrayList();
if (list.size() > 0) {
Collections.sort(list, new Comparator<Rec_Items>() {
@Override
public int compare(final Rec_Items object1, final Rec_Items object2) {
return object1.getProfession().compareTo(object2.getProfession());
}
});
}
// SORTED LIST
Adapter adapter = new Adapter(this, sortedList);
要显示每个部分的除数(线),你可以将它添加到xml设计上,因为android:visibility =“gone”并显示前一项专业的首字母与当前的不同:
//Inside onBindViewHolder method:
if(position > 0){
// [----------------PREVIOUS ITEM-----------------] [------------------CURRENT ITEM------------------]
if(!list.get(position-1).getProfession().substring(0,1).equals(list.get(position).getProfession().substring(0,1))){
//Example: holder.getDivisor().setVisibility(View.VISIBLE);
}
}
对于图标部分,您可以使用相同的逻辑.让它隐藏在xml设计中,并在项目是列表中的第一个项目时显示它,或者如果前一个项目专业的首字母不同,也就是说,当前项目是其部分的第一个.您可以检查专业名称以设置相应的图像.
快乐的编码!