Created
December 12, 2019 05:00
-
-
Save shishirthedev/ff06892227c8f8eef32812d88ae79b18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class HomeActivity : MvpBaseActivity<HomePresenter>(), HomeContract.View, | |
| NavigationView.OnNavigationItemSelectedListener { | |
| // Image Slider | |
| private lateinit var sliderAdapter: ImageSliderAdapter | |
| private var images: ArrayList<String> = ArrayList() // List of url of banenr images | |
| private var timer: Timer? = null | |
| private var currentImage = 0 | |
| private val SLIDER_DELAY: Long = 2500.toLong() | |
| private val SLIDER_GAP: Long = 2500.toLong() | |
| override fun getContentView(): Int { | |
| return R.layout.activity_home | |
| } | |
| override fun onViewReady(savedInstanceState: Bundle?, intent: Intent) { | |
| mPresenter.loadBannerImages() | |
| } | |
| private fun setUpImageSlider() { | |
| sliderAdapter = ImageSliderAdapter(this, images) | |
| slide_pager.adapter = sliderAdapter | |
| if (images.size > 1) { | |
| tabs_dot_indicator.setupWithViewPager(slide_pager, true) | |
| slide_pager.addOnPageChangeListener(sliderImageChangeListener) | |
| // Auto Change of Images | |
| scheduleImageSlider() | |
| } else { | |
| tabs_dot_indicator.visibility = View.GONE | |
| } | |
| } | |
| private fun scheduleImageSlider() { | |
| val handler = Handler(Looper.getMainLooper()) | |
| val updateImageRunnable = Runnable { | |
| slide_pager.let { | |
| currentImage = if (currentImage == images.size) 0 else ++currentImage | |
| slide_pager.setCurrentItem(currentImage, true) | |
| } | |
| } | |
| timer?.cancel() // Cancelling if timer already exists... | |
| timer = Timer() | |
| timer!!.schedule(object : TimerTask() { | |
| override fun run() { | |
| handler.post(updateImageRunnable) | |
| } | |
| }, SLIDER_DELAY, SLIDER_GAP) | |
| } | |
| private val sliderImageChangeListener = object : ViewPager.OnPageChangeListener { | |
| override fun onPageSelected(position: Int) { | |
| currentImage = position | |
| scheduleImageSlider() // Rescheduling Image Slider.... | |
| } | |
| override fun onPageScrollStateChanged(state: Int) {} | |
| override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {} | |
| } | |
| override fun bannerDidReceived(bannerImages: ArrayList<String>) { | |
| images = bannerImages | |
| setUpImageSlider() | |
| } | |
| } | |
| /////////////////// SLIDER ADAPTER //////////////////////////////// | |
| import android.content.Context | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.ImageView | |
| import androidx.viewpager.widget.PagerAdapter | |
| import com.squareup.picasso.Picasso | |
| import com.walletmix.binge.R | |
| import java.util.ArrayList | |
| class ImageSliderAdapter(private val context: Context, private val images: ArrayList<String>) : PagerAdapter() { | |
| override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) { | |
| container.removeView(`object` as View) | |
| } | |
| override fun getCount(): Int { | |
| return images.size | |
| } | |
| override fun instantiateItem(view: ViewGroup, position: Int): Any { | |
| val myImageLayout = LayoutInflater.from(view.context).inflate(R.layout.slider_layout, view, false) | |
| val sliderImage = myImageLayout.findViewById<ImageView>(R.id.sliderImage) | |
| Picasso.with(context).load(images[position]).into(sliderImage) | |
| view.addView(myImageLayout, 0) | |
| return myImageLayout | |
| } | |
| override fun isViewFromObject(view: View, `object`: Any): Boolean { | |
| return view == `object` | |
| } | |
| } | |
| //////////////////// ACTIVITY HOME LAYOUT //////////////////////// | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout | |
| xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| xmlns:app="http://schemas.android.com/apk/res-auto" | |
| android:layout_width="match_parent" android:layout_height="match_parent" | |
| android:orientation="vertical" | |
| tools:context=".ui.basic.home.HomeActivity" | |
| android:background="#f2f2f2"> | |
| <FrameLayout android:layout_width="match_parent" | |
| android:layout_height="170dp" | |
| android:padding="@dimen/xxSmallMargin"> | |
| <androidx.viewpager.widget.ViewPager | |
| android:id="@+id/slide_pager" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent"/> | |
| <com.google.android.material.tabs.TabLayout | |
| android:id="@+id/tabs_dot_indicator" | |
| android:layout_gravity="bottom" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| app:tabBackground="@drawable/slider_image_selector" | |
| app:tabGravity="center" | |
| app:tabIndicatorHeight="0dp"/> | |
| </FrameLayout> | |
| </LinearLayout> | |
| ///////////////// SLIDER LAYOUT /////////////////////////// | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| xmlns:app="http://schemas.android.com/apk/res-auto"> | |
| <ImageView | |
| android:id="@+id/sliderImage" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:fitsSystemWindows="true" | |
| android:contentDescription="@null" | |
| android:scaleType="centerCrop" | |
| android:adjustViewBounds="true" | |
| app:layout_collapseMode="parallax" | |
| tools:src="@drawable/img_product" /> | |
| </FrameLayout> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment