Android ViewPager详解之在FragmentPagerAdapter和PagerAdapter中自如切换

释放双眼,带上耳机,听听看~!

在向前的路上也要时不时回头看看基础 ————-出自《小编本人》

本文就是对于Viewpager用法的详细介绍 不多比比 直接上主菜

在这里插入图片描述在这里插入图片描述

在Activity中使用FragmentPagerAdapter和PagerAdapter实现viewpager切换的效果(效果图在上面)

1.Activity中使用PagerAdapter实现切换代码

主活动

public class ViewPagerManager extends AppCompatActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_manager);
viewPager = findViewById(R.id.viewpager);
initView();
}
private void initView() {
List<String> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add("第"+i+"个View");
}
viewPager.setAdapter(new Adapter(this,list));
}
}

activity_view_pager_manager布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPagerManager">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

adapter:

public class Adapter extends PagerAdapter {
private Context mContext;
private List<String> mData;
public Adapter(Context context ,List<String> list) {
mContext = context;
mData = list;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = View.inflate(mContext, R.layout.activity_view_pager_item,null);
TextView tv = view.findViewById(R.id.textaa);
tv.setText(mData.get(position));
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}

activity_view_pager_item布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textaa"
android:gravity="center"
android:text="sadsadsa"
>
</TextView>

Activity中使用FragmentPagerAdapter实现切换

主活动

public class ViewPagerSecondManager extends AppCompatActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_second_manager);
viewPager = findViewById(R.id.viewpager1);
viewPager.setAdapter(new SecondAdapter(getSupportFragmentManager()));
}
}

adapter:

public class SecondAdapter extends  FragmentPagerAdapter{
ArrayList<Fragment> list;
//通过构造获取list集合
public SecondAdapter(FragmentManager fm) {
super(fm);
}
//设置每一个的内容
@Override
public Fragment getItem(int arg0) {
return new DefaultFragment();//你要显示的东西显示在这里面就可以了 (Fragment)
// return list.get(arg0);
}
//设置有多少内容
@Override
public int getCount() {
return 3;
}
}

使用Fragment实现FragmentPagerAdapter实现切换

主活动:

public class PagerFragment extends Fragment {
private ViewPager viewPager;
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view =  inflater.inflate(R.layout.fragment_pager, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewPager = view.findViewById(R.id.viewpager2);
viewPager.setAdapter(new ViewPagerThirdManagerAdapter(getChildFragmentManager()));
}
}

fragment_pager布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".PagerFragment">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager2"
android:layout_width="match_parent"
android:layout_height="match_parent"></androidx.viewpager.widget.ViewPager>
</LinearLayout>

adapter:

public class ViewPagerThirdManagerAdapter extends FragmentPagerAdapter {
public ViewPagerThirdManagerAdapter(FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
return new DefaultFragment();
}
@Override
public int getCount() {
return 3;
}
}

ok!以上就是使用FragmentPagerAdapter和PagerAdapter简单的实现Viewpager切换的的效果,你可以根据你的需求进行改进

拓展:(以下是小编运用kotlin之前写的项目中引用的,懒就不单独写demo了 )

class KnolagePagerAdapter(val list: List<KonlageEntity.DataBean.ChildrenBean>,fm: FragmentManager?) : FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
var fragment = KnolageShowPagerFragment()//向指定的Fragment中传递参数
var bundle = Bundle()
bundle.putString("wakaka",list.get(position).id)
fragment.arguments = bundle
return fragment
}
override fun getCount(): Int {
return list.size
}
override fun getPageTitle(position: Int): CharSequence? {
return list.get(position).name
}
}

指定的Fragment中接收参数:

 var cid = getArguments()?.getString("wakaka")

好啦,本期知识就到这里啦,共同见证进步,是我最大的愿望 一起加油!!

人已赞赏
Android文章

Android新特性介绍,ConstraintLayout完全解析

2020-3-3 14:49:21

Android文章

Android kotlin AlertDialog使用方法以及kotlin点击事件和长按事件冲突

2020-3-3 16:45:08

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索