Android Viewpager 内外间距

Android使用Viewpager_内外边距


代码:

1、adapter:

java 复制代码
<?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:background="@drawable/button_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20dp">

    <ImageView
        android:id="@+id/ivImg"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:textSize="24dp"
        android:textStyle="bold" />

</LinearLayout>
java 复制代码
public class VpAdapter extends PagerAdapter {

    private Context mContext;
    private List<VpModel> mList;

    public VpAdapter(Context context, List<VpModel> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_vp, container, false);
        ImageView img = itemView.findViewById(R.id.ivImg);
        TextView name = itemView.findViewById(R.id.tvName);
        Glide.with(mContext).load(mList.get(position).getUrl()).into(img);
        name.setText(mList.get(position).getName());
        container.addView(itemView);
        return itemView;
    }
}

2、UI

java 复制代码
<?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:clipChildren="false"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="VP Test"
        android:textSize="30dp" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="30dp"
        android:clipChildren="false" />

</LinearLayout>
java 复制代码
public class VpActivity extends ComponentActivity {

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

        List<VpModel> list = new ArrayList<>();
        list.add(new VpModel("https://st-gdx.dancf.com/gaodingx/1942/articles/0/20201117-160009-ff34.png","哦莫"));
        list.add(new VpModel("https://pic0.sucaisucai.com/11/50/11050520_2.jpg","伸手让世界闭嘴"));
        list.add(new VpModel("https://cdn.pixabay.com/photo/2015/03/18/11/02/sunset-679122_1280.jpg","街边的服务站"));
        ViewPager viewPager = findViewById(R.id.vp);
        viewPager.setOffscreenPageLimit(list.size());
        viewPager.setPageMargin(30);
        viewPager.setAdapter(new VpAdapter(this, list));
    }
}

3、数据类

java 复制代码
public class VpModel {

    private String url;
    private String name;

    public VpModel(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
相关推荐
Bervin1213813 分钟前
Flutter Android环境的搭建
android·flutter
e***87707 小时前
windows配置永久路由
android·前端·后端
fouryears_234178 小时前
现代 Android 后台应用读取剪贴板最佳实践
android·前端·flutter·dart
YF02119 小时前
Frida for MacBook/Android 安装配置
android·前端
雨白10 小时前
Android实战:构建高可维护的日志系统
android
茄子凉心11 小时前
android 开机启动App
android·java·开发语言
2501_9371931413 小时前
神马影视 8.8 版源码:4K 播放优化体验测评
android·源码·源代码管理·机顶盒
修炼者14 小时前
Kotlin中的Flow流
android·kotlin
洞见不一样的自己14 小时前
Android studio 编译问题
android
j***630814 小时前
SpringbootActuator未授权访问漏洞
android·前端·后端