通过另一个适配器访问适配器的数据

时间:2018-09-02 07:19:40

标签: android android-recyclerview fragment adapter

是否可以通过另一个适配器访问适配器的数据? 我想开始一个活动并将数据从适配器传递到一个片段,该片段在TabLayout中用作三个片段之一,我有两个适配器和一个单击以开始活动的按钮,其Java代码位于我的第一个适配器,我需要通过第二个适配器本身传递第二个适配器的数据 这是我的代码:

我的第一个适配器:

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder>{

    private ArrayList<SectionDataModel> dataList;
    private Context mContext;
    private RecyclerView.RecycledViewPool recycledViewPool;
    private SnapHelper snapHelper;

    public RecyclerViewDataAdapter(ArrayList<SectionDataModel> dataList, Context mContext) {
        this.dataList = dataList;
        this.mContext = mContext;
        recycledViewPool = new RecyclerView.RecycledViewPool();
    }

    @Override
    public ItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
        ItemRowHolder rowHolder = new ItemRowHolder(v);
        snapHelper = new GravitySnapHelper(Gravity.START);
        return rowHolder;
    }

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

        ArrayList singleSectionItems = dataList.get(position).getAllItemInSection();

        final String sectionName = dataList.get(position).getHeaderTitle();
        holder.itemTitle.setText(sectionName);

        SectionDataAdapter adapter = new SectionDataAdapter(singleSectionItems, mContext);
        holder.recyclerView.setHasFixedSize(true);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        holder.recyclerView.setAdapter(adapter);
        holder.recyclerView.setRecycledViewPool(recycledViewPool);
        snapHelper.attachToRecyclerView(holder.recyclerView);


        holder.btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//here i can start the activity but..(second adapter)
                Toast.makeText(view.getContext(), sectionName, Toast.LENGTH_SHORT).show();
            }
        });
    }

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

    public class ItemRowHolder extends RecyclerView.ViewHolder {
        protected ImageView mitemImage;
        protected TextView mitemtext;
        protected TextView itemTitle;
        protected RecyclerView recyclerView;
        protected Button btnMore;

        public ItemRowHolder(View itemView) {
            super(itemView);
            this.mitemImage = itemView.findViewById(R.id.itemImage);
            this.mitemtext = itemView.findViewById(R.id.tvTitle);
            this.itemTitle = itemView.findViewById(R.id.itemTitle);
            this.recyclerView = itemView.findViewById(R.id.recycler_view_list);
            this.btnMore = itemView.findViewById(R.id.btnMore);
        }
    }
} '

和我的第二个适配器:

import java.net.PortUnreachableException;
import java.util.ArrayList;

public class SectionDataAdapter extends RecyclerView.Adapter<SectionDataAdapter.SssingleItemRowHolder>{

    private ArrayList<SingleItemModel> itemModels;
    private Context mContext;

    public SectionDataAdapter(ArrayList<SingleItemModel> itemModels, Context mContext) {
        this.itemModels = itemModels;
        this.mContext = mContext;
    }

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_single_card, null);
    SingleItemRowHolder singleItemRowHolder = new SingleItemRowHolder(v);
    return singleItemRowHolder;
}

@Override
public void onBindViewHolder(SingleItemRowHolder holder, int position) {
    SingleItemModel itemModel = itemModels.get(position);
    holder.tvTitle.setText(itemModel.getName());
    holder.mitemImage.setImageResource(itemModel.getImage());

}


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

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView tvTitle;
    protected ImageView mitemImage;


    public SingleItemRowHolder(View itemView) {
        super(itemView);

        final Intent intent = new Intent(mContext,MainActivity.class);
        this.mitemImage = itemView.findViewById(R.id.itemImage);
        this.tvTitle = itemView.findViewById(R.id.tvTitle);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//... need to start the activity from here
                Toast.makeText(view.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}

1 个答案:

答案 0 :(得分:1)

@MilaDroid 一样,您只需要一个getter即可返回另一个Adapter's ArrayList<SingleItemModel>,但是您将要面对的问题是您需要Adapter中与Activity相同的实例,以获取填充的ArrayList<SingleItemModel>

  

一个好的解决方法是在Adapter

中使用Bill Pugh的Singleton
public class Adapter {

private ArrayList<SingleItemModel> list;

private Adapter() {}

public static Adapter getInstance() {
    return InstInit.INSTANCE;
}

// Don't forget to set the list (or NPE)
// because we can't argue with a Singleton
public void setList(ArrayList<SingleItemModel> list) {
    this.list = list;
}

// You can now get the ArrayList
public ArrayList<SingleItemModel> getList() {
    return list;
}

private static class InstInit {
    private static final Adapter INSTANCE = new Adapter();
}

// Overrided RecyclerView.Adapter Methods
.................

}
  

假设以下ArrayList是Singleton,则检索Adapters

AdapterOne a1 = AdapterOne.getInstance();
AdapterTwo a2 = AdapterTwo.getInstance();

ArrayList<SingleItemModel> a1RetrievedList = a1.getList();
// You don't need to create a new instance
// creating a new instance doesn't make sense
// because you need to repopulate the list
// for the new instance.

ArrayList<SingleItemModel> a2RetrievedList = a2.getList();
// You can also retrieve from AdapterTwo