androidkotlinviewflipperandroid-viewflipper

AdapterViewFlipper stops flipping after two clicks


I have a simple AdapterViewFlipper with an onClickListener attached to its items.

When i click it should flip through all of the numbers in data array, "one" through to "seven".

It is only flipping on first two clicks up to "three". After that my adapterViewFlipper stops clicking and no flips occur. What am i doing wrong?


class MainActivity : AppCompatActivity() {

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

        val adapterViewFlipper = findViewById<AdapterViewFlipper>(R.id.activity_main_flipper)
        adapterViewFlipper.adapter = FlipperAdapter()
        
        val item = adapterViewFlipper.findViewById<TextView>(R.id.text_view)

        item.setOnClickListener {
            adapterViewFlipper.showNext()
        }
    }

    class FlipperAdapter() : BaseAdapter() {

        private val data = arrayOf("one", "two", "three", "four","five","six","seven")

        override fun getCount(): Int {
            return data.size
        }

        override fun getItem(p0: Int): Any {
            return data[p0]
        }

        override fun getItemId(p0: Int): Long {
            return p0.toLong()
        }

        override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
            val view = LayoutInflater.from(getAppInstance()).inflate(R.layout.flipper_item, null)
            val textView = view.findViewById<TextView>(R.id.text_view)
            textView.setText(data[p0])
            return view
        }
    }
}

<LinearLayout 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=".MainActivity">

    <AdapterViewFlipper
        android:id="@+id/activity_main_flipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:layout_marginBottom="300dp"
        android:layout_marginStart="100dp"
        android:layout_marginEnd="100dp">

    </AdapterViewFlipper>


</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_view"
        android:text="hello"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:height="100dp"
        android:width="100dp"
        android:background="@color/teal_200"/>

</LinearLayout>

Solution

  • When you first create the activity, you're searching for R.id.textView inside your AdapterViewFlipper, and setting the click listener on that. But that TextView is created when getView in your adapter inflates the flipper_view layout.

    When you flip to another view, getView inflates another copy of the layout, and that instance of textView doesn't have the click listener set on it, because it's a new view. I don't know why clicking would work more than once, but that's what I'd look into.

    Try setting the click listener in getView instead - you'll need to make adapterViewFlipper visible to it (or call a showNext function in the activity that flips it, something like that)