【问题标题】:How to pass a listener within a recyclerview adapter that is in an OnSuccessListener如何在 OnSuccessListener 中的 recyclerview 适配器中传递侦听器
【发布时间】:2019-01-14 03:47:48
【问题描述】:

我希望我的问题是有道理的:基本上我在片段中有一个回收器视图,我想使用 Onclick 打开另一个片段。因为它的片段到片段我在我的 recyclerview 适配器中使用接口/侦听器。但是我需要在我的 recyclerView 适配器中传递一些东西。问题是适配器位于 OnCreateView 中,它位于 OnSuccessListener 中,因为我正在获取我的应用程序的位置。因此,我不知道如何在我的适配器中为我的侦听器传递除 null 之外的任何内容。我需要在我的适配器中传递一个监听器。我希望这是有道理的,但这是我的代码,所以很清楚:

RecyclerViewAdapter

public class MessierRecyclerViewAdapter extends RecyclerView.Adapter<MessierRecyclerViewAdapter.ViewHolder> {

private List<MessierData> MessierDataList;
   Context mContext;

private MessierListener messierListener;

public interface MessierListener{
    void onClicked(Bundle bundle);
}





 class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView currentLocation;

    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        currentLocation = (TextView) itemView.findViewById(R.id.messier_location);


    }

    @Override
    public void onClick(View view) {
        if (messierListener !=null) {
            int col = getAdapterPosition();
            Bundle bundle = new Bundle();
            bundle.putDouble("RightAscension", MessierDataList.get(col).getMessierRA());
            bundle.putDouble("Declination", MessierDataList.get(col).getMessierDEC());
            bundle.putDouble("Azimuth", MessierDataList.get(col).getMessierAZ());
            bundle.putDouble("Altitude", MessierDataList.get(col).getMessierALT());
            messierListener.onClicked(bundle);



        }

        }

    }


public MessierRecyclerViewAdapter(List<MessierData> MessierDataList, Context mContext, MessierListener messierListener){
    this.MessierDataList = MessierDataList;
    this.mContext = mContext;
    this.messierListener = messierListener;
}

@Override
public MessierRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.messier_recycler_item,parent, false);



    return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder( MessierRecyclerViewAdapter.ViewHolder holder, int position) {
    holder.currentLocation.setText(MessierDataList.get(position).getMessierLocation());


}

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


}

包含 Recycler 视图的片段(Messier 片段) 它有很多代码,所以我将添加目前的问题 onCreateView:

public class MessierFragment extends Fragment implements     
MessierRecyclerViewAdapter.MessierListener {
...
   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_messier, container, false);

    mRecyclerView = (RecyclerView)view.findViewById(R.id.messier_recycler_view);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(),DividerItemDecoration.VERTICAL));



    mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {

            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();

                Sun sunInstance = new Sun();
                sunInstance.SunmCalculation(longitude, latitude);

                Andromeda andromedaexample = new Andromeda();
                andromedaexample.AndromedaCalculation(latitude);


                messierList();
                messierItems.addAll(messierList());

                adapter = new MessierRecyclerViewAdapter(messierItems, mRecyclerView.getContext(),MessierFragment.this);
                mRecyclerView.setAdapter(adapter);


            }

        }
    });

    return view;
}

我面临的问题是,当我尝试在适配器中为 Messier 侦听器传递某些内容时,它会从 android studio 发出警告,因此我不确定如何解决该问题。现在我有 MessierFragment.this 不起作用。谢谢你的帮助。

编辑/警告 ANDROID STUDIO 提供

适配器中的预期参数:MessierListener

如果我在 onCreateView 的适配器中使用“this”:this (anonymous...android.location.Location>)

我希望这能在一定程度上澄清问题所在。

【问题讨论】:

  • 警告是什么?
  • 请明确您的问题,您需要从哪个片段传递给适配器?
  • 我在描述中添加了我收到的警告。

标签: android android-fragments android-recyclerview


【解决方案1】:

我无法清楚地回答您的问题,但我认为您需要处理来自回收器适配器的操作,并在适配器及其片段与另一个片段之间进行通信

如果正确,您可以使用 EventBus 库,我认为它会在很多情况下为您提供帮助

https://github.com/greenrobot/EventBus, http://greenrobot.org/eventbus/

【讨论】:

    【解决方案2】:

    我最终弄清楚了,或者至少弄清楚了为什么这段代码是正确的。根据我发现的内容,您必须引用主机片段正在实现的侦听器。这就是为什么在正常情况下将“this”放在我的适配器中以将该侦听器传递给recyclerview。但是,因为我的适配器在 OnSuccessListener 中,所以“this”引用了会产生错误的侦听器。为了解决这个问题,我放入了 MessierFragment.this 并引用了主机片段正在实现的侦听器。

     mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new      
     OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
    
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
    
                Sun sunInstance = new Sun();
                sunInstance.SunmCalculation(longitude, latitude);
    
                Andromeda andromedaexample = new Andromeda();
                andromedaexample.AndromedaCalculation(latitude);
    
    
                messierList();
                messierItems.addAll(messierList());
    
                adapter = new MessierRecyclerViewAdapter(messierItems, mRecyclerView.getContext(),MessierFragment.this);
                mRecyclerView.setAdapter(adapter);
    

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多