2015-12-14 61 views
0

我有一個viewpager應用程序,我正在處理3個片段。 enter image description here 我通過類似的FragmentStatePagerAdapter動態添加片段:如何從活動中引用片段的方法?

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created 
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created 


// Build a Constructor and assign the passed Values to appropriate values in the class 
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) { 
    super(fm); 

    this.Titles = mTitles; 
    this.NumbOfTabs = mNumbOfTabsumb; 

} 

//This method return the fragment for the every position in the View Pager 
@Override 
public Fragment getItem(int position) { 

    if(position == 0) // if the position is 0 we are returning the First tab 
    { 
     Current_forecast_fragment current_forecast_fragment = new Current_forecast_fragment(); 
     return current_forecast_fragment; 
    } 
    else if (position == 1)   // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab 
    { 
     return new Hourly_forecast_fragment(); 
    }else { 
     return new Daily_forecast_fragment(); 
    } 

} 

// This method return the titles for the Tabs in the Tab Strip 

@Override 
public CharSequence getPageTitle(int position) { 
    return Titles[position]; 
} 

// This method return the Number of tabs for the tabs Strip 

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

我如何引用片段,可以說從我的主要活動根據目前的預測呢?我嘗試過類似的東西,但我需要片段ID來引用它。有什麼建議嗎?:

public class MainActivity extends AppCompatActivity { 
ViewPager pager; 
ViewPagerAdapter adapter; 
SlidingTabLayout tabs; 
CharSequence Titles[]={"Current","Hourly","Daily"}; 
int Numboftabs =3; 
Current_forecast_fragment mCurrent_forecast_fragment; 

public static final String TAG = MainActivity.class.getSimpleName(); 
public static final String LOCATION_KEY = "location_key"; 
public Forecast mForecast; 
public static final String DAILY_FORECAST = "DAILY_FORECAST"; 
public static final String HOURLY_FORECAST = "HOURLY_FORECAST"; 
//default coordinates - Gotse Delchev, UK Lati:57.156866 ; Long: 
private double latitude = 41.5667; 
private double longitude = 23.7333; 
private LocationManager locationManager; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //-----------MY CODE STARTS HERE----------------- 

    mCurrent_forecast_fragment = (Current_forecast_fragment) getSupportFragmentManager().findFragmentById(R.id.??????); 
    //Log.d("MainActivity",mCurrent_forecast_fragment.getTag() + "Georgi"); 
    getLocation(); 

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs. 
    adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs); 

    // Assigning ViewPager View and setting the adapter 
    pager = (ViewPager) findViewById(R.id.pager); 
    pager.setAdapter(adapter); 

    // Assiging the Sliding Tab Layout View 
    tabs = (SlidingTabLayout) findViewById(R.id.tabs); 
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width 

    // Setting Custom Color for the Scroll bar indicator of the Tab View 
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
     @Override 
     public int getIndicatorColor(int position) { 
      return getResources().getColor(R.color.tabsScrollColor); 
     } 
    }); 

    // Setting the ViewPager For the SlidingTabsLayout 
    tabs.setViewPager(pager); 




} 
} 
+1

請訪問http://計算器。 com/questions/8785221/retrieve-a-fragment-from-a-viewpager – QuantumTiger

+1

可能的XY問題。你爲什麼要訪問片段? –

+0

我想訪問片段類中的一些方法。 –

回答

0

經過大量的實驗後,我帶着一個簡單的解決方案來解決我的問題。由於我動態添加片段(在viewPagerAdapter中),而不是在XML中,我無法通過id或標記來引用它們。我的解決方案是稍微修改適配器,以便它存儲作爲參數傳遞的片段的引用。然後,所有的片段的方法是在MainActivity訪問(如果declired市民):

MainActivity:

public class MainActivity extends AppCompatActivity { 
ViewPager pager; 
ViewPagerAdapter adapter; 
SlidingTabLayout tabs; 
CharSequence Titles[]={"Current","Hourly","Daily"}; 
int Numboftabs =3; 
Current_forecast_fragment mCurrent_forecast_fragment; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //-----------MY CODE STARTS HERE----------------- 
    this.mCurrent_forecast_fragment = new Current_forecast_fragment(); 

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs. 
    adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs,mCurrent_forecast_fragment); 

    // Assigning ViewPager View and setting the adapter 
    pager = (ViewPager) findViewById(R.id.pager); 
    pager.setOffscreenPageLimit(3); 
    pager.setAdapter(adapter); 

    // Assiging the Sliding Tab Layout View 
    tabs = (SlidingTabLayout) findViewById(R.id.tabs); 
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width 

    // Setting Custom Color for the Scroll bar indicator of the Tab View 
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
     @Override 
     public int getIndicatorColor(int position) { 
      return getResources().getColor(R.color.tabsScrollColor); 
     } 
    }); 

    // Setting the ViewPager For the SlidingTabsLayout 
    tabs.setViewPager(pager); 
} 

我在ViewPagerAdapter代碼:

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 
private Current_forecast_fragment mCurrent_forecast_fragment; 
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created 
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created 


// Build a Constructor and assign the passed Values to appropriate values in the class 
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb,Current_forecast_fragment current_fragment) { 
    super(fm); 
    this.mCurrent_forecast_fragment = current_fragment; 
    this.Titles = mTitles; 
    this.NumbOfTabs = mNumbOfTabsumb; 

} 

//This method return the fragment for the every position in the View Pager 
@Override 
public Fragment getItem(int position) { 

    if(position == 0) // if the position is 0 we are returning the First tab 
    { 
     return this.mCurrent_forecast_fragment; 
    } 
    else if (position == 1)   // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab 
    { 
     return new Hourly_forecast_fragment(); 
    }else { 
     return new Daily_forecast_fragment(); 
    } 

} 
}