我希望我的问题有意义:基本上,我在一个片段中有一个回收站视图,我想使用Onclick打开另一个片段。因为它是片段,所以我在recyclerview适配器中使用接口/侦听器。但是,我需要在recyclerView适配器中传递一些信息。问题在于适配器位于OnSuccessListener内的OnCreateView中,因为我正在获取应用程序的位置。因此,我不知道如何为适配器中的侦听器传递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() ;
}
}
包含“回收者视图”的片段(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(匿名... android.location.Location>)
我希望这可以澄清问题所在。
答案 0 :(得分:0)
我不清楚您的问题,但我认为您需要处理回收站适配器的操作,并在适配器及其片段与另一个片段之间进行通信
如果正确的话,您可以使用EventBus库,我认为它将在许多情况下为您提供帮助
https://github.com/greenrobot/EventBus, http://greenrobot.org/eventbus/
答案 1 :(得分:0)
我最终弄清楚了,或者至少是为什么这段代码正确。根据我发现的内容,您必须引用主机片段正在实现的侦听器。这就是为什么在正常情况下,将“ 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);