如何在Android中自定义ViewPager

时间:2017-05-05 13:39:33

标签: android android-viewpager

我想用ViewPager创建此设计。 enter image description here

如何自定义viewpager并创建此内容?

2 个答案:

答案 0 :(得分:0)

您需要根据要求 android ,app中实施滑块code

上有一个例子

答案 1 :(得分:0)

步骤1.创建一个包含viewpager photo_acitivity.xml的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:background="@color/deep_purple">
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

步骤2。创建viewPager适配器的布局项目适配器是item_adapter_photo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/img_item"
        android:layout_width="match_parent"
        android:background="@drawable/nen"
        android:scaleType="fitXY"
        android:contentDescription="@string/txt_setting"
        android:layout_height="match_parent" />
</RelativeLayout>

第3步。在Android中创建名称为:CustomViewPagerAdapter.class

的Adapter viewpager类。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.anhttvn.a3droselivewallpaper.R;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
 * adapter view pager
 */
public class AdapterViewPager extends PagerAdapter implements View.OnClickListener {
    private Context mContext;
    private ArrayList<String> listPhoto;
    private Onclick mOnclick;
    public AdapterViewPager(Context context, ArrayList<String> list,Onclick click){
        mContext =context;
        listPhoto = list;
        mOnclick = click;
    }
    @Override
    public int getCount() {
        return listPhoto.size();
    }
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == ((RelativeLayout) object);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_photo_adapter, container, false);
        ImageView imageView = itemView.findViewById(R.id.img_item);
        InputStream inputstream= null;
        try {
            inputstream = mContext.getAssets().open("image/"
                    +listPhoto.get(position));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Drawable drawable = Drawable.createFromStream(inputstream, null);
        imageView.setImageDrawable(drawable);
        container.addView(itemView);

        imageView.setOnClickListener(this);
        imageView.setTag(position);
        return itemView;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((RelativeLayout) object);
    }
    @Override
    public void onClick(View view) {
        int position = Integer.parseInt(view.getTag() + "");
        switch (view.getId()){
            case R.id.img_item:
                mOnclick.clickSelect(position);
                break;
        }
    }
    public interface Onclick{
        void clickSelect(int position);
    }
}

文章来源: How to custom viewPagerAdapter android