什么是片段中MainActivity.this的类似代码?

时间:2015-09-18 08:51:32

标签: java android android-fragments

我想使用这行代码:" mAdapter = new MembersAdapter(_______,membersList);" 在我的名为HomeFragment的片段类中如果它在Activity中使用,那么它可能被写为mAdapter = new MembersAdapter(MainActivity.this, membersList);

但是现在,我想在Fragment中执行for the Fragment所以我该怎么做?“

2 个答案:

答案 0 :(得分:0)

您可以将基本活动编写为Fragmentactivity,并通过将其扩展到其他活动来使用基本活动,以便在片段类中维护基本活动。

  youractivity youractivity_object;

  public void onAttach(Activity activity) {
        super.onAttach(activity);
        youractivity_object= (youractivity) activity;

    }

答案 1 :(得分:0)

您可以尝试以下代码: 1 /

class Adapter extends RecyclerView.Adapter {
 public interface Adapter2FragementInteraction {
     void onSomeEvent();
 }
 // Override the constructor;
 public Adapter(Adapter2FragmentInteraction listener){
      //Save this listener;
      this.listener = listener;
 }

}

2 /在你的片段中:

`RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setAdapter(new Adapter(
    new Adapter2FragementInteraction(){
        @Override
        public void onSomeEvent(){
              //Do something with your Fragment's Views
        }

    }
);`