Viewpager wireless carousel obtains network images,
Not much. You can understand the Code directly.
Dot attributes
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>
<?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>
Above is the layout
The following src code
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 (); // instantiate the tool class
Private List <ImageView> imgs = new ArrayList <ImageView> (); // set of images
Private List <View> dots = new ArrayList <View> (); // small dot set
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 );
// Asynchronous Network request
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 (); // Initialization Method
}
Cmd.exe cute ();
}
// Initialization Method
Private void init (){
ViewPager = (ViewPager) findViewById (R. id. viewPager );
Layout = (LinearLayout) findViewById (R. id. ll );
GetImgs (); // obtain the image
GetDots (); // get small dots
// ViewPager Adapter
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 Object ){
Container. removeView (View) object );
}
});
// Set the initialization location
I = imgs. size () * 10;
// Obtain the location
ViewPager. setCurrentItem (I );
// Set the first dot to true
Dots. get (0). setBackgroundResource (R. drawable. dots_focus );
// Automatic timer carousel
Timer timer = new Timer ();
Timer. schedule (new TimerTask (){
@ Override
Public void run (){
I = viewPager. getCurrentItem () + 1;
Handler. sendEmptyMessage (I );
}
}, 1,2000 );
}
// Image Method
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 );
}
}
// Method for small dots
Private void getDots (){
For (int I = 0; 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 );
}
}
}