CristiAlex92
Lurker
Hi,
I'm trying to build a hello world app with tabs.
The problem I'm encountering now is that I get some errors, using adapter on ViewPager2.
I updated from ViewPager to ViewPager2.
Here's the code:
app > java > MainActivity.java
app > res > layout > activity_main.xml
Dependencies I have:
What's wrong?
Thanks
I'm trying to build a hello world app with tabs.
The problem I'm encountering now is that I get some errors, using adapter on ViewPager2.
I updated from ViewPager to ViewPager2.
Here's the code:
app > java > MainActivity.java
Java:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPagerAdapter adapter;
private TabLayout tabLayout;
private Spinner spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this creates the three-dot button in the right on the toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
//uses toolbar as action bar
setSupportActionBar(toolbar);
ViewPager2 viewPager2 = findViewById(R.id.pager);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager2.setAdapter(adapter); // <- adapter in this line is the problem (error: incompatible types: ViewPagerAdapter cannot be converted to Adapter)
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager2); // <- and this variable: viewPager2 (error: incompatible types: ViewPager2 cannot be converted to ViewPager)
}
app > res > layout > activity_main.xml
Code:
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Dependencies I have:
What's wrong?
Thanks