Android轮播图
1.前言
其实做这个也是无奈之举,毕竟自己心在java,但别人给你安排任务你也不能不做啊。
2.直接上代码
可以直接在AS上运行;
Mainactivity.java
package com.example.myapplication1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ViewPager loopPager;
private LooperPagerAdapter looperPagerAdapter;
private static List<Integer>spic = new ArrayList<>();
private Handler handler;
static {
spic.add(R.drawable.ic_launcher_1);
spic.add(R.drawable.ic_launcher_2);
spic.add(R.drawable.ic_launcher_3);
spic.add(R.drawable.ic_launcher_4);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
handler = new Handler();
// Random random = new Random();
// for (int i = 0; i < 5; i++) {
// colos.add(Color.argb(random.nextInt(255),random.nextInt(255),random.nextInt(255),random.nextInt(255)));
//
//
// }
// looperPagerAdapter.setData(colos);
// looperPagerAdapter.notifyDataSetChanged();
}
public void onAttachedToWindow(){
super.onAttachedToWindow();
handler.post(looperTask);
}
public void onDetachedFromWindow(){
super.onDetachedFromWindow();
handler.removeCallbacks(looperTask);
}
private Runnable looperTask = new Runnable() {
@Override
public void run() {
int currentItem = loopPager.getCurrentItem();
loopPager.setCurrentItem(++currentItem,true);
handler.postDelayed(this,2000);
}
};
private void initView(){
//找到这个控件
loopPager = this.findViewById(R.id.loop_pager);
//2.给这个控件设置适配器
looperPagerAdapter = new LooperPagerAdapter();
looperPagerAdapter.setData(spic);
loopPager.setAdapter(looperPagerAdapter);
loopPager.setCurrentItem(looperPagerAdapter.getDataRealSize()*100,false);
}
LooperPagerAdapter.java
package com.example.myapplication1;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;
public class LooperPagerAdapter extends PagerAdapter {
private List<Integer>pics= null;
@Override
public int getCount() {
//基本上是无限循环
if (pics!=null){
return Integer.MAX_VALUE;
}
return 0;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
int realPosition = position%pics.size();
//getContext返回视图运行的上下文,通过该上下文可以访问当前主题、资源等。
ImageView imageView = new ImageView(container.getContext());
//设置视图的资源
imageView.setImageResource(pics.get(realPosition));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
public void setData(List<Integer>colos){
this.pics = colos;
}
public int getDataRealSize(){
if (pics!=null){
return pics.size();
}
return 0;
}
}
activity_main.xml
<?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"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/loop_pager"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</RelativeLayout>
这是是用viewpager实现的,实现的方法有很多,其实用别人的库更方便
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/111679.html