gpt4 book ai didi

Android - 未创建选项卡项

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

我正在学习如何进行选项卡布局的教程。我不知道我在下面的代码中做了什么,它不输出标签。当应用程序运行时,我看不到选项卡项。其他一切正常。

Activity

public class dashboard extends AppCompatActivity {

TabLayout tabs;
ViewPager container;

private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;

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

// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(container);
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dashboard, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private String fragments [] = {"Completed", "In Progress"};
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new dashboard_completed();
case 1:
return new dashboard_in_progress();
default:
return null;
}
}

@Override
public int getCount() {
return fragments.length;
}

@Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}

dashboard_in_progress

public class dashboard_in_progress extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dashboard_in_progress, container, false);
}
}

dashboard_completed

public class dashboard_completed extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dashboard_completed, container, false);
}
}

XML

    <android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabs">

</android.support.design.widget.TabLayout>

<view
android:layout_height="match_parent"
class="android.support.v4.view.ViewPager"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_marginTop="48dp"/>
</android.support.design.widget.CoordinatorLayout>

最佳答案

这是你的问题。

ViewPager container;
...

private ViewPager mViewPager;
...

tabs.setupWithViewPager(container);

您声明了两个 ViewPager,但您只初始化了一个,并尝试使用空值设置 TabLayout

删除 ViewPager container; 声明,并将设置调用更改为:

tabs.setupWithViewPager(mViewPager);

此外,您不需要自己在 TabLayout 上设置 OnTabSelectedListenersetupWithViewPager() 方法将为您处理。

关于Android - 未创建选项卡项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40560452/

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