[embedyt] https://www.youtube.com/watch?v=tM7rwJoK-18[/embedyt]
Links & Dependenciesactivity_main.xmlViewPagerAdapter.javaMainActivity.javaAndroidManifest.xml
Library on GitHub with dependency + instructions:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.codinginflow.picassoimagesliderexample.MainActivity"> </android.support.v4.view.ViewPager>
package com.codinginflow.picassoimagesliderexample; import android.content.Context; import android.support.annotation.NonNull; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.squareup.picasso.Picasso; public class ViewPagerAdapter extends PagerAdapter { private Context context; private String[] imageUrls; ViewPagerAdapter(Context context, String[] imageUrls) { this.context = context; this.imageUrls = imageUrls; } @Override public int getCount() { return imageUrls.length; } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { ImageView imageView = new ImageView(context); Picasso.get() .load(imageUrls[position]) .fit() .centerCrop() .into(imageView); container.addView(imageView); return imageView; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } }
package com.codinginflow.picassoimagesliderexample; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { private String[] imageUrls = new String[]{ "https://cdn.pixabay.com/photo/2016/11/11/23/34/cat-1817970_960_720.jpg", "https://cdn.pixabay.com/photo/2017/12/21/12/26/glowworm-3031704_960_720.jpg", "https://cdn.pixabay.com/photo/2017/12/24/09/09/road-3036620_960_720.jpg", "https://cdn.pixabay.com/photo/2017/11/07/00/07/fantasy-2925250_960_720.jpg", "https://cdn.pixabay.com/photo/2017/10/10/15/28/butterfly-2837589_960_720.jpg" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = findViewById(R.id.view_pager); ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageUrls); viewPager.setAdapter(adapter); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codinginflow.picassoimagesliderexample"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>