Search code examples
androidandroid-viewpagerandroid-viewpager2

Simple example of ViewPager2 just does not work


I have a very simple ViewPager2, however it does not work. All I can see is a white screen. Can somebody help me with this one? I believe this is simplest basic example of ViewPager2, but it just do not work. Any help appreciated.

MyAdapter:

class MyAdapter(private val ctx: Context) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
    private val titles = arrayOf("ABC", "DEF", "GHCH","IJK", "LMN")

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view: View = LayoutInflater.from(ctx).inflate(R.layout.my_holder, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.text.text = titles[position]
    }

    override fun getItemCount(): Int {
        return titles.size
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var text: TextView

        init {
            text = itemView.findViewById(R.id.titles)
        }
    }
}

Holder:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/titles"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="25sp"
        android:textColor="@android:color/black"
        android:gravity="center"
        />

</LinearLayout>

Activity:

class TestActivity : AppCompatActivity() {

    private lateinit var binding: ActivityTestBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityTestBinding.inflate(layoutInflater)
        setContentView(R.layout.activity_themes)

        val viewPager2Adapter = MyAdapter(this)
        binding.viewpager.adapter = viewPager2Adapter
        binding.viewpager.registerOnPageChangeCallback(object : OnPageChangeCallback() {})
    }
}

Activity 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">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        />

</LinearLayout>

Solution

  • I think You have code right but there is activity binding error.

    private ActivityMainBinding binding;
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
    
    // Now you can access views directly using the binding object
    binding.textView.setText("Hello, View Binding!");
    

    Try this type of binding. You FORGOT to mention "binding.getRoot()" Please try above solution.