Skip to content

Commit

Permalink
Optimize code in WithViewPagerActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
ittianyu committed Jan 5, 2017
1 parent 94ca7ea commit c0883ec
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 83 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,69 +20,79 @@
import java.util.List;

public class WithViewPagerActivity extends AppCompatActivity {
private static final String TAG = "WithViewPagerActivity";
private ActivityWithViewPagerBinding binding;
private List<Item> items;
private static final String TAG = WithViewPagerActivity.class.getCanonicalName();
private ActivityWithViewPagerBinding bind;
private VpAdapter adapter;

// collections
private SparseArray<Integer> fragmentMap;// used for change ViewPager selected item
private List<Fragment> 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;
Expand All @@ -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) {

Expand All @@ -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);
}
}
Expand All @@ -134,10 +143,11 @@ public void onPageScrollStateChanged(int state) {
/**
* view pager adapter
*/
private static class VpAdapter extends PagerAdapter {
private List<Item> data;
private static class VpAdapter extends FragmentPagerAdapter {
private List<Fragment> data;

public VpAdapter(List<Item> data) {
public VpAdapter(FragmentManager fm, List<Fragment> data) {
super(fm);
this.data = data;
}

Expand All @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions widget/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

0 comments on commit c0883ec

Please sign in to comment.