您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Android 應用中的輪播圖怎么利用ViewPager實現,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
Drawable文件夾下的xml代碼:
Shape_yes.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ff6c67"></solid> <corners android:radius="1000dp"></corners> </shape>
Shape_no.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#f0f0f0"></solid> <corners android:radius="1000dp"></corners> </shape>
選擇器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/shape_no" android:state_selected="false" /> <item android:drawable="@drawable/shape_yes" android:state_selected="true" /> </selector>
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"> <RelativeLayout android:layout_width="match_parent" android:layout_height="160dp"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:padding="5dp" android:orientation="vertical" android:background="#66000000"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:gravity="center" android:orientation="horizontal" android:paddingBottom="10dp"> <ImageView android:id="@+id/iv_one" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginRight="20dp" android:background="@drawable/selector_show" /> <ImageView android:id="@+id/iv_two" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginRight="20dp" android:background="@drawable/selector_show" /> <ImageView android:id="@+id/iv_three" android:layout_width="20dp" android:layout_height="20dp" android:background="@drawable/selector_show" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout>
寫一個類ViewPagerAdapter_Circleextends PagerAdapter
public class ViewPagerAdapter_Circle extends PagerAdapter { ArrayList<View> arrayList; Context context; public ViewPagerAdapter_Circle(ArrayList<View> arrayList, Context context) { this.arrayList = arrayList; this.context = context; } /***數據源的大小(條目)*/ @Override public int getCount() { return arrayList.size(); } /** * 頁面視圖是否關聯到特定的對象 */ @Override public boolean isViewFromObject(View view, Object object) { return view == object;//判斷當前要顯示的頁面 } /** * 初始化頁面 * 1.頁面添加到container * 2.將頁面返回 */ @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(arrayList.get(position));//頁面添加到container,添加位置 return arrayList.get(position); } /** * 銷毀當前頁面 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(arrayList.get(position)); } }
MainActivity中代碼(含注解)
public class MainActivity extends AppCompatActivity { ViewPager viewPager; int imageResIds[]; ArrayList<View> imageViewList;//存圖片的集合 ArrayList<ImageView> imageViews;//存白點的集合 ImageView iv_one,iv_two,iv_three; int oldPostion;//記錄原來的位置 private Boolean flag; int count=0; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 0) { viewPager.setCurrentItem(count); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化布局 initView(); //初始化數據 initData(); //設置適配器 initAdapter(); //設置圓點 initPoint(); flag = true; //循環設置播放 new Thread() { @Override public void run() { while (flag) { try { Thread.sleep(3000); count++; if (count == 3) { count = 0; } } catch (InterruptedException e) { e.printStackTrace(); } /**傳遞信息*/ Message msg = new Message(); msg.what = 0; msg.obj = count; handler.sendMessage(msg); } } }.start(); } private void initView() { viewPager = (ViewPager) findViewById(R.id.vp); } //初始化要顯示的數據 private void initData() { //圖片資源數組 imageResIds = new int[]{R.mipmap.a, R.mipmap.b, R.mipmap.c}; imageViewList = new ArrayList<>();//存圖片的集合 ImageView imageView; for (int i = 0; i < imageResIds.length; i++) { imageView = new ImageView(this); imageView.setBackgroundResource(imageResIds[i]); imageViewList.add(imageView); } } //設置適配器 private void initAdapter() { ViewPagerAdapter_Circle myViewPagerAdapter = new ViewPagerAdapter_Circle(imageViewList, this); viewPager.setAdapter(myViewPagerAdapter); } /** * 設置圓點的 */ private void initPoint() { iv_one = (ImageView) findViewById(R.id.iv_one); iv_two = (ImageView) findViewById(R.id.iv_two); iv_three = (ImageView) findViewById(R.id.iv_three); imageViews = new ArrayList<>(); imageViews.add(iv_one); imageViews.add(iv_two); imageViews.add(iv_three); iv_one.setSelected(true);//設置第一個圓點為true /**viewPager監聽事件*/ viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { /**滑動時調用*/ @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } /**停止時調用*/ @Override public void onPageSelected(int position) { imageViews.get(position).setSelected(true);//滑動后顏色改變 imageViews.get(oldPostion).setSelected(false);//變為初始顏色 oldPostion = position;//把滑動后的圓點的position給原來的oldPostion } /**滑動狀態改變時調用*/ @Override public void onPageScrollStateChanged(int state) { } }); } }
關于Android 應用中的輪播圖怎么利用ViewPager實現就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。