viewpager 자동 순환 윤 방도 실현
레이아웃 파일
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_adv"
android:layout_width="match_parent"
android:layout_height="160dp"/>
<LinearLayout
android:id="@+id/View_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/vp_adv"
android:layout_marginTop="-25dp"
android:orientation="horizontal"
android:gravity="right">
</LinearLayout>
</RelativeLayout>
</LinearLayout>
핵심 코드
public class MainActivity extends AppCompatActivity {
private ImageView indicator;//
private ImageView [] indicators;//
private boolean iscontinue = true;
private ViewPager viewPager;
private ViewGroup viewGroup;
private ArrayList<Fragment> fragments;
List<Integer> imgs = null;
private AtomicInteger index = new AtomicInteger();
private Handler viewHandler= new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
viewPager.setCurrentItem(msg.what);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView(){
viewPager=findViewById(R.id.vp_adv);
viewGroup=findViewById(R.id.View_indicator);
//
List<View> listPics = new ArrayList<>();
ImageView img1 = new ImageView(this);
img1.setBackgroundResource(R.mipmap.food1);
listPics.add(img1);
ImageView img2 = new ImageView(this);
img1.setBackgroundResource(R.mipmap.shop2);
listPics.add(img2);
ImageView img3 = new ImageView(this);
img1.setBackgroundResource(R.mipmap.shop3);
listPics.add(img3);
ImageView img4 = new ImageView(this);
img1.setBackgroundResource(R.mipmap.shop4);
listPics.add(img4);
initFragments();
//
indicators = new ImageView[listPics.size()];
for(int i=0;i<indicators.length;i++){
indicator = new ImageView(this);
indicator.setLayoutParams(new LinearLayout.LayoutParams(40,40));
indicator.setPadding(5,5,5,5);
indicators[i] = indicator;
if(i == 0){
indicators[i].setBackgroundResource(R.drawable.ic_launcher_background);
}else{
indicators[i].setBackgroundResource(R.drawable.ic_launcher_foreground);
}
viewGroup.addView(indicators[i]);
}
// ViewPager
viewPager.setAdapter(new pagerAdapter(getSupportFragmentManager()));
// viewPager
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
index.getAndSet(position);
for(int i = 0;i <indicators.length; i ++){
if(i ==position){
indicators[i].setBackgroundResource(R.drawable.a1);
}else{
indicators[i].setBackgroundResource(R.drawable.a2);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
iscontinue = false;
break;
case MotionEvent.ACTION_UP:
iscontinue = true;
break;
}
return false;
}
});
// UI
new Thread(new Runnable() {
@Override
public void run() {
while (true){
if(iscontinue){
viewHandler.sendEmptyMessage(index.get());
whatOption();
}
}
}
}).start();
}
private void whatOption(){
index.incrementAndGet();// +1;
if(index.get()>indicators.length-1){
index.getAndAdd(-4);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void initFragments() {
imgs = new ArrayList<Integer>();
imgs.add(R.mipmap.timg1);
imgs.add(R.mipmap.timg2);
imgs.add(R.mipmap.timg3);
imgs.add(R.mipmap.timg4);
}
class pagerAdapter extends FragmentPagerAdapter{
public pagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
//
MyFragment fm = new MyFragment();
fm.setUrl(imgs.get(arg0));
return fm;
}
@Override
public int getCount() {
return imgs.size();
}
}
}
class MyPagerAdapter extends PagerAdapter{
private List<View> viewList;
public MyPagerAdapter(List<View>viewList){
this.viewList = viewList;
}
@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));
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.