今天学习了制作轮播图效果,实现了最基础的版本:单击实现轮播且不能无线滑动,即划到最后一张再点击就没有效果了
实现原理:与向listview中添加数据有很大相似之处
activity_main.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:tools="http://schemas.android.com/tools" tools:content=".MainActivity"> <androidx.viewpager.widget.ViewPager android:id="@+id/im1" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY"/> </RelativeLayout>
item,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"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/im2"/> </LinearLayout>
Activity.java
package com.example.myapplication23; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private List<Integer>list=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initData(); } private void initViews() { //加载布局 viewPager = (ViewPager) findViewById(R.id.im1); viewPager.setAdapter(my); } private PagerAdapter my=new PagerAdapter(){ @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view==object; } //Alt+Insert @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { View item= LayoutInflater.from(container.getContext()).inflate(R.layout.item,container,false); ImageView iv=item.findViewById(R.id.im2); //设置数据 iv.setImageResource(list.get(position)); if(iv.getParent() instanceof ViewGroup){ ((ViewGroup)iv.getParent()).removeView(iv); } container.addView(iv); return iv; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View)object); } }; private void initData() { //加载数据 list.add(R.mipmap.a1); list.add(R.mipmap.a2); list.add(R.mipmap.a3); my.notifyDataSetChanged(); } }