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

Posted

技术标签:

【中文标题】通过另一个适配器访问一个适配器的数据【英文标题】:accessing an adapter's data via another adapter 【发布时间】:2019-02-07 15:07:26 【问题描述】:

是否可以? 我想启动一个活动并将数据从适配器传递到在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();
            
        );

    

【问题讨论】:

如我所见,您只需要实现一个 getter 方法并在您的其他适配器中调用它! 【参考方案1】:

就像 @MilaDroid 说您只需要一个 getter 来返回另一个 Adapter's ArrayList&lt;SingleItemModel&gt; 但您将面临的问题是您需要拥有相同的 @987654324 实例@ 来自Activity 以获取填充的ArrayList&lt;SingleItemModel&gt;

一个好的解决方法是在 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 假设以下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

【讨论】:

强烈建议我搜索 Parcelable,你能帮我吗? 它可能会工作,但它比我的解决方案更乏味。

以上是关于通过另一个适配器访问一个适配器的数据的主要内容,如果未能解决你的问题,请参考以下文章

从适配器访问片段

适配器模式

8设计模式适配器模式

设计模式之适配器模式

适配器模式和代理模式的区别

Java设计模式之《适配器模式》及应用场景