您的位置:首页 > 移动开发 > Android开发

Android ViewPager加Fragment实现滑动或者点击页面切换

2016-08-23 18:09 671 查看
1.布局文件添加android.support.v4.ViewPager
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yuf.myapplication.MainActivity">

<LinearLayout
android:id="@+id/vp_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/title_tv_recomend"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text=""
android:layout_weight="1"
android:gravity="center"
android:drawableBottom="#000000"
/>

<TextView
android:id="@+id/title_tv_new"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text=""
android:layout_weight="1"
android:gravity="center"
/>

</LinearLayout>
<LinearLayout
android:id="@+id/lin_index"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@+id/vp_title">
<ImageView
android:id="@+id/im_index"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/holo_blue_dark"
/>
</LinearLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/lin_index"
android:layout_marginTop="5dp"
></android.support.v4.view.ViewPager>

</RelativeLayout>


然后添加java代码

设置TextView下边的下标标签图片

//TextView下边的标签图片的设置
public void setIm_index(){
DisplayMetrics dpMetrics = new DisplayMetrics();
getWindow().getWindowManager().getDefaultDisplay()
.getMetrics(dpMetrics);
screenWidth = dpMetrics.widthPixels;
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) im_index
.getLayoutParams();
lp.width = screenWidth / 2;
im_index.setLayoutParams(lp);
}添加Adapter继承自FragmentPagerAdapter
//添加继承自FragmentPagerAdapter的pageradapter
class MyPagerAdapter extends FragmentPagerAdapter{

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}

@Override
public int getCount() {
return fragmentList.size();
}
}


添加pager切换的监听
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) im_index
.getLayoutParams();
//页面切换控制地下的标签图片
if (currentIndex == 0 && position == 0)// 0->1
{
lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 2) + currentIndex
* (screenWidth / 2));

} else if (currentIndex == 1 && position == 0) // 1->0
{
lp.leftMargin = (int) (-(1 - positionOffset)
* (screenWidth * 1.0 / 2) + currentIndex
* (screenWidth / 2));

}
im_index.setLayoutParams(lp);
}

@Override
public void onPageSelected(int position) {

title_tv_recomend.setTextColor(Color.BLACK);
title_tv_new.setTextColor(Color.BLACK);
switch (position){
case 0:
title_tv_recomend.setTextColor(Color.BLUE);
break;
case 1:
title_tv_new.setTextColor(Color.BLUE);
break;
}
currentIndex = position;

}

@Override
public void onPageScrollStateChanged(int state) {

}
});

下面是全部代码   两个Fragment就不上代码了

public class MainActivity extends AppCompatActivity {

private ViewPager viewpager;
private ImageView im_index;
private TextView title_tv_recomend;
private TextView title_tv_new;
private RecomentFragment mRecomentFragment;
private NewFragment mNewFragment;
private MyPagerAdapter pagerAdapter;
private List<Fragment> fragmentList;
private int screenWidth;
private int currentIndex = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();

}

public void initView(){
viewpager = (ViewPager) findViewById(R.id.viewpager);
im_index = (ImageView) findViewById(R.id.im_index);
title_tv_recomend = (TextView) findViewById(R.id.title_tv_recomend);
title_tv_new = (TextView) findViewById(R.id.title_tv_new);
title_tv_recomend.setText("推荐");
title_tv_new.setText("最新");

mRecomentFragment = new RecomentFragment();
mNewFragment = new NewFragment();
fragmentList = new ArrayList<>();
//把两个fragment添加到List
fragmentList.add(mRecomentFragment);
fragmentList.add(mNewFragment);
pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
//设置默认加载的标签页
viewpager.setCurrentItem(currentIndex);
viewpager.setAdapter(pagerAdapter);
setIm_index();

//页面切换监听
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) im_index
.getLayoutParams();
//页面切换控制地下的标签图片
if (currentIndex == 0 && position == 0)// 0->1
{
lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 2) + currentIndex
* (screenWidth / 2));

} else if (currentIndex == 1 && position == 0) // 1->0
{
lp.leftMargin = (int) (-(1 - positionOffset)
* (screenWidth * 1.0 / 2) + currentIndex
* (screenWidth / 2));

}
im_index.setLayoutParams(lp);
}

@Override
public void onPageSelected(int position) {

title_tv_recomend.setTextColor(Color.BLACK);
title_tv_new.setTextColor(Color.BLACK);
switch (position){
case 0:
title_tv_recomend.setTextColor(Color.BLUE);
break;
case 1:
title_tv_new.setTextColor(Color.BLUE);
break;
}
currentIndex = position;

}

@Override
public void onPageScrollStateChanged(int state) {

}
});

title_tv_recomend.setOnClickListener(new View.OnClickListener() {
@Override

4000
public void onClick(View v) {
currentIndex = 0;
viewpager.setCurrentItem(currentIndex);
}
});
title_tv_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentIndex = 1;
viewpager.setCurrentItem(currentIndex);
}
});
}
//TextView下边的标签图片的设置
public void setIm_index(){
DisplayMetrics dpMetrics = new DisplayMetrics();
getWindow().getWindowManager().getDefaultDisplay()
.getMetrics(dpMetrics);
screenWidth = dpMetrics.widthPixels;
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) im_index
.getLayoutParams();
lp.width = screenWidth / 2;
im_index.setLayoutParams(lp);
}

//添加继承自FragmentPagerAdapter的pageradapter
class MyPagerAdapter extends FragmentPagerAdapter{

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}

@Override
public int getCount() {
return fragmentList.size();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android viewpager
相关文章推荐