ViewPager 一屏显示多个效果

这里写图片描述
项目中我们经常要求实现像上图那种ViewPager中一屏显示多个页面的做法,下面来看怎么实现这种效果。
首先我们先了解android:clipChildren=”false”,这个属性,默认是true。
使用的时候给子View和根节点View控件都设置android:clipChildren=”false”,那么这个子View就不会限制在父View当中,会产生那种旁边页溢出的效果。
设置ViewPager android:layout_marginLeft=”28dp” android:layout_marginRight=”28dp”,然后再代码中设置 mViewpager.setPageMargin(60);//设置页与页之间的间距,具体如下
这里写图片描述
代码:

`public class ViewPagerZoomActivity extends FragmentActivity {
private ViewPager mViewpager;

private ArrayList<ViewPagerItemFragment> mList = new ArrayList<>();

private ViewPagerAnimationAdapter viewPagerAnimationAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_pager_zoom);
    mViewpager = (ViewPager) findViewById(R.id.viewpager);
    initData();
}

private void initData() {
    addPage(ViewPagerItemFragment.PAGE_ONE);
    addPage(ViewPagerItemFragment.PAGE_TWO);
    addPage(ViewPagerItemFragment.PAGE_THREE);
    addPage(ViewPagerItemFragment.PAGE_FOUR);

    viewPagerAnimationAdapter = new ViewPagerAnimationAdapter(getSupportFragmentManager());
    viewPagerAnimationAdapter.setList(mList);
    mViewpager.setAdapter(viewPagerAnimationAdapter);
    mViewpager.setOffscreenPageLimit(2);//设置缓存页数
    mViewpager.setPageMargin(60);//设置页与页之间的间距
}

private void addPage(int page) {

    Bundle bundle = new Bundle();
    bundle.putInt("fromWhere", page);
    ViewPagerItemFragment buyRecordItemFragmentOne = ViewPagerItemFragment.newInstance(bundle);
    mList.add(buyRecordItemFragmentOne);


}

}`

布局文件

<?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:id="@+id/activity_view_pager_zoom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipChildren="false"
    android:orientation="vertical">


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"
            android:layout_marginTop="40dp"
            android:clipChildren="false" />


</LinearLayout>

在根view和子View中要设置 android:clipChildren=”false”。

ViewPager中的Fragment

public class ViewPagerItemFragment extends Fragment {


    public static final int PAGE_ONE = 0;

    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;

    private TextView tvName;


    /**
     * model
     *
     * @param bundle bundle值
     * @return 对象
     */
    public static ViewPagerItemFragment newInstance(Bundle bundle) {
        ViewPagerItemFragment fragment = new ViewPagerItemFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    public ViewPagerItemFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = LayoutInflater.from(getActivity()).inflate(
                R.layout.buyrecord_itempager_layout, null);

        tvName = (TextView) view.findViewById(R.id.tv_name);


        Bundle bundle = getArguments();

        if (bundle != null) {

            int fromWhere = bundle.getInt("fromWhere");

            if (fromWhere == PAGE_ONE) {

                tvName.setText("one Page");

            } else if (fromWhere == PAGE_TWO) {

                tvName.setText("two Page");
            } else if (fromWhere == PAGE_THREE) {

                tvName.setText("three Page");
            } else if (fromWhere == PAGE_FOUR) {

                tvName.setText("four Page");
            }

        }

        return view;

    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }


}

Fragment布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_color_cornner_solid_yellow"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="dssf" />


</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值