最近工作需要一个效果 就是需要展示一屏多个view 使用viewpager实现这个功能 可以预览下一个view的一部分 效果图如下:
代码如下:先上activity
public class MyViewPagerActivity extends BaseActivity {
private ViewPager viewPager;
private MyViewPagerAdapter adapter;
private List<Integer> list;
private Context context;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
context = this;
addData();
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.page_magin)); //显示viewpager间距
viewPager.setOffscreenPageLimit(3);
adapter = new MyViewPagerAdapter(context, list);
viewPager.setAdapter(adapter);
}
/***
* 填充数据
*/
private void addData() {
list = new ArrayList<>();
list.add(R.color.red);
list.add(R.color.blue);
list.add(R.color.white);
list.add(R.color.colorPrimary);
}
接着适配器:
package com.example.administrator.demo.viewpager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.administrator.demo.R;
import java.util.List;
/**
* Created by Administrator on 2017/6/7 0007.
*/
public class MyViewPagerAdapter extends PagerAdapter {
private Context mContext;
private List<Integer> mList;
public MyViewPagerAdapter(Context context, List<Integer> list) {
this.mContext = context;
this.mList = list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_my_viewpager, null);
container.addView(view); //这一步很关键 把当前的imageview添加到适配器上
ImageView ivViewPager = (ImageView) view.findViewById(R.id.iv_viewpager);
ivViewPager.setBackgroundResource(mList.get(position));
Log.d("TAG", "position ==== " +mList.get(position));
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
@Override
public float getPageWidth(int position) {
return (float) 1;
}
}
xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_marginTop="@dimen/margin_size_50"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:layerType="software"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:clipChildren="false" />
</FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/corners_bg"
/>
</LinearLayout>
done!!!!!!!!!!