Created
March 11, 2020 05:22
-
-
Save SouravKumarPandit/b6a7dc5234e9903024c8b7ae0a4954a7 to your computer and use it in GitHub Desktop.
This file contains 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
package com.pandit.application.demoapp.ui | |
import android.os.Bundle | |
import android.os.Handler | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.core.view.size | |
import androidx.fragment.app.Fragment | |
import com.pandit.application.demoapp.R | |
import com.pandit.application.demoapp.adapters.ViewPagerAdapter | |
import kotlinx.android.synthetic.main.fragment_home.view.* | |
import java.util.* | |
// TODO: Rename parameter arguments, choose names that match | |
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | |
private const val ARG_PARAM1 = "param1" | |
private const val ARG_PARAM2 = "param2" | |
/** | |
* A simple [Fragment] subclass. | |
* Use the [Home.newInstance] factory method to | |
* create an instance of this fragment. | |
*/ | |
class Home : Fragment() { | |
// TODO: Rename and change types of parameters | |
private var param1: String? = null | |
private var param2: String? = null | |
lateinit var timer:Timer | |
var currentPage = 0 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
arguments?.let { | |
param1 = it.getString(ARG_PARAM1) | |
param2 = it.getString(ARG_PARAM2) | |
} | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
// Inflate the layout for this fragment | |
val view = inflater.inflate(R.layout.fragment_home, container, false) | |
view.view_pager2.adapter = | |
ViewPagerAdapter() | |
val handler = Handler() | |
val Update = Runnable { | |
if (view.view_pager2.size - 1 == currentPage) { | |
currentPage = 0 | |
} | |
view.view_pager2.setCurrentItem(currentPage++, true) | |
if(currentPage > view.view_pager2.size+1) currentPage = 0 | |
} | |
timer = Timer() // This will create a new Thread | |
timer.schedule(object : TimerTask() { | |
// task to be scheduled | |
override fun run() { | |
handler.post(Update) | |
} | |
}, 500, 3000) | |
return view | |
} | |
companion object { | |
@JvmStatic | |
fun newInstance(param1: String, param2: String) = | |
Home().apply { | |
arguments = Bundle().apply { | |
putString(ARG_PARAM1, param1) | |
putString(ARG_PARAM2, param2) | |
} | |
} | |
} | |
} | |
class ViewPagerAdapter : RecyclerView.Adapter<PagerVH>() { | |
val slider = arrayOf("https://www.vrlocal.in/images/main-slider/flooerclean-home-banner.jpg", | |
"https://www.vrlocal.in/images/main-slider/interior-banner-home.jpg", | |
"https://www.vrlocal.in/images/main-slider/clean-home-banner.jpg") | |
//array of colors to change the background color of screen | |
//get the size of color array | |
override fun getItemCount(): Int = slider.size | |
override fun onBindViewHolder(holder: PagerVH, position: Int) { | |
Glide.with(holder.itemView).load(slider.get(position)) | |
.into(holder.itemView.image) | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerVH { | |
return PagerVH( | |
LayoutInflater.from( | |
parent.context | |
).inflate( | |
R.layout.pager_item, | |
parent, | |
false | |
) | |
) | |
} | |
//binding the screen with view | |
} | |
class PagerVH(itemView: View) : RecyclerView.ViewHolder(itemView) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment