簡略談談C#中深拷貝、淺拷貝。本站提示廣大學習愛好者:(簡略談談C#中深拷貝、淺拷貝)文章只能為提供參考,不一定能成為您想要的結果。以下是簡略談談C#中深拷貝、淺拷貝正文
許多APP中都完成了相似引誘頁的主動輪播,忍不住想到昨天的引誘頁上修正一下代碼完成輪播。 其實年夜體上只須要添加一個線程輪回履行便可以了。
項目已同步至:https://github.com/nanchen2251/viewpagerDemo
異樣的先上圖
直接上代碼,正文都全的,我想如許更有益於懂得。
先改了Adapter
package com.example.nanchen.taketurnplaydemo; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * Created by 南塵 on 2016/7/10. */ public class GuidePageAdapter extends PagerAdapter { private List<View> viewList; public GuidePageAdapter(List<View> viewList) { this.viewList = viewList; } /** * @return 前往頁面的個數 */ @Override public int getCount() { if (viewList != null){ // return viewList.size(); //第一處修正,設置輪播最年夜值,等於無窮輪回 return Integer.MAX_VALUE; } return 0; } /** * 斷定對象能否生成界面 * @param view * @param object * @return */ @Override public boolean isViewFromObject(View view, Object object) { return view == object; } /** * 初始化position地位的界面 * @param container * @param position * @return */ @Override public Object instantiateItem(ViewGroup container, int position) { //第二處修正,以後要顯示的數據索引為聚集長度 int newPosition = position % viewList.size(); container.addView(viewList.get(newPosition)); return viewList.get(newPosition); // container.addView(viewList.get(position)); // return viewList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { //第三處修正,移除的索引為聚集的長度 int newPosition = position % viewList.size(); container.removeView(viewList.get(newPosition)); // container.removeView(viewList.get(position)); } }
在看Activity
package com.example.nanchen.taketurnplaydemo; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; /** * 完成初次啟動的引誘頁面 */ public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{ private ViewPager vp; private int []imageIdArray;//圖片資本的數組 private List<View> viewList;//圖片資本的聚集 private ViewGroup vg;//放置圓點 //實例化原點View private ImageView iv_point; private ImageView []ivPointArray; private boolean isLooper; //最初一頁的按鈕 private ImageButton ib_start; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_guide); ib_start = (ImageButton) findViewById(R.id.guide_ib_start); ib_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(GuideActivity.this,MainActivity.class)); finish(); } }); //加載ViewPager initViewPager(); //加載底部圓點 initPoint(); //修正添加設置ViewPager確當前頁,為了包管閣下輪播 vp.setCurrentItem(5000000); //開啟一個線程,用於輪回 new Thread(new Runnable() { @Override public void run() { isLooper = true; while (isLooper){ try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { //這裡是設置以後頁的下一頁 vp.setCurrentItem(vp.getCurrentItem() + 1); } }); } } }).start(); } /** * 加載底部圓點 */ private void initPoint() { //這裡實例化LinearLayout vg = (ViewGroup) findViewById(R.id.guide_ll_point); //依據ViewPager的item數目實例化數組 ivPointArray = new ImageView[viewList.size()]; //輪回新建底部圓點ImageView,將生成的ImageView保留到數組中 int size = viewList.size(); for (int i = 0;i<size;i++){ iv_point = new ImageView(this); iv_point.setLayoutParams(new ViewGroup.LayoutParams(20,20)); iv_point.setPadding(30,0,30,0);//left,top,right,bottom ivPointArray[i] = iv_point; //第一個頁面須要設置為選中狀況,這裡采取兩張分歧的圖片 if (i == 0){ iv_point.setBackgroundResource(R.mipmap.full_holo); }else{ iv_point.setBackgroundResource(R.mipmap.empty_holo); } //將數組中的ImageView參加到ViewGroup vg.addView(ivPointArray[i]); } } /** * 加載圖片ViewPager */ private void initViewPager() { vp = (ViewPager) findViewById(R.id.guide_vp); //實例化圖片資本 imageIdArray = new int[]{R.mipmap.guide1,R.mipmap.guide2,R.mipmap.guide3}; viewList = new ArrayList<>(); //獲得一個Layout參數,設置為全屏 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); //輪回創立View並參加到聚集中 int len = imageIdArray.length; for (int i = 0;i<len;i++){ //new ImageView並設置全屏和圖片資本 ImageView imageView = new ImageView(this); imageView.setLayoutParams(params); imageView.setBackgroundResource(imageIdArray[i]); //將ImageView參加到聚集中 viewList.add(imageView); } //View聚集初始化好後,設置Adapter vp.setAdapter(new GuidePageAdapter(viewList)); //設置滑動監聽 vp.setOnPageChangeListener(this); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } /** * 滑動後的監聽 * @param position */ @Override public void onPageSelected(int position) { //修正全體的position長度 int newPosition = position % viewList.size(); //輪回設置以後頁的標志圖 int length = imageIdArray.length; for (int i = 0;i<length;i++){ ivPointArray[newPosition].setBackgroundResource(R.mipmap.full_holo); if (newPosition != i){ ivPointArray[i].setBackgroundResource(R.mipmap.empty_holo); } } // //輪回設置以後頁的標志圖 // int length = imageIdArray.length; // for (int i = 0;i<length;i++){ // ivPointArray[position].setBackgroundResource(R.mipmap.full_holo); // if (position != i){ // ivPointArray[i].setBackgroundResource(R.mipmap.empty_holo); // } // } //斷定能否是最初一頁,若是則顯示按鈕 // if (position == imageIdArray.length - 1){ // ib_start.setVisibility(View.VISIBLE); // }else { // ib_start.setVisibility(View.GONE); // } } @Override public void onPageScrollStateChanged(int state) { } }
在Activity中值得一提的是,我們在現實的安卓開辟中會常常用到更新UI,之前我們平日應用的都是Handler,這裡就供給了別的的一種完成方法runOnUiThread(),須要一個完成Runnable接口的完成類作為參數,
XML異樣簡略
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".GuideActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp"> <android.support.v4.view.ViewPager android:id="@+id/guide_vp" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/guide_ll_point" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:paddingBottom="10dp" android:gravity="center_horizontal"> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/guide_ib_start" android:src="@mipmap/btn_start" android:layout_centerHorizontal="true" android:layout_above="@+id/guide_ll_point" android:background="@null" android:visibility="gone"/> </RelativeLayout> </RelativeLayout>
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。