ViewPager+小圓點
阿新 • • 發佈:2018-11-27
package com.example.day13rk;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<ImageView> imageViews = new ArrayList(); private int[] imageResIds = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, }; //圖片字串陣列String[]資源. private String[] descs = { "鞏俐不低俗,我就不能低俗", "撲樹又回來啦!再唱經典老歌引萬人大合唱", "揭祕北京電影如何升級", "樂視網TV版大派送", "熱血屌絲的反殺", }; private LinearLayout ll_dot; private TextView viewPager_tv; private ViewPager viewPager_vp; Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0: viewPager_vp.setCurrentItem(viewPager_vp.getCurrentItem()+1); break; } handler.sendEmptyMessageDelayed(0,2000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { //找可以設定點的容器LinearLayout的物件,進行點的新增 ll_dot = findViewById(R.id.ll_dot); //找到文字的物件 viewPager_tv = findViewById(R.id.viewPager_tv); //找到VIewPager物件. viewPager_vp = findViewById(R.id.viewPager_vp); for(int i = 0 ; i < imageResIds.length; i++){ //建立ImageView物件 ImageView imageView = new ImageView(this); //通過該物件新增圖片資源..setBackgroundResource方法 imageView.setBackgroundResource(imageResIds[i]); //把控制元件新增到集合ImageViews中去,以方便在VIewPager的介面卡裡instantiateItem方法獲取 imageViews.add(imageView); //進行點的新增,其個數和圖片的個數一致,因此放到該迴圈中 createDot(); } //設定介面卡 viewPager_vp.setAdapter(new MyViewpagerAdapter(MainActivity.this,imageViews)); //滑動監聽 viewPager_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { //同過getCurrentItem方法拿到當前使用者所互動ViewPager的item位置. int currentItem = viewPager_vp.getCurrentItem(); changeTextAndDot(currentItem%imageResIds.length); } @Override public void onPageSelected(int i) { int position = i % imageViews.size(); viewPager_tv.setText(descs[position]); } //和使用者互動時,state就是ViewPager的狀態 @Override public void onPageScrollStateChanged(int state) { //如果空閒狀態就自動滑動 if(state == ViewPager.SCROLL_STATE_IDLE){ handler.sendEmptyMessageDelayed(0,2000); }else{ handler.removeMessages(0); } } }); //設定左右滑動 viewPager_vp.setCurrentItem(Integer.MAX_VALUE/2+3); handler.sendEmptyMessageDelayed(0,2000); /* viewPager_vp.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } });*/ } private void changeTextAndDot(int position) { viewPager_tv.setText(descs[position]); for(int i = 0; i < imageResIds.length; i++){ View childAt = ll_dot.getChildAt(i); childAt.setBackgroundResource(i == position ?R.drawable.dot_focus:R.drawable.dot_normal); } } private void createDot() { View view = new View(this); view.setBackgroundResource(R.drawable.dot_normal); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(8,8); layoutParams.leftMargin = 8 ; view.setLayoutParams(layoutParams); ll_dot.addView(view); } @Override protected void onDestroy() { super.onDestroy(); handler.removeMessages(0); }
}