在两个recyclerView列表之间交换项目

时间:2015-07-21 05:45:43

标签: android android-recyclerview

我有i_have的两个片段to_buypageView。 它们都有使用recyclerView创建的项目列表。 对于这两个片段,我分别有两个不同的适配器infoAdapterinfo2Adapter

我在每个项目的图标(图片)上设置了onClickListner,点击后会删除项目

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView title;
    ImageView icon;
    TextView amt;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.i_have_row_name);
        icon = (ImageView) itemView.findViewById(R.id.i_have_row_image);
        amt = (TextView) itemView.findViewById(R.id.i_have_row_amount);
        icon.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        delete(getPosition());        
    }
}

删除方法如下

public void delete(int position){
    data.remove(position);
    notifyItemRemoved(position);
}

现在我想要的是,每当项目从i_have中删除时,它都会自动添加到to_buy和反之亦然。

我在add class

的适配器中创建了一个to_buy方法
public void add(String s){
    Information2 information2 = new Information2();
    information2.name2 = s;
    information2.iconId2 = R.drawable.potatoes;
    information2.amount = "1KG";
    data2.add(0,information);
    notifyDataSetChanged();
}

但我很困惑如何从i_have类的适配器调用此方法。因为它是一个非静态方法,我应该使用哪个对象来调用add方法?我创建了另一个类的对象,但我不确定我应该在哪里引用它?

[编辑]

我实现了yshahsk在答案中所说的内容,但我需要在MyViewHolder调用中添加一个参数。

onCreateViewHolder()如下:

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.i_have_row, parent, false);

    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

[EDIT2]

根据要求

这是i_have片段的适配器文件infoAdapter

package example.srujan.com.exapp4;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;

/**
 * Created by Srujan on 14-07-2015.
 */
public class infoAdapter extends     RecyclerView.Adapter<infoAdapter.MyViewHolder> {

private LayoutInflater inflater;
List<Information> data = Collections.emptyList() ;


public infoAdapter(Context context, List<Information> data){
    inflater = LayoutInflater.from(context);
    this.data=data;
}

public void delete(int position){
    data.remove(position);
    notifyItemRemoved(position);
}

public void add(String s){
    Log.d("TAG",data.size()+" String received: "+s);
    Information information = new Information();
    information.name = s;
    information.iconId = R.drawable.potatoes;
    information.amount = "1KG";
    data.add(0,information);
    notifyDataSetChanged();
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.i_have_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current = data.get(position);
    holder.title.setText(current.name);
    holder.amt.setText(current.amount);
    holder.icon.setImageResource(current.iconId);

}

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

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView title;
    ImageView icon;
    TextView amt;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.i_have_row_name);
        icon = (ImageView) itemView.findViewById(R.id.i_have_row_image);
        amt = (TextView) itemView.findViewById(R.id.i_have_row_amount);
        icon.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        delete(getPosition());
        //Log.d("TAG3","String passed 2: Tametu");
        //add("Tomatu");
    }
}
}

这是我的另一个适配器info2Adapter,这个适用于to_buy

package example.srujan.com.exapp4;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

/**
 * Created by Srujan on 19-07-2015.
 */
public class info2Adapter extends     RecyclerView.Adapter<info2Adapter.MyViewHolder> {

private LayoutInflater inflater;
List<Information2> data2 = Collections.emptyList() ;


public info2Adapter(Context context, List<Information2> data){
    inflater = LayoutInflater.from(context);
    this.data2=data;
}

//TEST PURPOSE
public info2Adapter() {

}

//    public void addToBuy(Information2 i){
//        Information2 current = new Information2();
//        current.name2 = i.name2;
//        current.iconId2 = i.iconId2;
//        data2.add(current);
//        notifyDataSetChanged();
//    }

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.to_buy_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Information2 current = data2.get(position);
    //holder.title.setText(current.name);
    holder.title.setText(current.name2);
    holder.icon.setImageResource(current.iconId2);

}

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

class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title;
    ImageView icon;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.to_buy_row_name);
        icon = (ImageView) itemView.findViewById(R.id.to_buy_row_image);
    }
}
}

2 个答案:

答案 0 :(得分:0)

有一些方法可以做到这一点。这里的建议:

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

   private LayoutInflater inflater;
   List<Information> data = Collections.emptyList() ;
   Activity activity;


   public infoAdapter(Activity activity, List<Information> data){
       inflater = LayoutInflater.from(activity);
       this.data=data;
       this.activity = activity;
   }

   public void delete(int position){
       String removed = data.remove(position);
       notifyItemRemoved(position);
       activity.yourSecondAdapter.add(removed);
   }

   //rest of your adapter class

}

您可能需要做一些额外的调整。

答案 1 :(得分:0)

花了几个小时后,我设法做到了这一点(不是很优雅)。我宣称fragment2的arraylist是静态的(不是很好的练习)。只需在infoadapter的viewholder中添加以下代码并进行调整:

    deletebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Firstitems item = mArrayList.get(getAdapterPosition());
                mArrayList.remove(getAdapterPosition());
                notifyItemRemoved(getAdapterPosition());
                Fragment2.mArrayListActress.add(item);

            }
        });