怎么在Android中使用ViewPager和Fragment实现滑动页面效果
怎么在Android中使用ViewPager 和 Fragment实现滑动页面效果?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
“只有客户发展了,才有我们的生存与发展!”这是创新互联建站的服务宗旨!把网站当作互联网产品,产品思维更注重全局思维、需求分析和迭代思维,在网站建设中就是为了建设一个不仅审美在线,而且实用性极高的网站。创新互联对成都网站设计、做网站、网站制作、网站开发、网页设计、网站优化、网络推广、探索永无止境。
PagerData类:
package com.cloud.viewpagerdemo; import java.io.Serializable; class PagerData implements Serializable { private int mImageResId; private String mContent; PagerData(int imageResId, String content) { mImageResId = imageResId; mContent = content; } public int getImageResId() { return mImageResId; } public void setImageResId(int imageResId) { mImageResId = imageResId; } public String getContent() { return mContent; } public void setContent(String content) { mContent = content; } }
PagerFragment类:
package com.cloud.viewpagerdemo; import android.graphics.Outline; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewOutlineProvider; import android.widget.ImageView; import android.widget.TextView; public class PagerFragment extends Fragment { private static final String ARG_DATA = "data"; private PagerData mData; private ImageView mImageView; private TextView mContent; /** * 通过静态方法获取Fragment实例,向Fragment传参数 * @param data 数据 * @return Fragment */ public static PagerFragment newInstance(PagerData data) { PagerFragment fragment = new PagerFragment(); //使用FragmentArguments传递参数 Bundle args = new Bundle(); args.putSerializable(ARG_DATA, data); fragment.setArguments(args); return fragment; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mData = (PagerData) getArguments().getSerializable(ARG_DATA); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_pager, container, false); //绑定控件 mImageView = view.findViewById(R.id.image_view); mContent = view.findViewById(R.id.text_content); //ImageView设置圆角 ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { outline.setRoundRect(0, 0, mImageView.getWidth(), mImageView.getHeight(), 16); } }; mImageView.setOutlineProvider(viewOutlineProvider); mImageView.setClipToOutline(true); mImageView.setImageResource(mData.getImageResId()); mContent.setText(mData.getContent()); return view; } }
PagerActivity:
package com.cloud.viewpagerdemo; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class PagerActivity extends AppCompatActivity { private ListmPagerData = new ArrayList<>(); private ViewPager mViewPager; private TextView mTextPage; private String page = "1 / 3"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pager); initData(); FragmentManager fragmentManager = getSupportFragmentManager(); mViewPager = findViewById(R.id.view_pager); mTextPage = findViewById(R.id.text_page); //设置ViewPager适配器 mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) { @Override public Fragment getItem(int position) { return PagerFragment.newInstance(mPagerData.get(position)); } @Override public int getCount() { return mPagerData.size(); } }); //监听页面改变 mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //页面选中 page = String.valueOf(position + 1) + " / " + String.valueOf(mPagerData.size()); mTextPage.setText(page); } @Override public void onPageScrollStateChanged(int state) { } }); //设置缓存页数 mViewPager.setOffscreenPageLimit(3); //设置默认页 mViewPager.setCurrentItem(0); mTextPage.setText(page); } private void initData() { //添加三项数据 mPagerData.add(new PagerData(R.drawable.img_0, "Photo by Adam Krowitz")); mPagerData.add(new PagerData(R.drawable.img_1, "Photo by Samuel Ferrara")); mPagerData.add(new PagerData(R.drawable.img_2, "Photo by Earth")); } }
fragment_pager:
activity_pager:
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
本文标题:怎么在Android中使用ViewPager和Fragment实现滑动页面效果
URL分享:http://ybzwz.com/article/jeghsi.html