android studio 怎样写fragment+tabhost
1个回答
展开全部
tabhost这个控件谷歌已经弃用了,多学无益。现在都流行tablayout+fragment+viewpager.
先看安卓无忧中对这个的demo,如果想看源码,请百度安卓无忧,点击源码就可以看。
fragment:
package mm.shandong.com.testtablayout.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by 安卓无忧 on 2016/8/11.
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> list_fragment; //fragment列表
private List<String> list_Title; //tab名的列表
public MyFragmentPagerAdapter(FragmentManager fm,
List<Fragment> list_fragment, List<String> list_Title) {
super(fm);
this.list_fragment = list_fragment;
this.list_Title = list_Title;
}
@Override
public Fragment getItem(int position) {
return list_fragment.get(position);
}
@Override
public int getCount() {
return list_fragment.size();
}
@Override
public CharSequence getPageTitle(int position) {
return list_Title.get(position);
}
}
activity:
package mm.shandong.com.testtablayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import mm.shandong.com.testtablayout.adapter.MyFragmentPagerAdapter;
import mm.shandong.com.testtablayout.fragment.HomeFragment;
import mm.shandong.com.testtablayout.fragment.SearchFragment;
import mm.shandong.com.testtablayout.fragment.TuanGouFragment;
import mm.shandong.com.testtablayout.fragment.UserFragment;
public class TestTabLayoutActivity extends AppCompatActivity {
ViewPager viewPager;
TabLayout tabLayout;
List<Fragment> list_fragment;
List<String> list_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_tab_layout);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
list_fragment = new ArrayList<Fragment>();
list_title = new ArrayList<String>();
initControls();
}
///初始化
private void initControls() {
//创建Fragement
HomeFragment homeFragment = new HomeFragment();
SearchFragment searchFragment = new SearchFragment();
TuanGouFragment tuanGouFragment = new TuanGouFragment();
UserFragment userFragment = new UserFragment();
//将fragment装进列表中
list_fragment = new ArrayList<>();
list_fragment.add(homeFragment);
list_fragment.add(searchFragment);
list_fragment.add(tuanGouFragment);
list_fragment.add(userFragment);
//将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用
list_title = new ArrayList<>();
list_title.add("网站主页");
list_title.add("搜索周边");
list_title.add("我的团购");
list_title.add("用户管理");
//设置TabLayout的模式
tabLayout.setTabMode(TabLayout.MODE_FIXED);
//为TabLayout添加tab名称
tabLayout.addTab(tabLayout.newTab().setText(list_title.get(0)));
tabLayout.addTab(tabLayout.newTab().setText(list_title.get(1)));
tabLayout.addTab(tabLayout.newTab().setText(list_title.get(2)));
tabLayout.addTab(tabLayout.newTab().setText(list_title.get(3)));
//创建ViewPager的FragmentPagerAdapter
MyFragmentPagerAdapter myFragmentPagerAdapter;
myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.
getSupportFragmentManager(), list_fragment, list_title);
//viewpager加载adapter
viewPager.setAdapter(myFragmentPagerAdapter);
//tabLayout与viewPager建立联系
tabLayout.setupWithViewPager(viewPager);
//tab_FindFragment_title.set
}
}
ps:为了方便交流看一下我名字中文和除了中文以外的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询