recyclerview中的多个布局

ht4b089n  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(429)

我想做一个类似聊天的表示,其中我有三个布局:mainactivity:我有一个recyclerview布局\u sent:这是发送消息的布局\u received:这是接收消息的布局
我使用的是mysql数据库:获取数据的查询是:

Select cr.user_id, cr.reply, d.griev_desc, c.user_one, c.user_two from grievance_details d , conversation_reply cr, conversation c where d.complain_id = 11 and cr.complain_id = d.complain_id and c.complain_id = 11

这是给我的输出:

我的android适配器代码是:

public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

String user_one;
String user_two;
ArrayList<String> user_id_array;
ArrayList<String> griev_desc_array;
ArrayList<String> reply_array_emp;
ArrayList<String> reply_array_hr;
Context c;

public DifferentRowAdapter(Context c, String user_one, String user_two,
                           ArrayList<String> user_id_array, ArrayList<String> griev_desc_array,
                           ArrayList<String> reply_array_emp, ArrayList<String> reply_array_hr) {
    this.c =c ;
    this.user_id_array = user_id_array;
    this.user_one = user_one;
    this.user_two = user_two;
    this.griev_desc_array = griev_desc_array;
    this.reply_array_emp = reply_array_emp;
    this.reply_array_hr = reply_array_hr;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;

    System.out.println("top of for onCreateView");
    //for(String s : user_id_array)
    for(int i =0;i<user_id_array.size();i++)
    {
        System.out.println("in side for");
        if(user_id_array.get(i).equals(user_one)){
        //if(!s.equals(user_one)){
            //System.out.println("in side if onCreateViewHolder"+s);
            System.out.println("in side if onCreateViewHolder"+user_id_array.get(i));
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_emp_conv, parent, false);
            return new EmployeeViewHolder(view);
        }else if(user_id_array.get(i).equals(user_two)){
            //System.out.println("in side else if onCreateViewHolder"+s);
            System.out.println("in side if onCreateViewHolder"+user_id_array.get(i));
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_hr_conv, parent, false);
            return new HrViewHolder(view);
        }
        else
        {
            //System.out.println("in side else onCreateViewHolder"+s);
            System.out.println("in side if onCreateViewHolder"+user_id_array.get(i));
        }
    }
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    System.out.println("top of for");
    //for(String s : user_id_array)
    for(int i =0;i<user_id_array.size();i++)
    {
        System.out.println("in side for");
        if(user_id_array.get(i).equals(user_one)){
            System.out.println("in side if onBindViewHolder"+ user_id_array.get(i));
            //System.out.println("in side if onBindViewHolder"+s);
            //holder.employee_conversation.setText(reply_array_emp.get(position));
            ((EmployeeViewHolder)holder).employee_conversation.setText(reply_array_emp.get(position));

        }else if(user_id_array.get(i).equals(user_two)){
            //System.out.println("in side else if onBindViewHolder"+s);
            System.out.println("in side if onBindViewHolder"+user_id_array.get(i));
            ((HrViewHolder) holder).hr_conversation.setText(reply_array_hr.get(position));
        }
        else
        {
            //System.out.println("in side else onBindViewHolder"+s);
        }
    }

}

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

}
但是,我仍然无法对这两种布局进行充气,因为我在以下方面遇到了一个错误:

((HrViewHolder) holder).hr_conversation.setText(reply_array_hr.get(position));

说打字不可能。
其他两个持有人类别为:

public class HrViewHolder extends RecyclerView.ViewHolder {
TextView hr_conversation;

public HrViewHolder(View itemView) {
    super(itemView);
    hr_conversation =(TextView) itemView.findViewById(R.id.hr_conversation);
    System.out.println("in hrViewHolder");
}

}

public class EmployeeViewHolder extends RecyclerView.ViewHolder {

TextView employee_conversation;

public EmployeeViewHolder(View itemView) {
    super(itemView);
    employee_conversation=(TextView) itemView.findViewById(R.id.employee_conversation);
    System.out.println("in employeeViewHolder");
}

}
我将代码修改为:

