From c0883ec7781777b41398bf78a537beba065fa46f Mon Sep 17 00:00:00 2001 From: ittianyu <86839868@qq.com> Date: Fri, 6 Jan 2017 00:00:08 +0800 Subject: [PATCH] Optimize code in WithViewPagerActivity --- app/build.gradle | 4 +- .../viewpager/WithViewPagerActivity.java | 125 +++++++----------- build.gradle | 2 +- widget/build.gradle | 4 +- 4 files changed, 52 insertions(+), 83 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index becc844..7621a17 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -27,8 +27,8 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.0.0' - compile 'com.android.support:design:25.0.0' + compile 'com.android.support:appcompat-v7:25.0.1' + compile 'com.android.support:design:25.0.1' testCompile 'junit:junit:4.12' compile project(':widget') } diff --git a/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/viewpager/WithViewPagerActivity.java b/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/viewpager/WithViewPagerActivity.java index 5b70428..d0fa91d 100644 --- a/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/viewpager/WithViewPagerActivity.java +++ b/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/viewpager/WithViewPagerActivity.java @@ -5,14 +5,13 @@ import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentTransaction; -import android.support.v4.view.PagerAdapter; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; +import android.util.SparseArray; import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; import com.ittianyu.bottomnavigationviewexsample.R; import com.ittianyu.bottomnavigationviewexsample.databinding.ActivityWithViewPagerBinding; @@ -21,69 +20,79 @@ import java.util.List; public class WithViewPagerActivity extends AppCompatActivity { - private static final String TAG = "WithViewPagerActivity"; - private ActivityWithViewPagerBinding binding; - private List items; + private static final String TAG = WithViewPagerActivity.class.getCanonicalName(); + private ActivityWithViewPagerBinding bind; private VpAdapter adapter; + // collections + private SparseArray fragmentMap;// used for change ViewPager selected item + private List fragments;// used for ViewPager adapter + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_with_view_pager); - binding = DataBindingUtil.setContentView(this, R.layout.activity_with_view_pager); + bind = DataBindingUtil.setContentView(this, R.layout.activity_with_view_pager); initView(); initData(); initEvent(); } + /** + * change BottomNavigationViewEx style + */ private void initView() { - binding.bnve.enableItemShiftingMode(true); - binding.bnve.enableAnimation(false); + bind.bnve.enableItemShiftingMode(true); + bind.bnve.enableAnimation(false); } + /** + * create fragments + */ private void initData() { - items = new ArrayList<>(3); - // get transaction - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + fragments = new ArrayList<>(3); + fragmentMap = new SparseArray<>(3); // create music fragment and add it BaseFragment musicFragment = new BaseFragment(); Bundle bundle = new Bundle(); bundle.putString("title", getString(R.string.music)); musicFragment.setArguments(bundle); - transaction.add(musicFragment, "music"); // create backup fragment and add it BaseFragment backupFragment = new BaseFragment(); bundle = new Bundle(); bundle.putString("title", getString(R.string.backup)); backupFragment.setArguments(bundle); - transaction.add(backupFragment, "backup"); // create friends fragment and add it BaseFragment friendsFragment = new BaseFragment(); bundle = new Bundle(); bundle.putString("title", getString(R.string.friends)); friendsFragment.setArguments(bundle); - transaction.add(friendsFragment, "friends"); - // commit - transaction.commit(); + // add to fragments for adapter + fragments.add(musicFragment); + fragments.add(backupFragment); + fragments.add(friendsFragment); - // add fragment root view to view pager - items.add(new Item(R.id.menu_music, musicFragment)); - items.add(new Item(R.id.menu_backup, backupFragment)); - items.add(new Item(R.id.menu_friends, friendsFragment)); + // add to fragmentMap for change ViewPager item + fragmentMap.put(R.id.menu_music, 0); + fragmentMap.put(R.id.menu_backup, 1); + fragmentMap.put(R.id.menu_friends, 2); // set adapter - adapter = new VpAdapter(items); - binding.vp.setAdapter(adapter); + adapter = new VpAdapter(getSupportFragmentManager(), fragments); + bind.vp.setAdapter(adapter); } + /** + * set listeners + */ private void initEvent() { // set listener to change the current item of view pager when click bottom nav item - binding.bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { + bind.bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { // int id = 0; @@ -98,17 +107,17 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) { // id = 2; // break; // } -// binding.vp.setCurrentItem(id, false); +// bind.vp.setCurrentItem(id, false); // you can write as above. - // I recommend this method. You can change the item order or count without update code here. - binding.vp.setCurrentItem(items.indexOf(new Item(item.getItemId(), null)), false); - return false; + // I recommend this method. You can change the item order or counts without update code here. + bind.vp.setCurrentItem(fragmentMap.get(item.getItemId())); + return true; } }); // set listener to change the current checked item of bottom nav when scroll view pager - binding.vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + bind.vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { @@ -117,9 +126,9 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse @Override public void onPageSelected(int position) { // check whether current item is equal position - if (binding.bnve.getCurrentItem() != position) { + if (bind.bnve.getCurrentItem() != position) { // only set item when scroll view pager by hand - binding.bnve.setCurrentItem(position); + bind.bnve.setCurrentItem(position); Log.i(TAG, "setCurrentItem:" + position); } } @@ -134,10 +143,11 @@ public void onPageScrollStateChanged(int state) { /** * view pager adapter */ - private static class VpAdapter extends PagerAdapter { - private List data; + private static class VpAdapter extends FragmentPagerAdapter { + private List data; - public VpAdapter(List data) { + public VpAdapter(FragmentManager fm, List data) { + super(fm); this.data = data; } @@ -147,50 +157,9 @@ public int getCount() { } @Override - public boolean isViewFromObject(View view, Object object) { - return view == object; - } - - @Override - public Object instantiateItem(ViewGroup container, int position) { - View view = data.get(position).fragment.getView(); - container.addView(view); - return view; - } - - @Override - public void destroyItem(ViewGroup container, int position, Object object) { - container.removeView((View) object); + public Fragment getItem(int position) { + return data.get(position); } } - /** - * used for view pager data bean - */ - private static class Item { - int resId; - Fragment fragment; - - public Item(int resId, Fragment fragment) { - this.resId = resId; - this.fragment = fragment; - } - - // rewrite equal and hashcode by resId - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Item item = (Item) o; - - return resId == item.resId; - - } - - @Override - public int hashCode() { - return resId; - } - } } diff --git a/build.gradle b/build.gradle index 51bf9c1..df4cca9 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.2' + classpath 'com.android.tools.build:gradle:2.2.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/widget/build.gradle b/widget/build.gradle index 90bfa14..c1a2050 100644 --- a/widget/build.gradle +++ b/widget/build.gradle @@ -29,7 +29,7 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.0.0' - compile 'com.android.support:design:25.0.0' + compile 'com.android.support:appcompat-v7:25.0.1' + compile 'com.android.support:design:25.0.1' testCompile 'junit:junit:4.12' }