如何以不同的方式在两个活动中使用相同的适配器

时间:2017-04-20 17:34:05

标签: android

是否可以在两个活动中为不同的不同工作调用相同的适配器 这是我的RecyclerAdapter: 从两个活动调用时只有区别在于:从Activity1.java开始:

        holder.Name.setText(arrayList.get(position).getName());

来自Activity2.java

        holder.Name.setText(arrayList.get(position).getVehicle());

所有其他代码是相同的,我如何使用相同的适配器进行不同的工作。

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

private ArrayList<Contact> arrayList= new ArrayList<>();

public RecyclerAdapter(ArrayList<Contact> arrayList){

    this.arrayList= arrayList;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);

    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder (MyViewHolder holder, int position) {

    holder.Name.setText(arrayList.get(position).getName());
    int sync_status = arrayList.get(position).getSync_status();
    if(sync_status== DbContact.SYNC_STATUS_OK){
        holder.Sync_Status.setImageResource(R.drawable.success);

    }
    else {
        holder.Sync_Status.setImageResource(R.drawable.stopwatch);
    }

}

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

public static class MyViewHolder extends RecyclerView.ViewHolder{

    ImageView Sync_Status;
    TextView Name;

    public MyViewHolder(View itemView) {
        super(itemView);
        Sync_Status=(ImageView)itemView.findViewById(R.id.imgSync);
        Name=(TextView)itemView.findViewById(R.id.txtName);
    }
}
}

1 个答案:

答案 0 :(得分:2)

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

private ArrayList<Contact> arrayList= new ArrayList<>();
private int whichActivity;

public RecyclerAdapter(ArrayList<Contact> arrayList, int activity){
    whichActivity = activity;
    this.arrayList= arrayList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder (MyViewHolder holder, int position) {
    if(whichActivity == 0) {
        holder.Name.setText(arrayList.get(position).getName());
    }
    else {
        holder.Name.setText(arrayList.get(position).getVehicle());
    }
    int sync_status = arrayList.get(position).getSync_status();
    if(sync_status== DbContact.SYNC_STATUS_OK){
        holder.Sync_Status.setImageResource(R.drawable.success);

    }
    else {
        holder.Sync_Status.setImageResource(R.drawable.stopwatch);
    }

}

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

public static class MyViewHolder extends RecyclerView.ViewHolder{

    ImageView Sync_Status;
    TextView Name;

    public MyViewHolder(View itemView) {
        super(itemView);
        Sync_Status=(ImageView)itemView.findViewById(R.id.imgSync);
        Name=(TextView)itemView.findViewById(R.id.txtName);
    }
}
}

试试这段代码。现在,当您在 Activity1中创建RecyclerAdapter时,调用new RecyclerAdapter(arrayList,0),当您在 Activity2中创建RecyclerAdapter时,请调用new RecyclerAdapter(arrayList,1)。您只是将一个变量传递给构造函数,以便您的适配器知道它所处的活动,并且可以根据活动运行不同的逻辑。