gpt4 book ai didi

java - 单击选项卡布局项目时选项卡未更改

转载 作者:行者123 更新时间:2023-12-02 09:21:35 26 4
gpt4 key购买 nike

单击选项卡布局(选项卡按钮)时 fragment (选项卡)不会更改

我尝试过以下代码

MainActivity.java

package com.kunal.flightstatus;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.net.Uri;
import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TabLayout tabLayout = findViewById(R.id.tablayout);
tabLayout.addTab(tabLayout.newTab().setText("Home"));
tabLayout.addTab(tabLayout.newTab().setText("Favourite"));
tabLayout.addTab(tabLayout.newTab().setText("Search"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = findViewById(R.id.viewpager);
PageAdapter adapter = new PageAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);

}

}

PagerAdapter.java

package com.kunal.flightstatus;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;


public class PageAdapter extends FragmentPagerAdapter {


public PageAdapter(@NonNull FragmentManager fm) {
super(fm);
}

@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0 :
Tab1 tab1 = new Tab1();
position +=1;
Bundle bundle = new Bundle();
bundle.putString("message","fragment :"+position);
tab1.setArguments(bundle);
return tab1;
case 1 :
Tab2 tab2 = new Tab2();
position +=1;
Bundle bundl = new Bundle();
bundl.putString("message","fragment :"+position);
tab2.setArguments(bundl);
return tab2;
case 2 :
Tab3 tab3 = new Tab3();
position +=1;
Bundle bundlee = new Bundle();
bundlee.putString("message","fragment :"+position);
tab3.setArguments(bundlee);
return tab3;
default:
return null;
}
}

@Override
public int getCount() {
return 3;
}
}

activity_main.xml

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

<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tablayout"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

</com.google.android.material.tabs.TabLayout>

<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/viewpager">

</androidx.viewpager.widget.ViewPager>

</LinearLayout>

选项卡( fragment )应在单击选项卡布局项目时更改

最佳答案

需要链接 ViewPager 和 TabLayout 才能进行选项卡切换。

tabLayout.setupWithViewPager(viewPager);

应该可以解决问题。

这样 TabLayout 将连接到 ViewPager 并自动填充。在 FragmentPagerAdapter 中实现 CharSequence getPageTitle (intposition) 来填充标题,而不是手动调用 tabLayout.addTab():

@NonNull
private List<String> titles = Arrays.asList("Home", "Favourite", "Search");

@Override
public CharSequence getPageTitle (int position) {
return titles.get(position);
}

整个设置如下所示:

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

<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tablayout"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabGravity="fill" />

<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/viewpager" />

</LinearLayout>

Activity :

package com.kunal.flightstatus;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ViewPager viewPager = findViewById(R.id.viewpager);
PageAdapter adapter = new PageAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);

TabLayout tabLayout = findViewById(R.id.tablayout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
}

}

寻呼机适配器:

package com.kunal.flightstatus;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.Arrays;
import java.util.List;


public class PageAdapter extends FragmentPagerAdapter {
@NonNull
private List<String> titles = Arrays.asList("Home", "Favourite", "Search");

public PageAdapter(@NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}

@Override
public CharSequence getPageTitle (int position) {
return titles.get(position);
}

@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0 :
Tab1 tab1 = new Tab1();
position +=1;
Bundle bundle = new Bundle();
bundle.putString("message","fragment :"+position);
tab1.setArguments(bundle);
return tab1;
case 1 :
Tab2 tab2 = new Tab2();
position +=1;
Bundle bundl = new Bundle();
bundl.putString("message","fragment :"+position);
tab2.setArguments(bundl);
return tab2;
case 2 :
Tab3 tab3 = new Tab3();
position +=1;
Bundle bundlee = new Bundle();
bundlee.putString("message","fragment :"+position);
tab3.setArguments(bundlee);
return tab3;
default:
throw new IllegalStateException();
}
}

@Override
public int getCount() {
return 3;
}
}

关于java - 单击选项卡布局项目时选项卡未更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58660682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com