gpt4 book ai didi

android - 在 Android 的 Fragment Java 中获取选项卡的文本/标题

转载 作者:行者123 更新时间:2023-11-30 01:15:12 25 4
gpt4 key购买 nike

如何在 TopFreeFragment.java 中获取 Tab 的文本/标题?

我想知道选择了哪个选项卡并相应地加载 webview。我该怎么做?

我尝试过使用 sharedPreferences,但这不是正确的方法,而且也无法正常工作。

这里我想在 TopFreeFragment.java 中显示选项卡标题的 Toast

public class YouTube_Toolbar extends AppCompatActivity {

Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.you_tube__toolbar);

toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);

tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);

viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new TopFreeFragment(), "TUESDAY");
viewPagerAdapter.addFragments(new TopFreeFragment(), "WEDNESDAY");
viewPagerAdapter.addFragments(new TopFreeFragment(), "THURSDAY");
viewPagerAdapter.addFragments(new TopFreeFragment(), "FRIDAY");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

 public class TopFreeFragment extends Fragment {

WebView webView;
Button download;

public TopFreeFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)

return inflater.inflate(R.layout.fragment_top_free, container, false);
}
}

最佳答案

我会在您的 Fragment 中编写一个 newInstance() 方法和一个 getTitle() 方法,并通过 Bundle 传递名称。

像这样:

TopFreeFragment.java

public class TopFreeFragment extends Fragment {
WebView webView;
Button download;

public TopFreeFragment() {
// Required empty public constructor
}

public static TopFreeFragment newInstance(String title) {
TopFreeFragment fragment = new TopFreeFragment();

Bundle args = new Bundle();
args.putCharSequence("title", title);
fragment.setArguments(args);

return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)

return inflater.inflate(R.layout.fragment_top_free, container, false);
}

public String getTitle() {
Bundle args = getArguments();
return args.getCharSequence("title", "NO TITLE FOUND");
}
}

然后像这样将 fragment 添加到您的viewPagerAdapter:

YouTube_Toolbar.java

public class YouTube_Toolbar extends AppCompatActivity {

Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;

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

toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);

tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);

viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(TopFreeFragment.newInstance("TUESDAY"), "TUESDAY");
viewPagerAdapter.addFragments(TopFreeFragment.newInstance("WEDNESDAY"), "WEDNESDAY");
viewPagerAdapter.addFragments(TopFreeFragment.newInstance("THURSDAY"), "THURSDAY");
viewPagerAdapter.addFragments(TopFreeFragment.newInstance("FRIDAY"), "FRIDAY");
viewPager.setAdapter(viewPagerAdapter);

tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

要从 fragment 中获取 fragment 的标题,只需调用 getTitle()。您可以传递任何您想要的标题作为 newInstance(String title) 方法的参数。

关于android - 在 Android 的 Fragment Java 中获取选项卡的文本/标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37950156/

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