您好,登錄后才能下訂單哦!
如何在Android中利用viewpager實現一個無限輪播功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
小圓點屬性
dot_focused.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#f00"/> <corners android:radius="8dp"/> </shape>
dot_normal.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#88000000"/> <corners android:radius="8dp"/> </shape>
dot_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <View android:id="@+id/scrollView" android:layout_width="8dp" android:layout_height="8dp" android:layout_margin="3dp" android:background="@drawable/dot_normal" /> </LinearLayout>
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" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/ll" android:layout_marginTop="450dp" android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="20dp"/> </RelativeLayout>
以上是布局
下面src代碼
import android.content.Intent; import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import com.bwie.bean.SuperClass; import com.bwie.utils.NetWorkUtils; import com.bwie.utils.URLDB; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private Button bt; private LinearLayout layout; private List<SuperClass.ResultBean.DataBean> data; private NetWorkUtils netWorkUtils = new NetWorkUtils();//實例化工具類 private List<ImageView>imgs = new ArrayList<ImageView>();//圖片的集合 private List<View> dots = new ArrayList<View>(); //小圓點集合 private int i = 0; private int dot = 0; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); viewPager.setCurrentItem(i); dots.get(dot).setBackgroundResource(R.drawable.dots_normal); dots.get(i%imgs.size()).setBackgroundResource(R.drawable.dots_focus); dot=i%imgs.size(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //異步網絡請求 new AsyncTask<String,Integer,String>(){ @Override protected String doInBackground(String... params) { String json = netWorkUtils.getJsonHttpConnetion(URLDB.URL_IJ); return json; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); SuperClass superClass = new Gson().fromJson(s, SuperClass.class); data = superClass.getResult().getData(); init();//初始化方法 } }.execute(); } //初始化方法 private void init() { viewPager = (ViewPager) findViewById(R.id.viewPager); layout = (LinearLayout) findViewById(R.id.ll); getImgs();//獲取圖片 getDots();//獲取小圓點 //viewPager適配器 viewPager.setAdapter(new PagerAdapter() { @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { int i = position % imgs.size(); ImageView imageView = imgs.get(i); imageView.setScaleType(ImageView.ScaleType.FIT_XY); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }); //設置初始化位置 i = imgs.size()*10; //得到位置 viewPager.setCurrentItem(i); //設置第一個圓點為true dots.get(0).setBackgroundResource(R.drawable.dots_focus); //timer自動輪播 Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { i = viewPager.getCurrentItem() + 1; handler.sendEmptyMessage(i); } },1,2000); } //圖片的方法 private void getImgs() { for(int i = 0;i<data.size();i++){ List<SuperClass.ResultBean.DataBean.StepsBean> steps = data.get(i).getSteps(); String url = steps.get(0).getImg(); ImageView img = new ImageView(this); img.setScaleType(ImageView.ScaleType.FIT_XY); ImageLoader.getInstance().displayImage(url,img); imgs.add(img); } } //小圓點的方法 private void getDots() { for(int i=0;i<imgs.size();i++){ View view = LayoutInflater.from(this).inflate(R.layout.dot_item,null); View viewById = view.findViewById(R.id.scrollView); dots.add(viewById); layout.addView(view); } } }
關于如何在Android中利用viewpager實現一個無限輪播功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。