**this is my Activity pager **
private const val TAG = "CarpagerActivity"
var carList: ArrayList<Car>? = null
var mSerializer : JsonSerializer? = null
class CarPagerActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_car_pager)
mSerializer = JsonSerializer("CarTrinkApp.json",
applicationContext)
try {
carList = mSerializer!!.load()
} catch (e: Exception) {
carList = ArrayList()
Log.e("Error loading cars: ", "", e)
}
// create list of fragments, one fragment for each car
var carFragmentList = java.util.ArrayList<Fragment>()
for (car in carList!!) {
carFragmentList.add(ShowCarFragment.newInstance(car))
}
val pageAdapter = CarPagerAdapter(supportFragmentManager, carFragmentList)
findViewById<ViewPager>(R.id.pager_cars).adapter = pageAdapter
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val myPost = data?.getIntExtra("adapterPosition", 123)
println(myPost)
println("adadadada")
}
class CarPagerAdapter(fm: FragmentManager, private val carFragmentList: ArrayList<Fragment>) : FragmentPagerAdapter(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getCount() = carFragmentList.size
override fun getItem(position: Int) = carFragmentList[position]
}
}
[![I want add animation to this transition ][1]][1]
**and also this page in Fragment Layout **
[1]: https://i.stack.imgur.com/qFB3b.png
Fading animation, see reference here
viewPager.setPageTransformer(false) { page, position ->
// do transformation here
page.alpha = 0f
page.visibility = View.VISIBLE
// Start Animation for a short period of time
page.animate()
.alpha(1f).duration =
page.resources.getInteger(android.R.integer.config_longAnimTime)
.toLong()
}
or
viewPager.setPageTransformer(false) { page, position ->
// do transformation here
page.rotationY = position * -70
}
Related
I am working with recycler view, but it was behaving differently now.
The issue is when we scroll from top to bottom all things work okay, but when scrolling bottom to top it not working as expected.
I am loading arround 450 Items.
Please find more details in the attached video (Last Few Seconds Very Important).
Video : https://www.dropbox.com/s/rb7y52av6wwcq3q/RecyclerViewIssue.mp4?dl=0
Gradle dependencies
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
//RecyclerView
implementation 'androidx.recyclerview:recyclerview:1.2.1'
//Other dependencies
XML Code
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcvJobList"
android:layout_width="match_parent"
android:layout_height="#dimen/_400sdp"
android:background="#drawable/box2"
android:clipChildren="false"
android:paddingTop="#dimen/_5sdp"
android:paddingBottom="#dimen/_5sdp"
tools:itemCount="20"
tools:listitem="#layout/item_job" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity Code
class HomeActivity : BaseActivity<HomeViewModel, ActivityHomeBinding, HomeRepository>(),
RcvJobListAdapter.AdapterListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Other Code
ViewModel.jobList.observe(this, jobListObserver)
}
// Live data observer code
val jobListObserver = Observer<List<Job>> {
Log.e("HomeActivity", "Job List Observer Invoked Size : ${it.size}");
var tempList = arrayListOf<Job>()
tempList.addAll(it)
var adapter = RcvJobListAdapter(tempList, this#HomeActivity)
binding.rcvJobList.adapter = adapter
}
}
Adapter Code
class RcvJobListAdapter : RecyclerView.Adapter<RcvJobListAdapter.ViewHolder> {
var jobList: List<Job> = emptyList()
private lateinit var callback: AdapterListener
constructor(
jobList: List<Job>,
callback: AdapterListener,
) : super() {
this.jobList = jobList
this.callback = callback
}
class ViewHolder(var itemView: View) : RecyclerView.ViewHolder(itemView)
{
var binding = ItemJobBinding.bind(itemView)
init {
binding.root.setBackgroundColor(Color.WHITE)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_job,parent,false)
)
}
var prevPosition : Int = -1
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Log.e("Adapter","Bind Pos : ${holder.bindingAdapterPosition}, Absolute : ${holder.absoluteAdapterPosition} , Last ${prevPosition}")
var job = jobList.get(holder.bindingAdapterPosition)
holder.binding.tvJobText.setText("${holder.bindingAdapterPosition}\n${job.getCombainedString()}")
holder.binding.tvJobText.setOnClickListener {
if(prevPosition!=-1)
{
var temp = prevPosition
prevPosition = -1;
notifyItemChanged(temp)
if(temp == holder.bindingAdapterPosition)
{
return#setOnClickListener
}
}
prevPosition = holder.bindingAdapterPosition
holder.binding.root.setBackgroundColor(Color.parseColor("#BCF1C9"))
}
if(prevPosition == holder.bindingAdapterPosition)
{
holder.binding.root.setBackgroundColor(Color.parseColor("#BCF1C9"))
}
else
{
holder.binding.root.setBackgroundColor(Color.WHITE)
}
}
override fun getItemCount(): Int = jobList.size
open interface AdapterListener {
fun jobItemClick(position: Int, job: Job);
}
}
Hello fellow programmers!
I am currently facing problem with RecyclerView's add animation with smooth scroll simulataneously.
Problem
I have implemented RecyclerView with ListAdapter and DiffUtil for cool animations (insert, remove). The problem is that I can't get it to smooth scroll to added position without cancelling insert animation.
User is supposed to type in 'exercise name' and then push 'add' button to add it to current 'exercise list'. Then this exercise should be added to RecyclerView list. Everything works fine here, but I can't achieve smooth scroll to new item position without cancelling (or accelerating) insert animation.
viewModel.exerciseListLiveData.observe(viewLifecycleOwner, {
adapter.submitList(it) {
// Cancels insert animation (or accelerates it rapidly) - bad UX...
layoutManager.smoothScrollToPosition(recyclerView, RecyclerView.State(), 0)
}
})
On the other hand, substituting smoothScrollToPosition with scrollToPosition works fine but when there's more items (recycler's view is filled from top to bottom) it blinks on scroll. I know it's rather jump than smooth scroll but then, why is it working with add animation so it doesn't cancel or accelerate?
viewModel.exerciseListLiveData.observe(viewLifecycleOwner, {
adapter.submitList(it) {
// Works fine (recyclerView is scrolling to added position)
// but when there's no room to display more items - it scrolls to it with blink...
// which I simply can't stand!
layoutManager.smoothScrollToPosition(recyclerView, RecyclerView.State(), 0)
}
})
Possible solution?
I'm supposed to first smoothScrollToPosition(0) to top-most position, then call submitList(it) and finally call scrollToPosition(0) but I can't achieve that (it is doing so fast, simultaneously, that it's layering each other). Maybe I should use coroutines? Don't know...
How am I supposed to add proper delay? Maybe I am doing something wrong because i read that some programmers had this autoscroll when added item.
Code sample
Adapter
class ExercisesListAdapter :
ListAdapter<Exercise, ExercisesListAdapter.ExerciseItemViewHolder>(ExercisesListDiffUtil()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExerciseItemViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding: ItemRecyclerviewExerciseBinding =
DataBindingUtil.inflate(inflater, R.layout.item_recyclerview_exercise, parent, false)
return ExerciseItemViewHolder(binding)
}
override fun onBindViewHolder(holder: ExerciseItemViewHolder, position: Int) {
val currentExercise = getItem(position)
holder.bind(currentExercise)
}
override fun getItemId(position: Int): Long {
return getItem(position).id
}
// Item ViewHolder class
class ExerciseItemViewHolder(private val binding: ItemRecyclerviewExerciseBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(itemExercise: Exercise) {
binding.textCardExerciseName.text = itemExercise.name
}
}
}
DiffUtil
class ExercisesListDiffUtil: DiffUtil.ItemCallback<Exercise>() {
override fun areItemsTheSame(oldItem: Exercise, newItem: Exercise): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Exercise, newItem: Exercise): Boolean {
return oldItem == newItem
}
}
ViewModel
class CreateWorkoutViewModel(private val repository: WorkoutRepository) : ViewModel() {
private var exerciseId = 0L
private var exerciseList = mutableListOf<Exercise>()
val exerciseName = MutableLiveData<String?>()
private val _exerciseListLiveData = MutableLiveData<List<Exercise>>()
val exerciseListLiveData: LiveData<List<Exercise>>
get() = _exerciseListLiveData
fun onAddExercise() {
if (canValidateExerciseName())
addExerciseToList()
}
fun onAddWorkout() {
exerciseList.removeAt(2)
_exerciseListLiveData.value = exerciseList.toList()
}
private fun canValidateExerciseName(): Boolean {
return !exerciseName.value.isNullOrBlank()
}
private fun addExerciseToList() {
exerciseList.add(0, Exercise(exerciseId++, exerciseName.value!!))
_exerciseListLiveData.value = exerciseList.toList()
}
}
Fragment
class CreateWorkoutFragment : Fragment() {
lateinit var binding: FragmentCreateWorkoutBinding
lateinit var viewModel: CreateWorkoutViewModel
lateinit var recyclerView: RecyclerView
lateinit var adapter: ExercisesListAdapter
lateinit var layoutManager: LinearLayoutManager
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding =
DataBindingUtil.inflate(inflater, R.layout.fragment_create_workout, container, false)
setViewModel()
setRecyclerView()
// Observers:
// Update RecyclerView list on change
viewModel.exerciseListLiveData.observe(viewLifecycleOwner, {
adapter.submitList(it) {
layoutManager.smoothScrollToPosition(recyclerView, RecyclerView.State(), 0)
}
})
return binding.root
}
private fun setRecyclerView() {
recyclerView = binding.recyclerviewExercises
setLayoutManager()
setAdapter()
}
private fun setLayoutManager() {
layoutManager = LinearLayoutManager(context)
recyclerView.layoutManager = layoutManager
}
private fun setAdapter() {
adapter = ExercisesListAdapter()
adapter.setHasStableIds(true)
recyclerView.adapter = adapter
}
private fun setViewModel() {
val dao = WorkoutDatabase.getInstance(requireContext()).workoutDAO
val repository = WorkoutRepository(dao)
val factory = CreateWorkoutViewModelFactory(repository)
viewModel = ViewModelProvider(this, factory).get(CreateWorkoutViewModel::class.java)
binding.viewModel = viewModel
binding.lifecycleOwner = this
}
}
EDIT
I got it working but it's not 'elegant' way and insert animation is canceled through process :/
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if(layoutManager.findFirstCompletelyVisibleItemPosition() == 0)
layoutManager.scrollToPosition(0)
else {
recyclerView.clearAnimation()
layoutManager.smoothScrollToPosition(recyclerView, RecyclerView.State(), 0)
}
}
})
I am new to Firebase and RecyclerViews and I am trying to get a list of ingredients from my Firebase Database and I want to create checkboxes for every item I retrieve from the database. I got as far as creating a RecyclerView and adapter for my Firebase Query but binding lists from the query doesn't seem to be covered anywhere I could find.
Please find below the code for putting the data into the recycler view as well as the layout files being used for the recycler view.
Data being retrieved
that I want to display the original field of my object as checkboxes
ShoppingListFragment.kt
class ShoppingListFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Enable Firestore logging
FirebaseFirestore.setLoggingEnabled(true)
//Firestore
firestore = Firebase.firestore
// Get recipes that the user has liked up to ${LIMIT}
query = firestore.collection("recipes").whereArrayContains(
"plannedBy",
Firebase.auth.uid.toString()
)
.orderBy("name", Query.Direction.ASCENDING)
.limit(LIMIT.toLong())
// Init the adapter to hold the recipe objects.
adapter = object : ShoppingListAdapter(query) {
override fun onDataChanged() {
if (itemCount == 0) {
shoppingListRecycler.visibility = View.GONE
viewEmpty.visibility = View.VISIBLE
} else {
shoppingListRecycler.visibility = View.VISIBLE
viewEmpty.visibility = View.GONE
}
}
override fun onError(e: FirebaseFirestoreException) {
// Show a snackbar on errors
view?.let {
Snackbar.make(
it,
"Error: check logs for info.", Snackbar.LENGTH_LONG
).show()
}
}
}
...
}
...
}
ShoppingListAdapter.kt
open class ShoppingListAdapter(query: Query) :
FirestoreAdapter<ShoppingListViewHolder>(query) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShoppingListViewHolder {
return ShoppingListViewHolder(
ItemShoppingListBinding.inflate(
LayoutInflater.from(parent.context), parent, false))
}
override fun onBindViewHolder(holder: ShoppingListViewHolder, position: Int) {
holder.bind(getSnapshot(position))
}
}
ShoppingListViewHolder.kt
class ShoppingListViewHolder(val binding: ItemShoppingListBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(snapshot: DocumentSnapshot) {
val recipe = snapshot.toObject<Recipe>() ?: return
binding.listTitle.text = recipe.name
if(recipe.ingredients.size > 1) {
binding.shoppingListItem.text = recipe.ingredients[0].original
for(i in 1 until recipe.ingredients.size) {
val checkBox = CheckBox() //Not sure what to put here as the context for my recyclerview List Item
checkBox.text = recipe.ingredients[i].original
checkBox.layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
binding.shoppingListContainer.addView(checkBox)
}
} else {
binding.shoppingListItem.text = recipe.ingredients[0].original
}
}
}
I have tried passing a context parameter through ShoppingListViewHolder.kt and in turn adding that parameter to the adapter and so forth like so.
class ShoppingListViewHolder(val binding: ItemShoppingListBinding, val context: Context) : RecyclerView.ViewHolder(binding.root) {
fun bind(snapshot: DocumentSnapshot) {
val recipe = snapshot.toObject<Recipe>() ?: return
binding.listTitle.text = recipe.name
if(recipe.ingredients.size > 1) {
binding.shoppingListItem.text = recipe.ingredients[0].original
for(i in 1 until recipe.ingredients.size) {
val checkBox = CheckBox(context)
...
}
ShoppingListFragment.kt
// Init the adapter to hold the recipe objects.
adapter = object : ShoppingListAdapter(query, requireContext()) {
override fun onDataChanged() {
if (itemCount == 0) {
shoppingListRecycler.visibility = View.GONE
viewEmpty.visibility = View.VISIBLE
} else {
shoppingListRecycler.visibility = View.VISIBLE
viewEmpty.visibility = View.GONE
}
}
override fun onError(e: FirebaseFirestoreException) {
// Show a snackbar on errors
view?.let {
Snackbar.make(
it,
"Error: check logs for info.", Snackbar.LENGTH_LONG
).show()
}
}
}
But all that did was create a huge amount of white space between entries. After some debugging, it looks like passing the context from the fragment gave it the MainActivity context. I'm not too sure where to go from here and any help would be appreciated. Thank you!
you can use binding.root.context except passing context
I created a WaterQualityChecksDialogFragment, please correct if I have not written it correctly.
WaterQualityChecksDialogFragment.kt
class WaterQualityChecksDialogFragment : DialogFragment() {
#Inject
private lateinit var app: App
fun newTargetInstance(): WaterQualityChecksDialogFragment {
val fragment = WaterQualityChecksDialogFragment()
return fragment
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
app!!.bus.post(ScreenDimEvent(false))
val builder = AlertDialog.Builder(activity!!.applicationContext)
// Get the layout inflater
val inflater = activity!!.layoutInflater
#SuppressLint("InflateParams")
val inflatedView = inflater.inflate(R.layout.dialog_water_quality_cheks, null)
builder
.setView(inflatedView)
.setCancelable(false)
/*
* setPositiveButton and setNeutralButton are also set (and overridden) in {#link #onStart}
* to stop Android automatically closing the dialog on click
*/
.setPositiveButton(R.string.fuel_order_signature_dialog_save) { dialog, id ->
}
.setNeutralButton(R.string.fuel_order_signature_dialog_clear) { dialog, id ->
}
val dialog = builder.create()
// Stop touch events outside the dialog from cancelling/dismissing
dialog.setCanceledOnTouchOutside(false)
return dialog
}
interface WaterQCChecksDialogListener {
fun onDialogPositiveClick(dialog: WaterQualityChecksDialogFragment)
fun onDialogNegativeClick(dialog: WaterQualityChecksDialogFragment)
}
}
ServiceOrderDialogHelper.java
public void showWaterQCChecksDioalog(){
//Cannot call newTargetInstance() method.
waterQualityChecksDialogFragment = WaterQualityChecksDialogFragment.newTargetInstance()
}
Put newTargetInstance() inside companion object to make this static like java
companion object {
fun newTargetInstance(): WaterQualityChecksDialogFragment {
val fragment = WaterQualityChecksDialogFragment()
return fragment
}
}
And call from java like below:
WaterQualityChecksDialogFragment.Companion.newTargetInstance();
Please check this code, it may helps you
class DialogClassSample : DialogFragment() {
companion object {
fun newInstance(): DialogClassSample {
val dialog = DialogClassSample()
return dialog
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
isCancelable = false
return inflater.inflate(R.layout.view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setListener()
}
fun setListener() {
button.setOnClickListener({
dismiss()
})
}
override fun onResume() {
super.onResume()
val window = dialog?.window
val size = Point()
val display = window?.windowManager?.defaultDisplay
display?.getSize(size)
val width = size.x
window?.setLayout((width * 0.85).toInt(), WindowManager.LayoutParams.WRAP_CONTENT)
window?.setGravity(Gravity.CENTER)
}
}
and call fron your class
val dialog = DialogClassSample.newInstance()//show dialog description according to user type
dialog.show(activity?.supportFragmentManager?.beginTransaction(), DialogClassSample::class.java.name)
In on resume we have set dialog size dynamically ,because in some bigger size dialog its look not good so you can use as per your requiremnent
I have some problem with showing PopupWindow which is the popup overlaps with the keyboard. here my result
Here my popup code:
class NoteHintList(val view: View, val listener: MenuDetailFragment.OnItemListClickListener?){
var noteHintList: List<String> = ArrayList<String>()
val popupWindow = PopupWindow(view.context)
fun setNoteList(noteHint: List<String>){
this.noteHintList = noteHint
}
// ============ TODO: Pop up note hint ================================================================
fun popupWindow(): PopupWindow {
val popUpContents = arrayOfNulls<String>(noteHintList.size)
ArrayList(noteHintList).toArray(popUpContents)
val listView = ListView(view.context)
listView.adapter = noteHintAdapter(popUpContents)
// set the item click listener
listView.setOnItemClickListener { parent, view, position, id ->
listener!!.onItemListClickListener(listView.getItemAtPosition(position).toString())
}
// val params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
// params.setMargins(16,0,16,0)
popupWindow.setFocusable(false)
popupWindow.contentView = view
// popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT - 32)
// popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT)
popupWindow.setContentView(listView)
return popupWindow
}
// ============ TODO: Popup list adapter for pop up ===================================================
private fun noteHintAdapter(array: Array<String?>): ArrayAdapter<Any> {
return object : ArrayAdapter<Any>(view.context, android.R.layout.simple_list_item_1, array) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// setting the ID and text for every items in the list
val text = getItem(position)!!.toString()
// visual settings for the list item
val listItem = TextView(context)
listItem.setText(text)
listItem.tag = position
listItem.textSize = 22f
listItem.setPadding(10, 10, 10, 10)
listItem.setTextColor(Color.WHITE)
return listItem
}
}
}
}
The popup will show when text changed. here my code
txtHint.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
val text = txtHint.text.toString()
if (Pattern.compile("^\\s+$").matcher(text).find() || text == "") {
popupNoteHintList!!.setNoteList(noteHintAllList)
} else if (text.split(",").size > 0) {
val textSplit = text.split(",")
val myData = noteHintAllList.filter { s ->
s.toLowerCase().contains(textSplit[textSplit.size - 1].toLowerCase().replace(Regex("^ | \$"), "")) &&
s.toLowerCase() != textSplit[textSplit.size - 1].toLowerCase().replace(Regex("(^ +)|( +$)"), "")
}
popupNoteHintList!!.setNoteList(myData)
if (myData.size > 0) {
popupNoteHintList!!.popupWindow().showAsDropDown(txtHint, 0, 0)
}
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
popupNoteHintList!!.popupWindow().dismiss()
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
popupNoteHintList!!.popupWindow().dismiss()
}
})
How to make the popup not overlap with keyboard, maybe move to above EditText if overlap, but still under EditText if not overlap
So result seem like this:
You can hide the soft keyboard in the afterTextChanged method, so that the keyboard is always hidden when your popup appears.
Within the afterTextChanged method, add the following to hide the keyboard:
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0) //Hide soft keyboard.