2024年安卓轮播图代码+定时翻页
asda
这里是Fragment子类的继承如果使用 AppCompatActivity请修改一下很简单的如果又看不懂的话可以访问使用我的gpt:https://0.00000.work/ 免费3.5的 直接吧代码扔给他然后和他说帮忙解释一下每一行作用
Integer[] data={R.drawable.c1,R.drawable.c2,R.drawable.c3};为三张图片 自己放进去一下就可以了
java代码
package com.example.lbt4.menu;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.lbt4.HomeNurse.HomeFrom;
import com.example.lbt4.HomeNurse.HomeNurse;
import com.example.lbt4.R;
public class IndexFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_index, container, false);
TextView viewById = inflate.findViewById(R.id.textView3);
ViewPager viewById1 = inflate.findViewById(R.id.viewPager2);
Integer[] data={R.drawable.c1,R.drawable.c2,R.drawable.c3};
inflate.findViewById(R.id.button).setOnClickListener(view -> {
Intent intent = new Intent().setClass(inflate.getContext(), HomeNurse.class);
startActivity(intent);
});
viewById1.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return 9999;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view==object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
int i=position%data.length;
View inflate = inflater.inflate(R.layout.image_image, container, false);
ImageView imageView = inflate.findViewById(R.id.imageView);
imageView.setImageResource(data[i]);
container.addView(inflate);
return inflate;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
});
viewById1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
int i=position%data.length;
viewById.setText((i+1)+"/"+data.length);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
new Thread(new Runnable() {
@Override
public void run() {
while (true){
inflate.post(()->{
viewById1.setCurrentItem(viewById1.getCurrentItem()+1);
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
// Inflate the layout for this fragment
return inflate;
}
}
xml视图代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".menu.IndexFragment">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="61dp"
android:text="社区卫生服务中心"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="65dp"
android:layout_marginBottom="173dp"
android:text="预约服务"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="184dp"
android:layout_marginTop="18dp"
android:text="1/1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewPager2" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager2"
android:layout_width="400dp"
android:overScrollMode="never"
android:layout_height="176dp"
android:layout_marginTop="156dp"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="65dp"
android:layout_marginBottom="57dp"
android:text="上门护理"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="56dp"
android:layout_marginBottom="57dp"
android:text="上门体检"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
实现 效果:
有若有问题 评论区评论 或者联系我或者加QQ 直接问问 :911412667
标签:轮播,翻页,int,代码,Override,import,inflate,public,android From: https://blog.csdn.net/qq_56040798/article/details/137629219