I need to implement ProgressDialogFragment which extends DialogFragment. Before showing I should set the text to the dialog fragment. Here's my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<ProgressBar
android:id="#+id/pb_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:progressDrawable="#drawable/progressbar"
android:theme="#style/ProgressBar" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_toEndOf="#id/pb_loading"
tools:text="Loading text" />
</RelativeLayout>
Here's my class ProgressDialogFragment:
class ProgressDialogFragment : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.progress_dialog_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
tv_message?.text = requireArguments().getString("message", "")
}
companion object {
fun newInstance(message: String) = ProgressDialogFragment().apply {
arguments = bundleOf(Pair("message", message))
}
}
}
So, as you can see, now I am creating a new instance each time when I should show the dialog. But is there any way to set the message to the object which already exists? I tried to add the method setMessage which sets message to textview and not to use the new instance in this case, but I caught an exception that textview for the message is not initialized. So, are there any ways to solve my problem?
Related
The problem here is when I'm returning from my support fragment to home fragment back every time my items in the recycle viewer got doubled. Each time i am shifting fragment to fragment my recycle viewer item in the homefragment got doubled. But when, I reopen the app its all got corrected but when i click on another fragment and come back the item in recycle viewer in home fragment got doubled. Kindly help me
// **HomeFragment.kt**
package com.service.bookitapp
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_home.*
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class HomeFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
private val arrCategory = ArrayList<CategoryModel>()
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)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView =view.findViewById<RecyclerView>(R.id.recyclerCategory)
arrCategory.add(CategoryModel(R.drawable.electrician,"Electrician"))
arrCategory.add(CategoryModel(R.drawable.plumber,"Plumber"))
arrCategory.add(CategoryModel(R.drawable.acservice,"AC Service"))
arrCategory.add(CategoryModel(R.drawable.carpentry,"Carpentry"))
arrCategory.add(CategoryModel(R.drawable.drop,"Pick up & Drop"))
arrCategory.add(CategoryModel(R.drawable.painting,"Painting"))
arrCategory.add(CategoryModel(R.drawable.waterfilter,"Water Filter Repair"))
arrCategory.add(CategoryModel(R.drawable.packer,"Pack and Move"))
recyclerView.layoutManager = GridLayoutManager(context,3)
val recyclerAdapter = context?.let { RecycleCategoryAdapter(it,arrCategory) }
recyclerView.adapter = recyclerAdapter
}
}
// **fragment_home.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAF9F6"
android:padding="5dp"
android:orientation="vertical"
tools:context=".HomeFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_user"
android:textColor="#color/teal_200"
android:textSize="28sp"
android:padding="8dp"
android:textStyle="bold"
tools:ignore="RelativeOverlap" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:src="#drawable/profile"
android:padding="4dp"
android:contentDescription="#string/app_name" />
</RelativeLayout>
<RelativeLayout
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/catg"
android:textStyle="bold"
android:textColor="#6E16e8"
android:layout_alignParentStart="true"
android:textSize="22sp"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="370dp"
android:id="#+id/recyclerCategory">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/offers"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:textColor="#6E16e8"
android:textSize="22sp"/>
<HorizontalScrollView
android:layout_margin="8dp"
android:layout_width="350dp"
android:layout_height="200dp"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="350dp"
android:layout_height="wrap_content"
android:src="#drawable/plumbingservice"
android:contentDescription="#string/elect" />
<ImageView
android:layout_width="350dp"
android:layout_height="wrap_content"
android:contentDescription="#string/elect"
android:src="#drawable/cleanview" />
<ImageView
android:layout_width="360dp"
android:layout_height="wrap_content"
android:contentDescription="#string/elect"
android:src="#drawable/elecview" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
The followig are the images
first solution, before add first item in list, call clear
arrCategory.clear()
second solution, populate your list in onCreate method of your fragment
Have you tried moving this
private val arrCategory = ArrayList<CategoryModel>(),
locally inside onViewCreated() ?
like this
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView =view.findViewById<RecyclerView>(R.id.recyclerCategory)
val arrCategory = ArrayList<CategoryModel>() // <-- like this?
arrCategory.add(CategoryModel(R.drawable.electrician,"Electrician")
...
...
...
or you can simply initialize the list like this, removing all of it out of the onCreate()
private val arrCategory = arrayListOf<CategoryModel>(
CategoryModel(R.drawable.electrician,"Electrician"),
CategoryModel(R.drawable.plumber,"Plumber"),
CategoryModel(R.drawable.acservice,"AC Service"),
CategoryModel(R.drawable.carpentry,"Carpentry"),
CategoryModel(R.drawable.drop,"Pick up & Drop"),
CategoryModel(R.drawable.painting,"Painting"),
CategoryModel(R.drawable.waterfilter,"Water Filter Repair"),
CategoryModel(R.drawable.packer,"Pack and Move")
)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView =view.findViewById<RecyclerView>(R.id.recyclerCategory)
recyclerView.layoutManager = GridLayoutManager(context,3)
val recyclerAdapter = context?.let { RecycleCategoryAdapter(it,arrCategory) }
recyclerView.adapter = recyclerAdapter
}
I have a LoginActivity in which there are two fragments. Using the navigation library, I switch between these fragments. But now I need to go from the fragment to another Activity. My code doesn't work for some reason, there are no errors in logCat. When I used this code with Activity everything works well. Is it all about a fragment or the navigation library?
My ViewModel
class AuthViewModel #Inject constructor(private var mAuth: FirebaseAuth) : ViewModel() {
var mEmail: String = ""
var mPassword: String = ""
var loginListener: LoginListener? = null
fun login(view: View) {
if (mEmail.isNotEmpty() && mPassword.isNotEmpty()) {
mAuth = FirebaseAuth.getInstance()
mAuth.signInWithEmailAndPassword(mEmail, mPassword).addOnCompleteListener {
if (it.isSuccessful) {
loginListener?.startLoading()
android.os.Handler().postDelayed({
loginListener?.endLoading()
loginListener?.validateLoginAndPassword()
}, 500)
} else {
loginListener?.showError(textResource = R.string.login_error)
}
}
} else {
loginListener?.showError(textResource = R.string.login_or_password_empty)
}
}
}
My Fragment
class LoginFragment : DaggerFragment(), View.OnClickListener, KeyboardVisibilityEventListener, LoginListener {
private lateinit var navController: NavController
lateinit var binding: FragmentLoginBinding
#Inject
lateinit var factory: ViewModelProvider.Factory
lateinit var mAuthViewModel: AuthViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
binding = FragmentLoginBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
view.btn_registration.setOnClickListener(this)
mAuthViewModel = ViewModelProviders.of(this#LoginFragment, factory).get(AuthViewModel::class.java)
}
override fun onClick(view: View?) {
when(view!!.id) {
R.id.btn_registration -> navController.navigate(R.id.action_loginFragment_to_registerFragment)
}
}
override fun validateLoginAndPassword() {
//startActivity(Intent(activity, ContactListActivity::class.java))
val intent = Intent(activity, ContactListActivity::class.java)
startActivity(intent)
}
My LoginFragment XML
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="loginViewModel"
type="com.infernal93.phonebookappmvvmanddagger.viewmodels.AuthViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
tools:context=".view.fragments.LoginFragment">
<ScrollView
android:id="#+id/root_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText
android:id="#+id/login_email"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:background="#drawable/bg_inputs"
android:ems="10"
android:hint="#string/email_text"
android:text="#={loginViewModel.mEmail}"
android:inputType="textEmailAddress"
android:textColorHint="#color/dark_gray" />
<EditText
android:id="#+id/login_password"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_below="#id/login_email"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/bg_inputs"
android:hint="#string/password_text"
android:text="#={loginViewModel.mPassword}"
android:inputType="textPassword"
android:textColorHint="#color/dark_gray" />
<com.github.rahatarmanahmed.cpv.CircularProgressView
android:id="#+id/cpv_login"
android:layout_width="#dimen/cpv_size"
android:layout_height="#dimen/cpv_size"
android:layout_below="#id/login_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:visibility="gone"
app:cpv_animAutostart="true"
app:cpv_color="#color/colorPrimary"
app:cpv_indeterminate="true" />
<Button
android:id="#+id/btn_login_enter"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_below="#id/cpv_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#drawable/bg_buttons"
android:text="#string/login_btn_text"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="#dimen/login_btns_text_size"
android:onClick="#{(view) -> loginViewModel.login(view)}"/>
<Button
android:id="#+id/btn_registration"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_below="#id/btn_login_enter"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#drawable/bg_buttons"
android:text="#string/registration_btn_text"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="#dimen/login_btns_text_size" />
</RelativeLayout>
</ScrollView>
</FrameLayout>
</layout>
You didn't bind ViewModel with View. Use below:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
view.btn_registration.setOnClickListener(this)
mAuthViewModel = ViewModelProviders.of(this#LoginFragment, factory).get(AuthViewModel::class.java)
//Bind viewmodel
binding.loginViewModel = mAuthViewModel
}
I forgot to specify LoginListener in the fragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
//return inflater.inflate(R.layout.fragment_login, container, false)
binding = FragmentLoginBinding.inflate(inflater, container, false)
mAuthViewModel = ViewModelProviders.of(this#LoginFragment, factory).get(AuthViewModel::class.java)
binding.loginViewModel = mAuthViewModel
// LoginListener
mAuthViewModel.loginListener = this
return binding.root
}
So I've been searching on internet for a while already now but I just can't seem to find the correct topic to help me out with this.
I have the following code which is relevant for this question:
This is my adapter class.
class SmoelenBoekAdapter(var profiles: Array<Profile>) : RecyclerView.Adapter<CustomViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val layout = layoutInflater.inflate(com.otten.nvvofrankversie.R.layout.recyclerview_smoelenboek, parent, false)
return CustomViewHolder(layout)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val positioner = profiles.get(position)
var profileImage = positioner.profile_image
var firstName = positioner.first_name
var lastName = positioner.last_name
var plaats = positioner.place
var adres = positioner.address
holder.view.naamSmoelenBoek.text = firstName.string + " " + lastName.string
}
override fun getItemCount(): Int {
return profiles.size
}
}
class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view)
This is my 'MainActivity' (for this particular fragment)
class SmoelenBoek : ApplicationFragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_smoelenboek, null)
}
/*override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
}*/
}
xml where adapterinfo has to be filled in:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/profielPicSmoelenBoek"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/business_icon" />
<TextView
android:id="#+id/naamSmoelenBoek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Frank Otten"
android:textColor="#color/colorPrimaryDark"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/profielPicSmoelenBoek"
app:layout_constraintStart_toEndOf="#+id/profielPicSmoelenBoek"
app:layout_constraintTop_toBottomOf="#+id/profielPicSmoelenBoek" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:text=">"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/naamSmoelenBoek" />
</androidx.constraintlayout.widget.ConstraintLayout>
And this is the fragment where I have a recyclerview in which needs to be filled with the above xml:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView8"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/offer_bg"
android:adjustViewBounds="true"
android:scaleType="center"/>
<Button
android:id="#+id/searchInSmoelenBoek"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="15dp"
android:background="#color/lightGray"
android:hint="Zoek in smoelenboek"
android:padding="10dp"
android:textAlignment="textStart"
android:textColor="#color/colorAccent"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="60dp"
android:text="Vind gemakkelijk alle\naangesloten orthodontisten"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/searchInSmoelenBoek" />
<TextView
android:id="#+id/textView15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:text="Aangesloten orthodontisten"
android:textAlignment="textStart"
android:textColor="#color/browser_actions_title_color"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView8" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/smoelenboekRecyclerView"
android:layout_width="409dp"
android:layout_height="440dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView15"/>
</androidx.constraintlayout.widget.ConstraintLayout>
What am I doing wrong or missing here?
I think I still need to set the adapter correctly, but I can't figure that out either...
Hope anyone can help me!
// Set layout manager for recycler view
smoelenboekRecyclerView.layoutManager = LinearLayoutManager(this);
// Set adapter to recycler view
smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view){
val tvNaamSmolText = view.naamSmoelenBoek
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val positioner = profiles.get(position)
var profileImage = positioner.profile_image
var firstName = positioner.first_name
var lastName = positioner.last_name
var plaats = positioner.place
var adres = positioner.address
holder.tvNaamSmolText.text = firstName.string + " " + lastName.string
}
Try this
class SmoelenBoek : ApplicationFragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
View view = inflater.inflate(R.layout.fragment_smoelenboek, null)
view.smoelenboekRecyclerView.layoutManager = LinearLayoutManager(this);
view.smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
return view
}
}
}
class SmoelenBoek : ApplicationFragment(){
var profiles: Array<Profile> = arrayOf()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_smoelenboek, null)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
SharedInstance.api.getAllUsers {
profiles = it
smoelenboekRecyclerView.layoutManager = LinearLayoutManager(context)
smoelenboekRecyclerView.adapter = SmoelenBoekAdapter(profiles)
}
}
}
This is the solution to my problem.
I had to put the layoutmanager and adapter in OnviewCreated AFTER the async call (which I forgot to implement so that was part of the problem first) so the layoutmanager could be set and the adapter could fill my recyclerview with items.
Thanks for the help anyways guys!
so I'm trying to make a PagerView via Fragments containing a multiple BaseAdapters in them, but I ran into a little problem with BaseAdapter, I have multiple Fragment, but I'll post 1 as an example
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
listViewSpells?.adapter = AllSpells()
return inflater.inflate(R.layout.activity_spell, container, false)
}
}
class AllSpells: BaseAdapter() {
override fun getCount(): Int {
return player.learnedSpells.size/5+1
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItem(position: Int): Any {
return "TEST STRING"
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
val rowMain: View
val index:Int = if(position == 0) 0 else{
position*5
}
if (convertView == null) {
val layoutInflater = LayoutInflater.from(viewGroup!!.context)
rowMain = layoutInflater.inflate(R.layout.row_spells_managment, viewGroup, false)
val viewHolder = ViewHolder(rowMain.buttonSpellsManagment1,rowMain.buttonSpellsManagment2,rowMain.buttonSpellsManagment3,rowMain.buttonSpellsManagment4)
rowMain.tag = viewHolder
} else rowMain = convertView
val viewHolder = rowMain.tag as ViewHolder
viewHolder.buttonSpellsManagement1.setBackgroundResource(player.learnedSpells[index]!!.drawable)
viewHolder.buttonSpellsManagement2.setBackgroundResource(player.learnedSpells[index+1]!!.drawable)
viewHolder.buttonSpellsManagement3.setBackgroundResource(player.learnedSpells[index+2]!!.drawable)
viewHolder.buttonSpellsManagement4.setBackgroundResource(player.learnedSpells[index+3]!!.drawable)
viewHolder.buttonSpellsManagement1.setOnClickListener {
}
viewHolder.buttonSpellsManagement2.setOnClickListener {
}
viewHolder.buttonSpellsManagement3.setOnClickListener {
}
viewHolder.buttonSpellsManagement4.setOnClickListener {
}
return rowMain
}
private class ViewHolder(val buttonSpellsManagement1: Button, val buttonSpellsManagement2: Button, val buttonSpellsManagement3: Button, val buttonSpellsManagement4: Button)
}
I was originally using activities, but in the case of PagerView I've changed it to Fragment. What I know is, that I can't call this etc. what I found in java codes was in some cases getActivity() instead of this, which I couldn't find in Kotlin. So by saying that it could be in the viewGroup!!.context in the BaseAdapter.
Any help will be appreciated, thanks.
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:screenOrientation="landscape">
<android.support.constraint.Guideline
android:id="#+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<android.support.constraint.Guideline
android:id="#+id/guideline30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:orientation="horizontal"
app:layout_constraintEnd_toStartOf="#+id/listViewSpells"
app:layout_constraintGuide_percent="0.4"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="87dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/guideline7"
app:layout_constraintGuide_percent="0.1925"
app:layout_constraintStart_toEndOf="#+id/guideline11" />
<android.support.constraint.Guideline
android:id="#+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="83dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/guideline10"
app:layout_constraintGuide_percent="0.05"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="86dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/guideline14"
app:layout_constraintGuide_percent="0.4825"
app:layout_constraintStart_toEndOf="#+id/guideline7" />
<android.support.constraint.Guideline
android:id="#+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="7dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/imageView3"
app:layout_constraintGuide_percent="0.63"
app:layout_constraintStart_toEndOf="#+id/guideline13" />
<android.support.constraint.Guideline
android:id="#+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.774" />
<android.support.constraint.Guideline
android:id="#+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.92" />
<android.support.constraint.Guideline
android:id="#+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="83dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintGuide_percent="0.34"
app:layout_constraintStart_toEndOf="#+id/guideline10" />
<ListView
android:id="#+id/listViewSpells"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline14"
app:layout_constraintStart_toEndOf="#+id/guideline30"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewInfoSpell"
android:layout_width="0dp"
android:layout_height="141dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline9"
app:layout_constraintDimensionRatio="H,1:1.25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline30" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="75dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="#+id/guideline30"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toBottomOf="#+id/imageView3" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="0dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="7dp"
app:layout_constraintDimensionRatio="H,"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/guideline14"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I think you need to do your adapter works after declaring a view. In activity, after calling onCreate method we can get a view for our other works. Its slighting different in fragments. We can have view after inflating your view.
First, create a view as a first line of your onCreateView function.
val view = inflater!!.inflate(R.layout.activity_spells, container, false)
Second, return that view from your onCreateView function.
Finally, you can put(use) your other works between those. In that way, you can use the viwe (your fragment) as a variable view. Your SpellManagement class will be looks like this :
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.R.layout.activity_spells, container, false)
\**
Your other codes or works here
*\
listViewSpells?.adapter = AllSpells()
return view
}
}
I think this might helps. Cheer!
So... this was REALLY strange to me, the answer was to create a new ListView with the old nowadays not very used "view.FindViewById(...)" thingie, which I've never used before because of the plugins, that allows you to straight up write the name of the component such as "listView.adapter = ..." instead of external defining it by "view.findViewById(...)" thingie. Also with the combination of this answer by MinnKhant.The final code looks as follows:
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.activity_spell_managment, container, false)
val listView:ListView = view.findViewById(R.id.listViewSpells)
listView.adapter = AllSpells()
return view
}
}
Cheers
Edit: or just write view.listViewSpells.adapter = AllSpells(), instead of findViewById(...)
I have two Fragments hosted in one Activity. They are dead simple:
class OneFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.fragment_one, container, false)
val sharedView = view.findViewById<View>(R.id.shared_view)
ViewCompat.setTransitionName(sharedView, "test")
view.findViewById<Button>(R.id.go_button).setOnClickListener {
val fragmentTransaction = fragmentManager.beginTransaction()
val oneFragment = fragmentManager.findFragmentById(R.id.fragment_container_frame_layout)
val twoFragment = TwoFragment()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val moveTransition = TransitionInflater.from(activity).inflateTransition(android.R.transition.move)
oneFragment.exitTransition = Fade()
val transitionSet = TransitionSet()
transitionSet.addTransition(TransitionInflater.from(activity).inflateTransition(android.R.transition.move))
transitionSet.duration = 600
twoFragment.sharedElementEnterTransition = transitionSet
twoFragment.enterTransition = Fade()
fragmentTransaction.addToBackStack(TwoFragment::class.java.simpleName)
fragmentTransaction.addSharedElement(sharedView, "test")
fragmentTransaction.replace(R.id.fragment_container_frame_layout, twoFragment)
fragmentTransaction.commit()
}
else {
fragmentTransaction.addToBackStack(TwoFragment::class.java.simpleName)
fragmentTransaction.replace(R.id.fragment_container_frame_layout, twoFragment)
fragmentTransaction.commit()
}
}
return view
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kravtsov.transitiontest.MainActivity">
<View
android:id="#+id/shared_view"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerInParent="true"
android:background="#android:color/holo_blue_dark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/shared_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="HELLO"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="GO"
tools:ignore="HardcodedText" />
</RelativeLayout>
And the second one:
class TwoFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.fragment_two, container, false)
val sharedView = view.findViewById<View>(R.id.shared_view)
ViewCompat.setTransitionName(sharedView, "test")
return view
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kravtsov.transitiontest.MainActivity">
<View
android:id="#+id/shared_view"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_below="#+id/anchor"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_blue_dark" />
<TextView
android:id="#+id/anchor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="WORLD"
tools:ignore="HardcodedText" />
</RelativeLayout>
As you can see they have common shared view, that i want to animate during transition. The problem is - animation never appears. I can see that enter and exit fade transition for the rest of elements working correctly. Only shared element transition do not work.
I've searched web a lot and mimic a lot of guiedes... No answers i found still. Does anyone face such an issue? Where shoud i start to fix this bug?
Your code is working for me. I've used your layout and kotlin code, and it works seamlessly. One thing I took care of is importing all classes of support packages.