从ViewHolder获取RecyclerView.Adapter列表

时间:2016-07-07 13:33:48

标签: android android-viewholder

是否可以使用Activity OnClickListener将当前的项目列表转移到新的ViewHolder?或者其他任何方式?

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.EventsViewHolder> {
    List<Event> events; //get this List
    RVAdapter(List<Event> events) {
        this.events = events;
}

public static class EventsViewHolder extends RecyclerView.ViewHolder {

    TextView date;
    TextView text;
    ImageView photo;

    EventsViewHolder(final View itemView) {
        super(itemView);
        date = (TextView) itemView.findViewById(R.id.date);
        text = (TextView) itemView.findViewById(R.id.text);
        photo = (ImageView) itemView.findViewById(R.id.photo);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                SendObject sendObject = new SendObject(setList); //and transfer it here
                Intent intent = new Intent(context, ReadEvent.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("events", sendObject);
                bundle.putInt("pos", getAdapterPosition());
                bundle.putString("test", "test");
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }
}

4 个答案:

答案 0 :(得分:0)

解决方案1:在ViewHolder中创建传递列表的方法

解决方案2:将ViewHolder定义为非静态类并传递列表

另一种方法:

    // Solution 1:
    public class EventsViewHolder extends RecyclerView.ViewHolder {

            EventsViewHolder(final View itemView) {
                super(itemView);
            }

            public void bind(List<Event> events) {
                ...
                SendObject sendObject = new SendObject(events); //and transfer it here
                ...
            }
        }

    // Solution 2:
    public class EventsViewHolder extends RecyclerView.ViewHolder {
        EventsViewHolder(final View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SendObject sendObject = new SendObject(events); //and transfer it here
                }
            });
        }
    }

答案 1 :(得分:0)

  

适配器类。

public ServicesHomeAdapter(Context context, ArrayList<HomeScreenData> homeScreenDataArrayList,OnItemClickedListener listener) {
    this.context=context;
    this.homeScreenDataArrayList=homeScreenDataArrayList;
    this.listener=listener;
}

@Override
public ServicesHomeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{

    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_service,parent,false);
    ViewHolder viewHolder=new ViewHolder(view);
    return viewHolder;
}

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

    final HomeScreenData homeScreenData = homeScreenDataArrayList.get(position);

}

@Override
public int getItemCount()
{
    return (null!=homeScreenDataArrayList?homeScreenDataArrayList.size():0);
}


public class ViewHolder extends RecyclerView.ViewHolder {
    protected ImageView ivSection;
    protected TextView txImageName;


    public ViewHolder(View itemView)
    {
        super(itemView);
        this.ivSection= (ImageView) itemView.findViewById(R.id.ivcard);
        this.txImageName= (TextView) itemView.findViewById(R.id.txtimgname);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClicked(getAdapterPosition());
            }
        });
    }
}

public interface OnItemClickedListener{
    void onItemClicked(int position);
}

}

  

在您的活动中实施界面。

@Override
public void onItemClicked(int position) {
    Intent intent = new Intent(getActivity(), A.class);
    Pojo Class pojo = arrayList.get(position);
    intent.putExtra(TAG, pojo);
    startActivity(intent);

}`

答案 2 :(得分:0)

你的Pojo应该是可解析的或序列化的但是现在android建议实现parseable然后你可以通过intent传递任何Pojo,你如何使你的类可解析检查这个链接。 http://jbehave.org/reference/stable/annotations.html intent.putExtra(key,Serializable / Parcelable)然后在第二个活动中getParcelableExtra(key)/ getSerializableExtra(key)

答案 3 :(得分:0)

我已经有了一个Serializable对象:

public class SendObject实现Serializable {

private List<Event> events;

public SendObject(List<Event> events) {
    this.events = events;
}

public List<Event> getEvents() {
    return events;
}

}

主要问题是接收当前的适配器列表。使用Singleton解决了这个问题。解决方案很简单。

公共类RVAdapter扩展了RecyclerView.Adapter {

private static RVAdapter rvAdapter;
private List<Event> events;

public RVAdapter() {
}

public static RVAdapter getInstance() {
    if (rvAdapter==null) {
        rvAdapter = new RVAdapter();
    }
    return rvAdapter;
}


public void setList(List<Event> events) {
    this.events = events;
}

public static class EventsViewHolder extends RecyclerView.ViewHolder {

    TextView date;
    TextView text;
    ImageView photo;

    EventsViewHolder(final View itemView) {
        super(itemView);
        date = (TextView) itemView.findViewById(R.id.date);
        text = (TextView) itemView.findViewById(R.id.text);
        photo = (ImageView) itemView.findViewById(R.id.photo);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                SendObject sendObject = null;
                sendObject = new SendObject(RVAdapter.getInstance().events);  //current List
                Intent intent = new Intent(context, ReadEvent.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("events", sendObject);
                bundle.putInt("pos", getAdapterPosition());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }
}

无论如何,谢谢你的回复!