Home > database >  How to reference a PagerView ID from an .kt file
How to reference a PagerView ID from an .kt file

Time:12-16

I am new to Kotlin and Android Studio, I'm trying to work my way to a bit advance stuffs.. I am trying to make an App with View Pager 2 and circle indicator..

It's more difficult than it ought be because I'm working from a couple of fragments..

Long story short, They is a particular fragment I want to give the View Pager and circle indicator feature, but it's confusing for me working the backend.

So I this what the .xml of that fragment I want to give the features looks like:

<?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="match_parent"
    tools:context=".StoriesFragment">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@ id/ssViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <me.relex.circleindicator.CircleIndicator3
        android:id="@ id/ssCircleIndicator"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/teal_700"
        app:layout_constraintBottom_toBottomOf="@ id/ssViewPager"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

and the .kt file:

package com.example.firstmillion

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class StoriesFragment : Fragment(R.layout.fragment_stories) {

    private var titlesList = mutableListOf<String>()
    private var imagesList = mutableListOf<Int>()

    // unsure of the place
    postToList()

    // I didn't complete this code
    ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)
    ssViewPager.orientation =
    ssViewPager.ad

    private fun addToList(title: String, image: Int) {
        titlesList.add(title)
        imagesList.add(image)
    }

    private fun postToList() {
        for (i in 1..5) {
            addToList("Title $i", R.drawable.ss_coco_chanel)
        }

    }

}

I was following a tutorial when I got all this, The problem is I get a warning Error, couple of them actually from the .kt file when I write the following:

postToList()

    // This is code is not complete RN
    ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)
    ssViewPager.orientation =
    ssViewPager.ad

I actually don't know if I doing this right, I thought I will write the backend code in the.kt file of the fragment I want to give the feature, though the tutorial I was following wrote it in the MainActivity.kt file, he was working on a single screen though..

In sumary, My Questions are:

  1. Is it wrong that I'm writing the backend code in the .kt file of the fragment I want to give the feature, and not the MainActivity.kt?
  2. In the Tutorial he wrote this particular code giving me error (the one I pointed out above) inside the onCreate function. I tried to do same, as I cleared the .kt file of the fragment I want to have the feature, and put the codes in MainActivity.Kt I still got another error warning.

(Here's what the .kt of the fragment now looks like:)

package com.example.firstmillion

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class StoriesFragment : Fragment(R.layout.fragment_stories) {

}

(And here's what the MainActivity looks like code:)

class MainActivity : AppCompatActivity() {

    //ss
    private var titlesList = mutableListOf<String>()
    private var imagesList = mutableListOf<Int>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val homeFragment = HomeFragment()
        val storiesFragment = StoriesFragment()
        val menuFragment = MenuFragment()

        //This set the fragment that shows when the App is Opened
        //In this case homeFragment
        setCurrentFragment(homeFragment)

        //This makes switching to other fragments possible
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
        bottomNavigationView.setOnNavigationItemSelectedListener {
            when(it.itemId) {
                R.id.miHome -> setCurrentFragment(homeFragment)
                R.id.miStories -> setCurrentFragment(storiesFragment)
                R.id.miMenu -> setCurrentFragment(menuFragment)
            }
            true
        }

        //for viewpager of success story
        postToList()

        ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)
        ssViewPager.orientation = ss
        ssViewPager.ad

    }
    private fun setCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.fl_fragment, fragment)
            commit()
        }

    // ss
    private fun addToList(title: String, image: Int) {
        titlesList.add(title)
        imagesList.add(image)
    }

    private fun postToList() {
        for (i in 1..5) {
            addToList("Title $i", R.drawable.ss_coco_chanel)
        }

    }
  1. I noticed it is when I'm referening the function postToList() I get an error, though I don't get an error when I referenced it in my MainActivity.kt only in the .kt file of the fragement, so where is the best place for me to write this.
  2. The final and most important question after the function, the other code giving a warning error is an ID. called like ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList) the ssViewPager is an ID, this is exactly how it was in the tutorial, Please am I missing something??

This a very long question, I really apprecate your help, Thanks you for going through it. Thanks for your feedback. Thanks for your patience. And Thanks for your time..

CodePudding user response:

There are many ways to approach this, first one would be passing the CircleIndicator do the adapter and do what you need in that adapter.

ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList, circleIndicator)

Other would be to pass the fragment and call a function from that fragment to deal with the CircleIndicator

ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList, storiesFragment)

in stories fragment make a function to execute something for the circle indicator and call it in the adapter

storiesFragment.stopCircleIndicator()

CodePudding user response:

All I need to do was to use findViewByID for the ssViewPager

which will make the MainActivity.kt look soemthing like this:

class MainActivity : AppCompatActivity() {

    //ss
    private var titlesList = mutableListOf<String>()
    private var imagesList = mutableListOf<Int>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val homeFragment = HomeFragment()
        val storiesFragment = StoriesFragment()
        val menuFragment = MenuFragment()

        //This set the fragment that shows when the App is Opened
        //In this case homeFragment
        setCurrentFragment(homeFragment)

        //This makes switching to other fragments possible
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
        bottomNavigationView.setOnNavigationItemSelectedListener {
            when(it.itemId) {
                R.id.miHome -> setCurrentFragment(homeFragment)
                R.id.miStories -> setCurrentFragment(storiesFragment)
                R.id.miMenu -> setCurrentFragment(menuFragment)
            }
            true
        }

        //for viewpager of success story
        postToList()

        val ssViewPager: ViewPager2 = findViewById(R.id.ssViewPager)

        ssViewPager.adapter = StoriesViewPagerAdapter(titlesList, imagesList)
        ssViewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL

        val ssCircleIndicator = findViewById<CircleIndicator3>(R.id.ssCircleIndicator)
        ssCircleIndicator.setViewPager(ssViewPager)

    }
    private fun setCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.fl_fragment, fragment)
            commit()
        }

    // ss
    private fun addToList(title: String, image: Int) {
        titlesList.add(title)
        imagesList.add(image)
    }

    private fun postToList() {
        for (i in 1..5) {
            addToList("Title $i", R.drawable.ss_coco_chanel)
        }

    }
}

I left the .kt for the stories fragment empty and wrote the code in the MainActivity.kt it kinda works now. Thanks for your time..

  • Related