如何在最新 Android 设计支持库中将 Tab 添加到 TabLayout
How to add Tabs to the TabLayout in latest Android Design Support Library
我是 android 开发的新手。我按照教程 http://slidenerd.com/2015/07/22/android-design-support-library/ 将相同的片段添加到 tablayout.But 我需要将 3 个独特的片段添加到从网络下载数据的 tablayout server.How 来进行编程????
这是代码
`
包裹 com.example.naveen.helloworld;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class AppMainScreen extends AppCompatActivity {
private Toolbar mToolbar;
private TabLayout mTablayout;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_main_screen2);
mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
mTablayout = (TabLayout) findViewById(R.id.tab_layout);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
mTablayout.setupWithViewPager(mViewPager);
}
class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0: return new Fragment();
case 1: return new Fragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "News Feed";
case 1:
return "Events";
default:
return "News Feed";
} } }}`
提前致谢
pager 适配器中的 getItem
方法设置了片段,所以只需将 switch case 修改为
switch(position){
case 0:
return new NewsFragment();
case 1:
return new EventsFragment();
}
将NewsFragment
和EventsFragment
替换为您需要显示的相应片段
我是 android 开发的新手。我按照教程 http://slidenerd.com/2015/07/22/android-design-support-library/ 将相同的片段添加到 tablayout.But 我需要将 3 个独特的片段添加到从网络下载数据的 tablayout server.How 来进行编程???? 这是代码 ` 包裹 com.example.naveen.helloworld;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class AppMainScreen extends AppCompatActivity {
private Toolbar mToolbar;
private TabLayout mTablayout;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_main_screen2);
mToolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
mTablayout = (TabLayout) findViewById(R.id.tab_layout);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
mTablayout.setupWithViewPager(mViewPager);
}
class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0: return new Fragment();
case 1: return new Fragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "News Feed";
case 1:
return "Events";
default:
return "News Feed";
} } }}`
提前致谢
pager 适配器中的 getItem
方法设置了片段,所以只需将 switch case 修改为
switch(position){
case 0:
return new NewsFragment();
case 1:
return new EventsFragment();
}
将NewsFragment
和EventsFragment
替换为您需要显示的相应片段