使用viewpager为android片段定制标题
我想为每个片段添加自定义标题,它位于使用viewpager为android片段定制标题,android,android-fragments,android-viewpager,swipe,custom-titlebar,Android,Android Fragments,Android Viewpager,Swipe,Custom Titlebar,我想为每个片段添加自定义标题,它位于viewPager中。我该如何实施。在下面的代码中,我需要为FragmentMain、FragmentMore和FragmentChatView添加自定义标题 提前谢谢 public class HomeActivity extends FragmentActivity { // page adapter between fragment list and view pager public static PagerAdapter mPager
viewPager
中。我该如何实施。在下面的代码中,我需要为FragmentMain
、FragmentMore
和FragmentChatView
添加自定义标题
提前谢谢
public class HomeActivity extends FragmentActivity {
// page adapter between fragment list and view pager
public static PagerAdapter mPagerAdapter;
// view pager
public ViewPager mPager;
// activity data
public String p2text, p3text;
public static List<Fragment> fragments;// = buildFragments();
// / ArrayList<String> categories = {"1","2","3","4","5","6","7","8"};
ArrayList<String> categories = new ArrayList<String>();
static final String LOG_TAG = "HomeActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragments = new ArrayList<android.support.v4.app.Fragment>();
categories.add("1");
categories.add("2");
categories.add("3");
categories.add("4");
categories.add("5");
categories.add("6");
categories.add("7");
addFragments(FragmentMore.class.getName(), 1);
addFragments(FragmentMain.class.getName(), 2);
addFragments(FragmentChatView.class.getName(), 3);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPagerAdapter = new PagerAdapter(this, getSupportFragmentManager(), fragments, categories);
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(1);
}
public void addFragments(String className, int position) {
// List<android.support.v4.app.Fragment> fragments = new
// ArrayList<android.support.v4.app.Fragment>();
// (int i = 0; i<categories.size(); i++) {
Bundle b = new Bundle();
b.putInt("position", position);
fragments.add(Fragment.instantiate(this, className, b));
// }
// return fragments;
}
public void removeFragments(String className, int position) {
// List<android.support.v4.app.Fragment> fragments = new
// ArrayList<android.support.v4.app.Fragment>();
// (int i = 0; i<categories.size(); i++) {
Bundle b = new Bundle();
b.putInt("position", position);
fragments.remove(Fragment.instantiate(this, className, b));
// }
// return fragments;
}
private List<android.support.v4.app.Fragment> buildFragments() {
List<android.support.v4.app.Fragment> fragments = new ArrayList<android.support.v4.app.Fragment>();
for (int i = 0; i < categories.size(); i++) {
Bundle b = new Bundle();
b.putInt("position", i);
fragments.add(Fragment.instantiate(this, FragmentSearch.class.getName(), b));
}
return fragments;
}
@Override
public void onResume() {
super.onResume();
Log.e(LOG_TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.e(LOG_TAG, "onPause");
}
}
public class FragmentMore extends Fragment {
Button btnWrite;
public String ptext="More";
static final String LOG_TAG = "Fragment More";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
// onCreateView :
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.more,container,false);
return view;
}
@Override
public void onResume() {
super.onResume();
Log.e(LOG_TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.e(LOG_TAG, "onPause");
}
}
公共类HomeActivity扩展了FragmentActivity{
//片段列表和视图寻呼机之间的页面适配器
公共静态页面雷达;
//查看寻呼机
公众阅览器;
//活动数据
公共字符串p2text,p3text;
公共静态列表片段;//=buildFragments();
///ArrayList类别={“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”};
ArrayList categories=新的ArrayList();
静态最终字符串日志\u TAG=“HomeActivity”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
片段=新的ArrayList();
类别。添加(“1”);
类别。添加(“2”);
类别。添加(“3”);
类别。添加(“4”);
类别。添加(“5”);
类别。添加(“6”);
类别。添加(“7”);
addFragments(FragmentMore.class.getName(),1);
addFragments(FragmentMain.class.getName(),2);
addFragments(FragmentChatView.class.getName(),3);
mPager=(ViewPager)super.findViewById(R.id.pager);
mPagerAdapter=new PagerAdapter(这是getSupportFragmentManager(),片段,类别);
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(1);
}
公共void addFragments(字符串类名称,int位置){
//列表片段=新建
//ArrayList();
//(inti=0;i我想您正在寻找PagerTitleStrip元素
查看android教程的这一部分发布更多版面。我发布了更多版面和自定义标题版面。我想在我的fragmentMore中添加自定义标题。我需要一个自定义标题,该标题包含带有imageview的textview。是否可以在PagerTitleStrip中使用。?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/screen_back_color"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<Button
android:id="@+id/btnSettings"
android:layout_width="@dimen/more_button_width"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/more_button_margin"
android:layout_marginTop="@dimen/more_button_margin"
android:background="@drawable/button_selector_grey"
android:text="@string/settings"
android:textAllCaps="true"
android:textSize="@dimen/more_button_font_size"
android:textColor="@color/white" >
</Button>
<Button
android:id="@+id/btnTermsAndConditions"
android:layout_width="@dimen/more_button_width"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/more_button_margin"
android:layout_marginTop="@dimen/more_button_margin"
android:background="@drawable/button_selector_grey"
android:text="@string/termsandCondition"
android:textAllCaps="true"
android:textColor="@color/white" >
</Button>
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="fill_horizontal"
android:background="@color/black"
android:orientation="horizontal" >
<TextView
android:id="@+id/header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="@dimen/main_edittext_fontsize"
android:contentDescription="@string/desc"
android:gravity="center"
android:textColor="@color/light_blue"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:text="title" />
</RelativeLayout>