gpt4 book ai didi

android - 如何创建动态选项卡并为Android中的每个 fragment 添加不同的功能?

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

我知道如何创建静态标签,使用 volley 在 android 中获取数据并在每个 fragment 中工作,但是如何使用 JSON 创建动态标签并在每个 上存储不同的细节选项卡。就像第一个标签商店 "मुख्य समाचार" 来自不同网络服务的图像和详细信息。我所知道的是创建这样的标签:

public class TabActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;


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

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");

viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {

return mFragmentTitleList.get(position);
}
}
}

我想将 "cat_name" 存储为 TABS 名称。我该怎么做?我正在尝试但无法正确修复它如何获取数据并将其存储为选项卡名称以及为每个选项卡动态创建 fragment 并单独处理每个选项卡。

这是我获取标签名称的网络服务。

{
"status": 200,
"message": "Purchase order updated successfully",
"data": {
"page_title": "Home | Welcome",
"menu": [
{
"cat_id": "1",
"cat_name": "मुख्य समाचार",
"description": ""
},
{
"cat_id": "2",
"cat_name": "राजनीती ",
"description": ""
}

编辑

如何像这张图片中那样使用每个单独的选项卡: like in this image:

最佳答案

你可以这样做。

public class TabActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
// Fragment List
private final List<Fragment> mFragmentList = new ArrayList<>();
// Title List
private final List<String> mFragmentTitleList = new ArrayList<>();
private ViewPagerAdapter adapter;


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

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.container);
initTitle();

// edited , you can use multiple titles and one Fragment
for (int i = 0; i < mFragmentTitleList.size(); i++) {
mFragmentList.add(new OneFragment());
}

setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
// Tab ViewPager setting
viewPager.setOffscreenPageLimit(mFragmentList.size());
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabsFromPagerAdapter(adapter);
}

/**
* load title data ,it muest below ViewPagerAdapter initialize
*/
private void initTitle() {
String response = "{\n" +
" \"status\": 200,\n" +
" \"message\": \"Purchase order updated successfully\",\n" +
" \"data\": {\n" +
" \"page_title\": \"Home | Welcome\",\n" +
" \"menu\": [\n" +
" {\n" +
" \"cat_id\": \"1\",\n" +
" \"cat_name\": \"मुख्य समाचार\",\n" +
" \"description\": \"\" \n" +
" },\n" +
" {\n" +
" \"cat_id\": \"2\",\n" +
" \"cat_name\": \"राजनीती \",\n" +
" \"description\": \"\"\n" +
" }";

try {
JSONObject jsonObject = new JSONObject(response);
int status = jsonObject.getInt("status");
if (status == 200) {
JSONObject data = jsonObject.getJSONObject("data");
JSONArray menu = data.getJSONArray("menu");
for (int i = 0; i < menu.length(); i++) {
String cat_name = menu.getJSONObject(i).getString("cat_name");
mFragmentTitleList.add(cat_name);
}
} else {
for (int i = 0; i < mFragmentList.size(); i++) {
mFragmentTitleList.add("no title");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}

private void setupViewPager(ViewPager viewPager) {

adapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragmentList, mFragmentTitleList);

viewPager.setAdapter(adapter);
}

/**
* ViewPagerAdapter setting
*/
class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titleLists) {
super(fm);
this.mFragmentList = fragments;
this.mFragmentTitleList = titleLists;
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList == null ? 0 : mFragmentList.size();
}

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

希望能帮到你。

关于android - 如何创建动态选项卡并为Android中的每个 fragment 添加不同的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45457599/

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