我使用了带有4个制表符的slidingTabLayout,以及每个制表符的viewpager和4个片段类。
当用户点击某个按钮时,我需要在我的项目中重新加载我的一个片段(第一个标签),为此我需要为该片段添加标签
我的问题是:如何为其中一个viewpagers片段???
设置标签我的MainActivity标签和viewpager代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"نوع","قیمت","برند","همه"};
int Numboftabs = 4;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "درج آگهی رایگان", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// 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.colorWhite);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
pager.setCurrentItem(3);
} and ....
非常感谢
答案 0 :(得分:26)
对此有几种解决方案。
1)。当FragmentPagerAdapter
向Fragment
添加FragmentManager
时,它会根据Fragment
的特定位置使用特殊标记。因此,您可以使用这些代码行
Fragment
中获取当前可见ViewPager
Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + ViewPager.getCurrentItem());
// based on the current position you can then cast the page to the correct Fragment class and call some method inside that fragment to reload the data:
if (0 == ViewPager.getCurrentItem() && null != fragment) {
((TabFragment1)fragment).reloadFragmentData();
}
2)。您应该跟踪FragmentStatePagerAdapter
中的所有活动片段。对于演示示例,您可以参考第二种方法here
3)。您可以使用EventBus
从任何地方向Fragment
发布活动。您所要做的就是为该特定事件注册Fragment
。有关官方文档,请参阅this
答案 1 :(得分:8)
以下是FragmentPagerAdapter
用于创建Fragment
标记名称的方法:
private static String makeFragmentName(int viewId, long id) {
return "android:switcher:" + viewId + ":" + id;
}
viewId
是ViewPager
的身份证明
id
是ViewPager
内部片段的位置。
示例:
String fragmentTag = makeFragmentName(myViewPager.getId(), 2);
Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
答案 2 :(得分:0)
尽管已经回答了这个问题。我有另一种方法可以实现这一点,这可能会更好,因为代码中标记片段的方式可能会发生变化。另外要指出的是,使用adapter.getItem(position)不会返回正在显示的片段的实例,这将取决于我们自己实现的getItem方法。
所以基本上,问题在于我们没有viewPager中存在的Fragments实例。这个想法是覆盖方法
public Object instantiateItem(ViewGroup container, int position) {
// If we already have this item instantiated, there is nothing
// to do. This can happen when we are restoring the entire pager
// from its saved state, where the fragment manager has already
// taken care of restoring the fragments we previously had instantiated.
if (mFragments.size() > position) {
Fragment f = mFragments.get(position);
if (f != null) {
return f;
}
}
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
Fragment fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
if (mSavedState.size() > position) {
Fragment.SavedState fss = mSavedState.get(position);
if (fss != null) {
fragment.setInitialSavedState(fss);
}
}
while (mFragments.size() <= position) {
mFragments.add(null);
}
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
mFragments.set(position, fragment);
mCurTransaction.add(container.getId(), fragment);
return fragment;
}
FragmentStatePagerAdapter的。如果我们查看此方法的默认实现,则基本上可以完成实例化如果不存在的新Fragment并返回它们的工作。它还将它们存储在私有的片段列表中。因此,我们可以做的是重写此方法,并将实例存储在我们自己列表中的片段上。可以通过以下方式实现
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> fragmentList;
private final int NUM_PAGES;
public ScreenSlidePagerAdapter(FragmentManager fm, int numPages) {
super(fm);
NUM_PAGES = numPages;
fragmentList = new ArrayList<>(NUM_PAGES);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new CustomFragment();
//this method can be modified according to the requirement
return fragment;
}
@Override
public int getCount() {
return NUM_PAGES;
}
public Fragment getFragment(int position){
return fragmentList.get(position);
}
@Override
public Object instantiateItem(ViewGroup container, int position){
Fragment fragment = (Fragment) super.instantiateItem(container,position);
if(fragmentList.size()<NUM_PAGES&&!fragmentList.contains(fragment)){
fragmentList.add(fragment);
}
return fragment;
}
}
因此,我们将拥有所有可用片段的实例,我们将不再需要任何标签来查找显示给用户的片段实例。