但我没有得到我想要的结果。我需要得到这个:
你看我已经把这个职业分组了,所有职业都有它自己的偶像。所以我现在将包含我的recyclerView
代码。我怎么能在图像中做到?如果我做错了什么,请告诉我,不要放弃投票。
这是项目类。
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;
}
}
现在这里adapter
的{{1}}。
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"));
}
}
需要更改哪些内容?
答案 0 :(得分:1)
您可以在设置适配器之前按专业字母顺序对列表进行排序:
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设计中,并在项目是列表中的第一个项目时显示它,或者如果前一个项目专业的首字母不同,也就是说,当前项目是其中的第一个项目。您可以查看专业名称以设置相应的图像。
快乐的编码!
答案 1 :(得分:0)
这是解决方案
@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());
//call button listener
holder.buttonCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Whom you wanna call?", Toast.LENGTH_SHORT).show();
}
});
//setting icons for each profession
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.spanner);
}
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);
}
//setting first icon always visible
if (position == 0) {
holder.icon.setVisibility(View.VISIBLE);
}
//setting bottom view visible for dividing
if (position + 1 < getItemCount()) {
if (!list.get(position).getProfession().substring(0, 1).equals
(list.get(position + 1).getProfession().substring(0, 1))) {
holder.view.setVisibility(View.VISIBLE);
}
}
//setting icon visible for group of profession
if (position > 0) {
if (!list.get(position).getProfession().substring(0, 1).equals
(list.get(position - 1).getProfession().substring(0, 1))) {
holder.icon.setVisibility(View.VISIBLE);
}
}
}
@Override
public int getItemCount() {
return list.size();
}