public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

String user_one;
String user_two;
ArrayList<String> user_id_array;
ArrayList<String> griev_desc_array;
ArrayList<String> reply_array_emp;
ArrayList<String> reply_array_hr;
Context c;

public DifferentRowAdapter(Context c, String user_one, String user_two,
                           ArrayList<String> user_id_array, ArrayList<String> griev_desc_array,
                           ArrayList<String> reply_array_emp, ArrayList<String> reply_array_hr) {
    this.c =c ;
    this.user_id_array = user_id_array;
    this.user_one = user_one;
    this.user_two = user_two;
    this.griev_desc_array = griev_desc_array;
    this.reply_array_emp = reply_array_emp;
    this.reply_array_hr = reply_array_hr;
}

@Override
public int getItemViewType(int position) {
    for(String s: user_id_array)
    {
        if(s.equals(user_one))
        {
            return Integer.parseInt(user_one);
        }
        else if(s.equals(user_two))
            return Integer.parseInt(user_two);
        else
            return 3;
    }

    return 0;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;

    System.out.println("top of for onCreateView");

    switch (viewType)
    {
        case 1 :
            System.out.println("in side case 1 onCreateViewHolder viewType     "+ viewType);
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_emp_conv, parent, false);
            return new EmployeeViewHolder(view);
        case 2:
            System.out.println("in side case 2 onCreateViewHolder viewType     "+ viewType);
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_hr_conv, parent, false);
            return new HrViewHolder(view);

    }
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    System.out.println("top of for");
    //for(String s : user_id_array)
    for(int i =0;i<user_id_array.size();i++)
    {
        System.out.println("in side for");
        if(user_id_array.get(i).equals(user_one)){
            System.out.println("in side if onBindViewHolder"+ user_id_array.get(i));
            //System.out.println("in side if onBindViewHolder"+s);
            //holder.employee_conversation.setText(reply_array_emp.get(position));
            //((EmployeeViewHolder)holder).employee_conversation.setText(reply_array_emp.get(position));

        }else if(user_id_array.get(i).equals(user_two)){
            //System.out.println("in side else if onBindViewHolder"+s);
            System.out.println("in side if onBindViewHolder"+user_id_array.get(i));
            //((HrViewHolder) holder).hr_conversation.setText(reply_array_hr.get(position));
        }
        else
        {
            //System.out.println("in side else onBindViewHolder"+s);
        }
    }

}

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

}

nhjlsmyf

nhjlsmyf1#

首先,数组中不需要for循环,因为传递了视图的位置,所以可以安全地将其修改为

@Override
public int getItemViewType(int position) {
        if(user_id_array.get(position).equals(user_one))
            return 1;
        else if(user_id_array.get(position).equals(user_two))
            return 2;
        else
            return 3;
}

这同样适用于 onBindViewHolder ,在那里你有 ViewHolder 示例作为参数传递,因此您需要在运行时检查其类型以了解要绑定到的布局(使用 instanceof 操作员)。

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    if (holder instanceof EmployeeViewHolder) {
        System.out.println("in side for");
        System.out.println("in side if onBindViewHolder"+ user_id_array.get(position));
            //System.out.println("in side if onBindViewHolder"+s);
            //holder.employee_conversation.setText(reply_array_emp.get(position));
            ((EmployeeViewHolder)holder).employee_conversation.setText(reply_array_emp.get(position));

    } else if(holder instanceof HrViewHolder){
        //System.out.println("in side else if onBindViewHolder"+s);
        System.out.println("in side if onBindViewHolder"+user_id_array.get(position));
        ((HrViewHolder) holder).hr_conversation.setText(reply_array_hr.get(position));
    } else {
        //System.out.println("in side else onBindViewHolder"+s);
    }
}

希望这有帮助!

mrfwxfqh

mrfwxfqh2#

重写适配器中的getitemviewtype()。然后基于oncreateviewholder中的viewtype参数返回viewholder。
我希望下面的链接能帮助您在recycleview适配器中进行多布局:

相关问题