1.概念: 2.代码示例: (1)Java代码:
package com.example.test;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
ViewPager myViewPager;
List<View> viewList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewPager = (ViewPager) findViewById(R.id.my_view_pager);
{
View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager,null);
ImageView imageView = view.findViewById(R.id.item_image_view);
imageView.setBackgroundColor(Color.RED);
viewList.add(view);
}
{
View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager,null);
ImageView imageView = view.findViewById(R.id.item_image_view);
imageView.setBackgroundColor(Color.BLUE);
viewList.add(view);
}
{
View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager,null);
ImageView imageView = view.findViewById(R.id.item_image_view);
imageView.setBackgroundColor(Color.GREEN);
viewList.add(view);
}
myViewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(viewList.get(position));
}
});
}
}
(2)布局代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:collapseColumns="1"
android:stretchColumns="1,2"
android:shrinkColumns="1">
<androidx.viewpager.widget.ViewPager
android:id="@+id/my_view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:overScrollMode="never">
</androidx.viewpager.widget.ViewPager>
<!--android:overScrollMode: 控制滑动产生的阴影部分-->
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/item_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
</LinearLayout>
(3)图片: 图1: 图2:
|