我需要在ViewPager中实现片段替换。
经过一番搜索,我找到了这个适配器(看起来效果很好):
public class ViewPagerAdapter extends FragmentStatePagerAdapter implements ViewPager.OnPageChangeListener
{
private final AppCompatActivity mActivity;
private final TabLayout tabLayout;
private final ViewPager mPager;
private List<TabInfo> mTabs = new LinkedList<TabInfo>();
private int TOTAL_TABS;
private Map<Integer, Stack<TabInfo>> history = new HashMap<Integer, Stack<TabInfo>>();
public ViewPagerAdapter(AppCompatActivity activity, ViewPager pager, TabLayout tabLayout)
{
super(activity.getSupportFragmentManager());
activity.getSupportFragmentManager();
this.mActivity = activity;
this.mPager = pager;
this.tabLayout = tabLayout;
}
public void addTab(final Class fragmentClass, final Bundle args)
{
final TabInfo tabInfo = new TabInfo(fragmentClass, args);
tabLayout.newTab();
tabLayout.setTag(tabInfo);
mTabs.add(tabInfo);
notifyDataSetChanged();
}
@Override
public Fragment getItem(final int position)
{
final TabInfo tabInfo = mTabs.get(position);
return Fragment.instantiate(mActivity, tabInfo.fragmentClass.getName(), tabInfo.args);
}
@Override
public int getItemPosition(final Object object) {
int position = tabLayout.getSelectedTabPosition();
int pos = POSITION_NONE;
if (history.get(position).isEmpty()) {
return POSITION_NONE;
}
for (Stack<TabInfo> stack : history.values()) {
TabInfo c = stack.peek();
if (c.fragmentClass.getName().equals(object.getClass().getName())) {
pos = POSITION_UNCHANGED;
break;
}
}
return pos;
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position)
{
}
public void onTabSelected(final FragmentTransaction ft) {
TabInfo tabInfo = (TabInfo) tabLayout.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i).equals(tabInfo)) {
mPager.setCurrentItem(i);
}
}
}
public void onTabUnselected(FragmentTransaction ft) {
}
public void onTabReselected(FragmentTransaction ft) {
}
public void replace(final int position, final Class fragmentClass, final Bundle args)
{
mActivity.getSupportFragmentManager().beginTransaction().addToBackStack(null).commit();
updateTabs(new TabInfo(fragmentClass, args), position);
history.get(position).push(new TabInfo(mTabs.get(position).fragmentClass, mTabs.get(position).args));
notifyDataSetChanged();
}
private void updateTabs(final TabInfo tabInfo, final int position)
{
mTabs.remove(position);
mTabs.add(position, tabInfo);
tabLayout.getTabAt(position).setTag(tabInfo);
}
public void createHistory()
{
int position = 0;
TOTAL_TABS = mTabs.size();
for (TabInfo mTab : mTabs)
{
if (history.get(position) == null)
{
history.put(position, new Stack<TabInfo>());
}
history.get(position).push(new TabInfo(mTab.fragmentClass, mTab.args));
position++;
}
}
public void back()
{
int position = tabLayout.getSelectedTabPosition();
if (!historyIsEmpty(position))
{
if (isLastItemInHistory(position))
{
mActivity.finish();
}
final TabInfo currentTabInfo = getPrevious(position);
mTabs.clear();
for (int i = 0; i < TOTAL_TABS; i++)
{
if (i == position)
{
mTabs.add(new TabInfo(currentTabInfo.fragmentClass, currentTabInfo.args));
}
else
{
TabInfo otherTabInfo = history.get(i).peek();
mTabs.add(new TabInfo(otherTabInfo.fragmentClass, otherTabInfo.args));
}
}
}
notifyDataSetChanged();
}
private boolean historyIsEmpty(final int position)
{
return history == null || history.isEmpty() || history.get(position).isEmpty();
}
private boolean isLastItemInHistory(final int position)
{
return history.get(position).size() == 1;
}
private TabInfo getPrevious(final int position)
{
TabInfo currentTabInfo = history.get(position).pop();
if (!history.get(position).isEmpty()) {
currentTabInfo = history.get(position).peek();
}
return currentTabInfo;
}
private static class TabInfo
{
public Class fragmentClass;
public Bundle args;
public TabInfo(Class fragmentClass, Bundle args)
{
this.fragmentClass = fragmentClass;
this.args = args;
}
public boolean equals(final Object o)
{
return this.fragmentClass.getName().equals(o.getClass().getName());
}
public int hashCode() {
return fragmentClass.getName() != null ? fragmentClass.getName().hashCode() : 0;
}
public String toString() {
return "TabInfo{" +
"fragmentClass=" + fragmentClass +
'}';
}
}
}
我需要在此适配器中调用方法replace()
和back()
。
这就是我在WindFragment
方法中使用setupViewPager()
调用此适配器的方法:
private void setupViewPager(ViewPager viewPager, TabLayout tabLayout)
{
adapter = new ViewPagerAdapter((AppCompatActivity)getActivity(),viewPager,tabLayout);
adapter.addTab(THGSFragment.class,null);
adapter.addTab(FlightPerformanceFragment.class,null);
adapter.addTab(AviationConverterFragment.class,null);
adapter.addTab(SpeedFragment.class,null);
adapter.createHistory();
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText(getString(R.string.thgs));
tabLayout.getTabAt(1).setText(getString(R.string.tcgs));
tabLayout.getTabAt(2).setText(getString(R.string.wv));
tabLayout.getTabAt(3).setText(getString(R.string.thtas));
}
现在我在这堂课:
public class THGSFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_thg, container, false);
ImageView button= (ImageView) rootView.findViewById(R.id.calcEAS);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
}
});
return rootView;
}
}
并且在onClick()
方法中我需要从适配器调用方法replace()
,但我不知道如何。
所以,我需要使用一个界面(我不好用)?
谢谢