2017-09-02 83 views
1

我在viewpager中有2個片段,我想在兩者中都使用interface添加回調方法。我在viewpager adapter中啓動片段時添加接口偵聽器,但只調用最後一個片段。這就是爲什麼當我點擊MainActivity's菜單選項時,即使MyFragA顯示,只有 MyFragB被調用。在每個viewpager片段中添加回調監聽器

兩個片段......只有活動呼籲MyFragBToast顯示了當枯萎片段被打開

public class MyFragA extends Fragment implements FragmentCaller { 

    private TextView myTxt; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.myfraga, container, false); 
    } 

    @Override 
    public void CallFragment() { 
     if(getActivity()!=null) 
      Toast.makeText(getActivity(), "Activity calling MyFragA", Toast.LENGTH_SHORT).show(); 
    } 
} 

public class MyFragB extends Fragment implements FragmentCaller{ 

    private TextView myTxt; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.myfragb, container, false); 
    } 

    @Override 
    public void CallFragment() { 
     if(getActivity()!=null) 
      Toast.makeText(getActivity(), "Activity calling MyFragB", Toast.LENGTH_SHORT).show(); 
    } 
} 

主要活動

public class MainActivity extends AppCompatActivity { 

    TabLayout tabs; 
    ViewPager viewpager; 

    FragmentCaller fragListerner = null; 
    public interface FragmentCaller { 
     void CallFragment(); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tabs = (TabLayout)findViewById(R.id.tabs); 
     viewpager = (ViewPager)findViewById(R.id.viewpager); 
     SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(this, getSupportFragmentManager()); 
     viewpager.setAdapter(adapter); 
     tabs.setupWithViewPager(viewpager); 

    } // onCreate ends 



    public class SimpleFragmentPagerAdapter extends FragmentStatePagerAdapter { 

     private Context mContext; 

     public SimpleFragmentPagerAdapter(Context context, FragmentManager fm) { 
      super(fm); 
      mContext = context; 
     } 

     // This determines the fragment for each tab 
     @Override 
     public Fragment getItem(int position) { 
      Fragment fragment = null; 
      if (position == 0) { 
       fragment = new MyFragA(); 
       fragListerner = (FragmentCaller) fragment; 
       return fragment; 
      } else if (position == 1){ 
       fragment = new MyFragB(); 
       fragListerner = (FragmentCaller)fragment; 
       return fragment; 
      } 
      return null; 
     } 

     // This determines the number of tabs 
     @Override 
     public int getCount() { 
      return 2; 
     } 

     // This determines the title for each tab 
     @Override 
     public CharSequence getPageTitle(int position) { 
      // Generate title based on item position 
      switch (position) { 
       case 0: 
        return mContext.getString(R.string.fraga); 
       case 1: 
        return mContext.getString(R.string.fragb); 
       default: 
        return null; 
      } 
     } 

    } 

    public boolean onCreateOptionsMenu(Menu menu) { 

     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.callback, menu); 
     return true; 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     super.onOptionsItemSelected(item); 
     switch (item.getItemId()){ 
      case R.id.call: 
       if(fragListerner!=null){ 
        if(fragListerner instanceof MyFragA){ 
         fragListerner.CallFragment(); 
        }else if(fragListerner instanceof MyFragB){ 
         fragListerner.CallFragment(); 
        } 
       } 
       break; 
     } 

     return true; 
    } 


} 

回答

0

,但只有最後一個片段的被稱爲

當然!因爲在pos == 0你正在設置fragListerner = (FragmentCaller) fragment然後再在pos == 1重設fragListerner = (FragmentCaller)fragment;。所以這個條件,fragListerner instanceof MyFragB將永遠是真的!

 @Override 
     public Fragment getItem(int position) { 
      Fragment fragment = null; 
      if (position == 0) { 
       fragment = new MyFragA(); 
       fragListerner = (FragmentCaller) fragment; 
       return fragment; 
      } else if (position == 1){ 
       fragment = new MyFragB(); 
       fragListerner = (FragmentCaller)fragment; 
       return fragment; 
      } 
      return null; 
     } 

所以,你想傳達從activityfragment

沒關係,如果你仍然想使用的接口,你可以做這樣的:

創建的實例你的每個片段的接口!在getItem分配他們兩個,

@Override 
     public Fragment getItem(int position) { 
      Fragment fragment = null; 
      if (position == 0) { 
       fragment = new MyFragA(); 
       fragAListerner = (FragmentCaller) fragment; 
       return fragment; 
      } else if (position == 1){ 
       fragment = new MyFragB(); 
       fragBListerner = (FragmentCaller)fragment; 
       return fragment; 
      } 
      return null; 
     } 

,則調用片段的方法,從它的父活動,這就好比如下,爲前一個簡單的方法:

Fragment fragment = adapter.getItem(0); // MyFragA is at pos 0 
fragment.<specific_function_name>(); 

,使用它的ID!

MyFragA fragment = (MyFragA) getFragmentManager().findFragmentById(R.id.MyFragAId); 
fragment.<specific_function_name>(); 

或者,通過使用它的標籤!

MyFragA fragment = (MyFragA) getFragmentManager().findFragmentByTag("MyFragATag"); 
fragment.<specific_function_name>(); 
+0

我不明白這一點。爲什麼我應該重寫活動中的接口方法?我想從活動中做一些片段的活動。就像從活動中調用片段中的方法一樣。 – CodeAssasins

+0

好吧,我認爲是逆向!我會更新答案! –

+0

回答編輯!祝你好運 –

1

我用rick的回答從here

公共類MainActivity擴展AppCompatActivity {

TabLayout tabs; 
ViewPager viewpager; 
public static int currentFrag=0; 
SimpleFragmentPagerAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tabs = (TabLayout)findViewById(R.id.tabs); 
    viewpager = (ViewPager)findViewById(R.id.viewpager); 
    adapter = new SimpleFragmentPagerAdapter(this, getSupportFragmentManager()); 
    viewpager.setAdapter(adapter); 
    tabs.setupWithViewPager(viewpager); 
    viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(int position) { 
      System.out.println("sammy_current_position: "+position); 
      currentFrag = position; 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    }); 

} // onCreate ends 



public class SimpleFragmentPagerAdapter extends FragmentStatePagerAdapter { 

    private Context mContext; 

    public SimpleFragmentPagerAdapter(Context context, FragmentManager fm) { 
     super(fm); 
     mContext = context; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     if (position == 0) { 
      return new MyFragA(); 
     } else if (position == 1){ 
      return new MyFragB(); 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return 2; 
    } 

    // This determines the title for each tab 
    @Override 
    public CharSequence getPageTitle(int position) { 
     // Generate title based on item position 
     switch (position) { 
      case 0: 
       return mContext.getString(R.string.fraga); 
      case 1: 
       return mContext.getString(R.string.fragb); 
      default: 
       return null; 
     } 
    } 

} 

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.callback, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
    super.onOptionsItemSelected(item); 
    switch (item.getItemId()){ 
     case R.id.call: 
      if(currentFrag == 0) { 
       MyFragA frag1 = (MyFragA)viewpager.getAdapter().instantiateItem(viewpager, viewpager.getCurrentItem()); 
       frag1.specialCase(); 
      } 

      break; 
    } 

    return true; 
} 

}