viewpager
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package com.example.ninefourone.nutritionmaster.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.modules.viewpagerfragments.page1.Page1;
|
||||
import com.example.ninefourone.nutritionmaster.modules.viewpagerfragments.page2.BodyInformationFragment;
|
||||
import com.example.ninefourone.nutritionmaster.modules.viewpagerfragments.page3.Page3;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Wangtianrui on 2018/5/1.
|
||||
*/
|
||||
|
||||
public class HomePagerAdapter extends FragmentPagerAdapter {
|
||||
private final String[] TITLES;
|
||||
private Fragment[] fragments;
|
||||
|
||||
public HomePagerAdapter(FragmentManager fm, Context context) {
|
||||
super(fm);
|
||||
TITLES = context.getResources().getStringArray(R.array.sections);
|
||||
fragments = new Fragment[TITLES.length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
if (fragments[position] == null) {
|
||||
switch (position) {
|
||||
case 0:
|
||||
fragments[position] = Page1.getInstance();
|
||||
break;
|
||||
case 1:
|
||||
fragments[position] = BodyInformationFragment.getInstance();
|
||||
break;
|
||||
case 2:
|
||||
fragments[position] = Page3.getInstance();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return fragments[position];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return TITLES.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
return TITLES[position];
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ public abstract class BaseFragment extends Fragment {
|
||||
public abstract void initView(Bundle state);
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
@@ -3,8 +3,12 @@ package com.example.ninefourone.nutritionmaster.modules;
|
||||
import android.os.Bundle;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.adapter.HomePagerAdapter;
|
||||
import com.example.ninefourone.nutritionmaster.base.BaseActivity;
|
||||
import com.example.ninefourone.nutritionmaster.ui.NoScrollViewPager;
|
||||
import com.flyco.tablayout.SlidingTabLayout;
|
||||
import com.mxn.soul.flowingdrawer_core.ElasticDrawer;
|
||||
import com.mxn.soul.flowingdrawer_core.FlowingDrawer;
|
||||
|
||||
@@ -18,6 +22,10 @@ public class MainActivity extends BaseActivity {
|
||||
FlowingDrawer mDrawer;
|
||||
@BindView(R.id.navigation_layout)
|
||||
LinearLayout navigationLayout;
|
||||
@BindView(R.id.view_pager)
|
||||
NoScrollViewPager viewPager;
|
||||
@BindView(R.id.sliding_tab_layout)
|
||||
SlidingTabLayout slidingTabLayout;
|
||||
|
||||
|
||||
@Override
|
||||
@@ -41,6 +49,20 @@ public class MainActivity extends BaseActivity {
|
||||
// Logger.i("openRatio=" + openRatio + " ,offsetPixels=" + offsetPixels);
|
||||
}
|
||||
});
|
||||
initViewPager();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化ViewPager
|
||||
*/
|
||||
private void initViewPager() {
|
||||
HomePagerAdapter homePagerAdapter = new HomePagerAdapter(getSupportFragmentManager(), this);
|
||||
viewPager.setOffscreenPageLimit(3);
|
||||
viewPager.setAdapter(homePagerAdapter);
|
||||
// viewPager.setPageTransformer(true, new RotateUpTransformer());
|
||||
slidingTabLayout.setViewPager(viewPager);
|
||||
viewPager.setCurrentItem(1);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.ninefourone.nutritionmaster.modules.viewpagerfragments.page1;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.base.BaseFragment;
|
||||
|
||||
/**
|
||||
* Created by ScorpioMiku on 2018/8/26.
|
||||
*/
|
||||
|
||||
public class Page1 extends BaseFragment {
|
||||
@Override
|
||||
public int getLayoutResId() {
|
||||
return R.layout.page_1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initView(Bundle state) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static BaseFragment getInstance() {
|
||||
return new Page1();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.ninefourone.nutritionmaster.modules.viewpagerfragments.page2;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.base.BaseFragment;
|
||||
|
||||
/**
|
||||
* Created by ScorpioMiku on 2018/8/26.
|
||||
*/
|
||||
|
||||
public class BodyInformationFragment extends BaseFragment {
|
||||
@Override
|
||||
public int getLayoutResId() {
|
||||
return R.layout.page_2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initView(Bundle state) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static BaseFragment getInstance() {
|
||||
return new BodyInformationFragment();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.ninefourone.nutritionmaster.modules.viewpagerfragments.page3;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.base.BaseFragment;
|
||||
|
||||
/**
|
||||
* Created by ScorpioMiku on 2018/8/26.
|
||||
*/
|
||||
|
||||
public class Page3 extends BaseFragment {
|
||||
@Override
|
||||
public int getLayoutResId() {
|
||||
return R.layout.page_3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initView(Bundle state) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static BaseFragment getInstance() {
|
||||
return new Page3();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.example.ninefourone.nutritionmaster.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
/**
|
||||
* Created by Wangtianrui on 2018/5/1.
|
||||
* 重写Viewpager解决点击tab去除滑动动画效果的问题
|
||||
*/
|
||||
public class NoScrollViewPager extends ViewPager {
|
||||
public NoScrollViewPager(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public NoScrollViewPager(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scrollTo(int x, int y) {
|
||||
super.scrollTo(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentItem(int item, boolean smoothScroll) {
|
||||
super.setCurrentItem(item, smoothScroll);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentItem(int item) {
|
||||
super.setCurrentItem(item, false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user