使用片段作为Android中的上下文

时间:2018-02-04 23:29:28

标签: java android android-fragments android-context

我试图遵循这个tutorial

我有一个使用侧边栏导航的项目,因此我有一个MainActivity和多个碎片。在视频的大约6:20,您可以看到以下代码:

PersonListAdapter adapter = new PersonListAdapter(
                                    this,  
                                    R.layout.adapter_view_layout, 
                                    peopleList);

PersonListAdapter类的构造函数是:

public PersonListAdapter(Context context, int resource, ArrayList<Attacks> objects) {
        super(context, resource, objects);
        this.mContext = mContext;
        mResource = resource;
    }

问题在于上下文。

  • 如果我使用&#34; 这个&#34;这个词,则会出现一条红线。
  • 如果我更换 &#34;这&#34;使用&#34; getActivity()&#34;,没有红线,但应用 我跑的时候会崩溃。
  • 我还试过&#34; this.getContext()&#34;和&#34; this.getActivity()&#34;
  • 我也试过更换&#34;这个&#34;使用&#34; getActivity()。getApplicationContext()&#34;,然后应用程序崩溃。

本教程使用MainActivity.java,但我的代码在FragmentCharacters.java中。我不知道我应该写什么来代替&#34; 这个&#34;,或者如果我需要在PersonListAdapter类中更改Context的内容。

2 个答案:

答案 0 :(得分:0)

您不能将Fragment用作Context,因为Fragment不会从Context继承。

但是,如果您查阅Fragment lifecycle,您可以看到Fragment可以在生命周期回调OnActivityCreated()和onDestroyView()之间的任何时间访问其主机Activity。例如,如果您尝试在OnActivityCreated()之前访问Activity,它可能会返回null。

因此,请确保从onActivityCreated()或更高版本中调用getActivity(),这将确保您的Activity可用。

答案 1 :(得分:-1)

更新,我在代码片段中提供了一个案例,我选择了#34; FragmentName&#34;例如片段名称。

首先看看这个片段结构。 我在onCreateonCreateView

中添加了[mAdapter]

我为Forth参数添加了FragmentName.this

原因是,您可以将数据从适配器发送到其他活动,例如FragmentName.mAdapter.getLayoutPosition()

但是,我们假设我们有一个在MainActivity中的ImageView,我们想在我们的适配器中使用它,所以让我们在片段中建立一个ImageView,注意我声明了ImageView Inside onCreate

而且,另一个例子,让我们假设我们在片段末尾有一个公共虚空,它可以是字面上的任何东西。我将其命名为ExampleClass

&#13;
&#13;
/////////FIRST TAKE A LOOK AT FRAGMENT//////////
public class FragmentName extends Fragment {

  PersonListAdapter adapter;
  ImageView imageView; // For Example thi ImageView is from MainActivity

  public FragmentName() {
    ...
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    adapter = new PersonListAdapter(getContext(), R.layout.adapter_view_layout, peopleList, FragmentName.this);

    imageView = (ImageView) getActivity().findViewById(R.id.ImageView);
    // This Imageview is in Another Activity, Like MainActivity
    // So we Need to Find it Using 'getActivity()'

    ...
  }


  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    adapter = new PersonListAdapter(getContext(), R.layout.adapter_view_layout, peopleList, FragmentName.this);

  }

}

  public void ExampleClass(int color, ...) {
  ...
  }

  ////////////////////////////////////////////////////////////////
&#13;
&#13;
&#13;

现在,让我们将此示例带入我们的适配器,以显示如何使用它。

但是,在适配器中使用[FragmentName],而不是像下面这样的[Fragment]:

&#13;
&#13;
///////////NOW INSIDE YOUR ADAPTER/////////////
  public class PersonListAdapter extends RecyclerView.Adapter < PersonListAdapter.myViewHolder > {

      FragmentName myFragment; // SEE WHAT HAPPENDED HERE?

      ...

      public PersonListAdapter(Context context, int resource, ArrayList < Attacks > objects, FragmentName fragment) {
        super(context, resource, objects);
        this.mContext = mContext;
        mResource = resource;
        this.myFragment = fragment
      }


      @Override
      public PersonListAdapter.myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      
      // Example use of myFragment
      // Lets Execute ExmpleClass inside the Fragment
      myFragment.ExampleClass(int color, ...);
      
      // Let's Use the ImageView from MainActivity Here
      myFragment.imageView.setImageRresource(...);
      
      
        ...
      }
      ...

      // YOU CAN NOW USE "myFragment" As a Context In your Adapter
&#13;
&#13;
&#13;

关于这一点的好处是你可以在PersonListAdapter中使用片段作为上下文

更新:第二个代码,onCreateViewHolder错误,它必须在ViewHolder或onBindViewHolder中的ClickListener内部

相关问题