Last active
May 31, 2017 09:14
-
-
Save z8888q/056ff15d7bfa5fff57985bf4f08e907b to your computer and use it in GitHub Desktop.
Implement dynamic delete or add tab(TAB_C in this test code). Initial Conditions: use Android Studio 2.3.2 create a new Project with a Tabbed Activity, and the Navigation style is Action Bar Tabs(with ViewPager).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zq.testviewpager; | |
import android.support.annotation.Nullable; | |
import android.support.design.widget.TabLayout; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.design.widget.Snackbar; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
/** | |
* Created by [email protected] on 2017/5/31. | |
* Implement dynamic delete or add tab(TAB_C in this test code). | |
* Initial Conditions: use Android Studio 2.3.2 create a new Project with a Tabbed Activity, and the Navigation style is Action Bar Tabs(with ViewPager). | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
private static final int TAB_A = 1001; | |
private static final int TAB_B = 1002; | |
private static final int TAB_C = 1003; | |
private static final int TAB_D = 1004; | |
private static final int TAB_E = 1005; | |
private Tab[] tabsArray = new Tab[]{new Tab(TAB_A, "A"),new Tab(TAB_B, "B"),new Tab(TAB_C, "C"),new Tab(TAB_D, "D"),new Tab(TAB_E, "E")}; | |
private ArrayList<Tab> mTabs = new ArrayList<>(Arrays.asList(tabsArray)); | |
private Tab[] tabsArray2 = new Tab[]{new Tab(TAB_A, "A"),new Tab(TAB_B, "B"),new Tab(TAB_D, "D"),new Tab(TAB_E, "E")}; | |
private ArrayList<Tab> mTabs2 = new ArrayList<>(Arrays.asList(tabsArray2)); | |
/** | |
* The {@link android.support.v4.view.PagerAdapter} that will provide | |
* fragments for each of the sections. We use a | |
* {@link FragmentPagerAdapter} derivative, which will keep every | |
* loaded fragment in memory. If this becomes too memory intensive, it | |
* may be best to switch to a | |
* {@link android.support.v4.app.FragmentStatePagerAdapter}. | |
*/ | |
private SectionsPagerAdapter mSectionsPagerAdapter; | |
/** | |
* The {@link ViewPager} that will host the section contents. | |
*/ | |
private ViewPager mViewPager; | |
private TabLayout tabLayout; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
// Create the adapter that will return a fragment for each of the three | |
// primary sections of the activity. | |
mSectionsPagerAdapter = new SectionsPagerAdapter(mTabs, getSupportFragmentManager()); | |
// Set up the ViewPager with the sections adapter. | |
mViewPager = (ViewPager) findViewById(R.id.container); | |
mViewPager.setAdapter(mSectionsPagerAdapter); | |
tabLayout = (TabLayout) findViewById(R.id.tabs); | |
tabLayout.setupWithViewPager(mViewPager); | |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show(); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
}else if (id == R.id.action_delete) { | |
int currentItemPosition = mViewPager.getCurrentItem(); | |
Tab currentTab = mTabs.get(currentItemPosition); | |
if(currentTab.id == TAB_C){ | |
currentTab = mTabs.get(currentItemPosition == 0 ? currentItemPosition +1 : currentItemPosition - 1); | |
} | |
mSectionsPagerAdapter = new SectionsPagerAdapter(mTabs2, getSupportFragmentManager()); | |
mViewPager.setAdapter(mSectionsPagerAdapter); | |
tabLayout.setupWithViewPager(mViewPager); | |
mViewPager.setCurrentItem(mTabs2.indexOf(currentTab), false); | |
return true; | |
}else if (id == R.id.action_add) { | |
int currentItemPosition = mViewPager.getCurrentItem(); | |
Tab currentTab = mTabs2.get(currentItemPosition); | |
mSectionsPagerAdapter = new SectionsPagerAdapter(mTabs, getSupportFragmentManager()); | |
mViewPager.setAdapter(mSectionsPagerAdapter); | |
tabLayout.setupWithViewPager(mViewPager); | |
mViewPager.setCurrentItem(mTabs.indexOf(currentTab), false); | |
return true; | |
}else | |
return super.onOptionsItemSelected(item); | |
} | |
/** | |
* A placeholder fragment containing a simple view. | |
*/ | |
public static class PlaceholderFragment extends Fragment { | |
/** | |
* The fragment argument representing the section number for this | |
* fragment. | |
*/ | |
private static final String ARG_SECTION_NUMBER = "section_number"; | |
public PlaceholderFragment() { | |
} | |
/** | |
* Returns a new instance of this fragment for the given section | |
* number. | |
*/ | |
public static PlaceholderFragment newInstance(int sectionNumber) { | |
PlaceholderFragment fragment = new PlaceholderFragment(); | |
Bundle args = new Bundle(); | |
args.putInt(ARG_SECTION_NUMBER, sectionNumber); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Log.e("TestViewPager", "onCreate"+getArguments().getInt(ARG_SECTION_NUMBER)); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
Log.e("TestViewPager", "onDestroy"+getArguments().getInt(ARG_SECTION_NUMBER)); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.fragment_main, container, false); | |
TextView textView = (TextView) rootView.findViewById(R.id.section_label); | |
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); | |
return rootView; | |
} | |
} | |
/** | |
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to | |
* one of the sections/tabs/pages. | |
*/ | |
public class SectionsPagerAdapter extends FragmentPagerAdapter { | |
ArrayList<Tab> tabs; | |
public SectionsPagerAdapter(ArrayList<Tab> tabs, FragmentManager fm) { | |
super(fm); | |
this.tabs = tabs; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
// getItem is called to instantiate the fragment for the given page. | |
// Return a PlaceholderFragment (defined as a static inner class below). | |
return PlaceholderFragment.newInstance(tabs.get(position).id); | |
} | |
@Override | |
public int getCount() { | |
return tabs.size(); | |
} | |
@Override | |
public long getItemId(int position) { | |
return tabs.get(position).id; | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
return tabs.get(position).title; | |
} | |
} | |
private static class Tab { | |
String title; | |
public int id; | |
Tab(int id, String title){ | |
this.id = id; | |
this.title = title; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if(obj instanceof Tab){ | |
return ((Tab)obj).id == id; | |
}else{ | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment