时无法解析上下文或应用程序

时无法解析上下文或应用程序

本文介绍了从片段的适配器(A)导航到另一个片段(B)时无法解析上下文或应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个片段(A)导航到另一个(B),但是该片段但第一个片段(A)具有recyclerView的含义,当我单击任何项​​目时,我应该导航至下一个.我正在使用android Navigation组件,但是我无法解析方法 findNavController(xxx),因为它需要片段的ApplicationContext.,因为我尝试了 v.getContext() v.getApplicationContext() mContext ,但是没有运气.

Am trying to navigate from one fragment (A) to another (B), but the fragment, but the first fragment (A) has a recyclerView meaning when I click on any Item I should navigate to the next one. Am using android Navigation component but I couldn't resolve the method findNavController(xxx) since it requires the ApplicationContext of the fragment. , because I tried v.getContext(), v.getApplicationContext(), mContext, but there wasn't luck.

如何解决此问题,下面是RecyclerView Adapter类中的 onBindViewHolder().?

How can I resolve this issue, below is the onBindViewHolder() in the RecyclerView Adapter class. ?

解决这个问题的最佳方法是什么

What could be the best way to reslve this

@Override
    public void onBindViewHolder(@NonNull final CoordinatesViewHolder holder, int position) {

        final Coordinates coord = mCoordinates.get(position);
        holder.place_name.setText(coord.getmUPlaceName());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               NavHostFragment.findNavController(xxx).navigate(R.id.action_bookmarking_to_weatherFragment);
            }
        });



    }

推荐答案

RecyclerView适配器不负责重定向到另一个片段.

It is not the responsibility of RecyclerView's adapter to redirect to another fragment.

创建类似界面

public interface OnItemClickListener {
    void onItemClicked(int position)
}

在RecyclerView的适配器添加方法中:

Inside your RecyclerView's adapter add method:

public class YourAdapterName extend RecyclerView.Adapter...
    private OnItemClickListener onItemClickListener

    void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener
    }

...

    @Override
    public void onBindViewHolder(@NonNull final CoordinatesViewHolder holder, int position) {
        final Coordinates coord = mCoordinates.get(position);
        holder.place_name.setText(coord.getmUPlaceName());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null) {
                     onItemClickListener.onItemClicked(position)
                }
            }
        });
    }

在带回收站的片段中,在设置适配器的地方添加代码:

In your fragment with recycler, in place where you set adapter add code:

YourAdapterClassName adapter = new YourAdapterClassName(...init adapter...)
adapter.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClicked(int position) {
         //Navigate here
    }
})
yourRecyclerName.setAdapter(adapter)

希望对您有帮助

这篇关于从片段的适配器(A)导航到另一个片段(B)时无法解析上下文或应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:16