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!
Related
So I'm doing a project and I'm looking to see if my RecyclerView works. Here's what I got so far
The data class:
#Parcelize
#Entity(tableName = "asteroid_feed")
data class Asteroid(val id: Long, val codename: String, val closeApproachDate: String,
val absoluteMagnitude: Double, val estimatedDiameter: Double,
val relativeVelocity: Double, val distanceFromEarth: Double,
val isPotentiallyHazardous: Boolean) : Parcelable
The Adapter
class AsteroidViewAdapter (private val list: MutableList<Asteroid>) : ListAdapter<Asteroid, AsteroidViewAdapter.AsteroidViewHolder>(DiffCallback) {
companion object DiffCallback : DiffUtil.ItemCallback<Asteroid>() {
override fun areItemsTheSame(oldItem: Asteroid, newItem: Asteroid): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Asteroid, newItem: Asteroid): Boolean {
return oldItem == newItem
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): AsteroidViewHolder {
return AsteroidViewHolder(AsteroidListContainerBinding.inflate(LayoutInflater.from(parent.context)))
}
override fun onBindViewHolder(holder: AsteroidViewHolder, position: Int) {
holder.bind(getItem(position))
}
class AsteroidViewHolder (private val binding: AsteroidListContainerBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: Asteroid){
binding.value = item
}
}
}
The Fragment
class MainFragment : Fragment() {
private lateinit var manager: RecyclerView.LayoutManager
private val viewModel: MainViewModel by lazy {
ViewModelProvider(this).get(MainViewModel::class.java)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val binding = FragmentMainBinding.inflate(inflater)
binding.lifecycleOwner = this
binding.viewModel = viewModel
val mutableList: MutableList<Asteroid> = ArrayList()
mutableList.add(Asteroid(1, "fgnuugrhrg", "bihagtyjerwailgubivb", 4.0, 8.0,3.0, 9.0, false))
mutableList.add(Asteroid(2, "fguk.nuugrhrg", "bidjswjyhagrwailgubivb", 3.0, 90.0,355.0, 9.0, true))
mutableList.add(Asteroid(3, "fgnssuugrhrg", "bshjtihagrwailgubivb", 4.0, 33.0,33.0, 9.0, false))
mutableList.add(Asteroid(4, "fgnuw4suugrhrg", "bjsryjihagrwailgubivb", 6.0, 8.0,11.0, 9.0, true))
mutableList.add(Asteroid(5, "fgnuugrudkdkhrg", "bihjjkkuagrwailgubivb", 4.0, 5.0,77.0, 9.0, false))
manager = LinearLayoutManager(this.context)
binding.asteroidRecycler.apply {
adapter = AsteroidViewAdapter(mutableList)
layoutManager = manager
}
setHasOptionsMenu(true)
return binding.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.main_overflow_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return true
}
}
XML Files:
The Layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.udacity.asteroidradar.main.MainViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_background">
<FrameLayout
android:id="#+id/activity_main_image_of_the_day_layout"
android:layout_width="match_parent"
android:layout_height="220dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/activity_main_image_of_the_day"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:srcCompat="#drawable/placeholder_picture_of_day"/>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textColor="#android:color/white"
android:textStyle="bold"
android:textSize="20sp"
android:layout_gravity="bottom"
android:background="#55010613"
android:text="#string/image_of_the_day" />
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/asteroid_recycler"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/activity_main_image_of_the_day_layout"
app:layout_constraintVertical_bias="0.0" />
<ProgressBar
android:id="#+id/status_loading_wheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
The Container:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="value"
type="com.udacity.asteroidradar.Asteroid" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/app_background">
<TextView
android:id="#+id/codename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:textColor="#color/default_text_color"
android:textSize="20sp"
android:textStyle="bold"
android:text="#{value.codename}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="#string/codename" />
<TextView
android:id="#+id/date_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textColor="#color/default_text_color"
android:text="#{value.closeApproachDate}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/codename"
app:layout_constraintTop_toBottomOf="#+id/codename"
tools:text="#tools:sample/date/mmddyy" />
<ImageView
android:id="#+id/danger_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/codename"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/ic_status_potentially_hazardous" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
As you can see, I was using a MutableList to fill in the fields and having that list viewed on my RecyclerView.ListAdapter in the fragment_main but it is not showing anything. Again this is just checking to see if The RecyclerView is working. Thanks for the Help.
The issue that you send the list to the adapter, but didn't submit it using submitList()
You can do that in init{} of the adapter
class AsteroidViewAdapter(private val list: MutableList<Asteroid>) :
ListAdapter<Asteroid, AsteroidViewAdapter.AsteroidViewHolder>(DiffCallback) {
init {
submitList(list)
}
// omitted code
Based on my understanding list view doesn't know what size of the items in the list to populate. So you need to override item list count by below inside list adapter class.
override fun getItemCount(): Int {
return items.size
}
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
}
I'm currently trying to make a scrollable grid of ScratchViews, but the views that have already been scratched by the user, are appearing as not scratched when the user scrolls down and back up again, because the adapter is repainting each of the views back to their original unscratched state.
I've tried the solutions posted here: I want my RecyclerView to not recycle some items, but preventing the views from being recycled apparently doesn't prevent them from being redrawn.
Here is my code:
Fragment:
class ScratchOffGameFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_scratch_off_game, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var data = ArrayList<ScratchOffGameItem>()
for (i in 1..20) {
var aux = ScratchOffGameItem()
data.add(aux)
}
scratchOffGameRecycle.layoutManager = GridLayoutManager(context, 3)
val scratchOffGameAdapter = ScratchOffGameAdapter(activity!!,data)
scratchOffGameRecycle.recycledViewPool.setMaxRecycledViews(1,0);
scratchOffGameRecycle.adapter = scratchOffGameAdapter
}
Adapter:
class ScratchOffGameAdapter(var context: Context, private val mData: ArrayList<ScratchOffGameItem>) : RecyclerView.Adapter<ScratchOffGameAdapter.ViewHolder>() {
private val mInflater: LayoutInflater = LayoutInflater.from(context)
inner class ViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView)
#NonNull
override fun onCreateViewHolder(#NonNull parent: ViewGroup, viewType: Int): ViewHolder {
val view = mInflater.inflate(R.layout.scratch_off_game_item, parent, false)
return ViewHolder(view)
}
// binds the data to the TextView in each cell
override fun onBindViewHolder(#NonNull holder: ViewHolder, position: Int) {
holder.setIsRecyclable(false)
}
override fun onViewAttachedToWindow(holder: ViewHolder) {
super.onViewAttachedToWindow(holder)
ScratchoffController(context)
.setThresholdPercent(0.80)
.setTouchRadiusDip(context, 30)
.attach(holder.itemView.scratch_view, holder.itemView.scratch_view_behind)
}
// total number of cells
override fun getItemCount(): Int {
return mData.size
}
override fun getItemViewType(position: Int): Int {
return 1
}
}
Item being painted by the adapter (scratch_off_game_item.xml):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/achievementItem"
android:layout_width="110dp"
android:layout_height="90dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:background="#drawable/achievements_border">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/scratch_view_behind"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/zing_rust_demo_base"
android:gravity="center"
android:text="20"
android:textColor="#color/black"
android:textSize="24sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
<com.jackpocket.scratchoff.views.ScratchableLinearLayout
android:id="#+id/scratch_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#color/gold" >
<ImageView
android:layout_width="73dp"
android:layout_height="42dp"
android:src="#drawable/scratch_here_ic" />
</com.jackpocket.scratchoff.views.ScratchableLinearLayout>
</RelativeLayout>
</FrameLayout>
And RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="#+id/scratchOffGameRecycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/youWin"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="10dp"
android:background="#drawable/bg_gold_rectangle"
android:columnWidth="100dp"
android:numColumns="3" />
As additional information, the libraries that I've tried using are these: 'com.jackpocket:scratchoff:1.3.0', 'com.github.cooltechworks:ScratchView:v1.1'in conjuction with both recycler and grid views to no avail.
What I ended up doing was changing the scratchoff library to this one: 'com.goibibo.libs:scratchcardview:0.1.6' ('com.jackpocket:scratchoff:1.3.0' had problems setting the listener inside the OnBindViewHolder method) and making a compromise to only save fully scratched views and not partially scratched ones. Here are the modifications I made:
OnBindViewHolder:
override fun onBindViewHolder(#NonNull holder: ViewHolder, position: Int) {
holder.itemView.number.text = mData[position].number
var scratchRelativeLayoutView = holder.itemView.scratch_card
holder.setIsRecyclable(false)
if(mData[position].isScratched && scratchRelativeLayoutView!=null) {
scratchRelativeLayoutView.visibility = View.GONE
holder.itemView.numberHidden.text = mData[position].number
holder.itemView.numberHidden.visibility = View.VISIBLE
}
else {
scratchRelativeLayoutView.setStrokeWidth(5)
scratchRelativeLayoutView.setScratchView(R.layout.lyt_scratch) // scratchable layout
scratchRelativeLayoutView.setRevealListener(object : ScratchRelativeLayoutView.IRevealListener {
override fun onRevealed(tv: ScratchRelativeLayoutView) {
mData[position].isScratched = true
}
override fun onRevealPercentChangedListener(siv: ScratchRelativeLayoutView, percent: Float) {
if (percent>0.5) {
siv.reveal()
}
}
})
}
}
scratch_off_game_item.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/achievementItem"
android:layout_width="90dp"
android:layout_height="90dp">
<TextView
android:id="#+id/numberHidden"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/zing_rust_demo_base"
android:gravity="center"
android:text="23"
android:background="#color/white_scratch_off"
android:textColor="#color/black"
android:textSize="24sp"
android:layout_gravity="center"/>
<com.goibibo.libs.views.ScratchRelativeLayoutView
android:id="#+id/scratch_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_scratch_off">
<TextView
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/zing_rust_demo_base"
android:gravity="center"
android:textColor="#color/black"
android:textSize="24sp"
android:layout_centerInParent="true"/>
</com.goibibo.libs.views.ScratchRelativeLayoutView>
</FrameLayout>
I faced a strange problem : I can't add more than one object dynamically on a LinearLayout , if they were already things , that works, So That can display multiple views
I tried with different views , there is only problems when i try to push one after the other
(there is two buttons in index 0 and 1 to test for multiple objects on the layout)
CODE :
val view = layoutInflater.inflate(R.layout.sensor_item, container, false)
val view2 = layoutInflater.inflate(R.layout.nosensor_item, container, false)
val insertPoint = viewOfLayout.findViewById(R.id.box_Parent) as LinearLayout
insertPoint.addView(view, 2)
//insertPoint.addView(view2, 3) //works
This Works too :
//insertPoint.addView(view, 3)
insertPoint.addView(view2, 2) //works
This doesn't works :
insertPoint.addView(view, 2)
insertPoint.addView(view2, 3) //doesn't works
Full Code (fragments.kt):
package com.corrupted.radheat.TEMPER
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import kotlinx.android.synthetic.main.sensor_item.view.*
class EditFragment : Fragment() {
lateinit var option : Spinner
lateinit var result : TextView
private lateinit var viewOfLayout: View
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewOfLayout = inflater.inflate(R.layout.edit_fragment, container, false)
//create a view to inflate the layout_item (the xml with the textView created before)
val view = layoutInflater.inflate(R.layout.sensor_item, container, false)
val view2 = layoutInflater.inflate(R.layout.nosensor_item, container, false)
val insertPoint = viewOfLayout.findViewById(R.id.box_Parent) as LinearLayout
insertPoint.addView(view, 2)
insertPoint.addView(view2, 3)
option = view.spinner as Spinner
result = view.textView7 as TextView
val options = arrayOf("A","V")
option.adapter = ArrayAdapter<String>(activity,android.R.layout.simple_list_item_1,options)
option.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
result.text = "0"
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
result.text = options[position]
}
}
return viewOfLayout
}
class InfoFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.info_fragment, container, false)
}
}
class ParamsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.parameters_fragment, container, false)
}
}
Code of the fragment :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/box_Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Code of the added view :
<?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:fillViewport="true"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="12dp"
android:background="#color/background_Item_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:text=".0 C°"
android:textColor="#color/Text_Color_Light"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/textView8"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toTopOf="#+id/textView8"
app:layout_constraintVertical_bias="0.625" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginStart="4dp"
android:text=".0 C°"
android:textColor="#color/Text_Color_Light"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/textView5"
app:layout_constraintStart_toEndOf="#+id/textView5"
app:layout_constraintTop_toTopOf="#+id/textView5"
app:layout_constraintVertical_bias="0.79" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="00"
android:textColor="#color/Text_Color_Light"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageButton"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.937" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:text="#string/ROOMtext"
android:textColor="#color/Text_Color_Light"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/spinner"
android:layout_width="45dp"
android:layout_height="43dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="8dp"
android:background="#color/UI_ITEM_OVERLAP_LIGHT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="40dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_keyboard_arrow_up_black_24dp" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="40dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageButton"
app:srcCompat="#drawable/ic_keyboard_arrow_down_black_24dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="00"
android:textColor="#color/Text_Color_Light"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.652" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.kt :
package com.corrupted.radheat.TEMPER
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.widget.*
class MainActivity : AppCompatActivity() {
private val fragmentManager = supportFragmentManager
private val fragmentTransaction = fragmentManager.beginTransaction()
private val editfragment = EditFragment()
private val infofragment = InfoFragment()
private val parametersfragment = ParamsFragment()
private fun showFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit()
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.action_edit -> {
showFragment(editfragment)
return#OnNavigationItemSelectedListener true
}
R.id.action_info-> {
showFragment(infofragment)
return#OnNavigationItemSelectedListener true
}
R.id.action_Settings-> {
showFragment(parametersfragment)
return#OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
fragmentTransaction.add(R.id.fragment_container, infofragment)
fragmentTransaction.commit()
setContentView(R.layout.activity_main)
val navigation = findViewById<BottomNavigationView>(R.id.activity_main_bottom_navigation)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity.
}
}
Pic of it working : https://imgur.com/gallery/mLb4G5b
Thanks
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(...)