- PagerTabStrip:可以点击跳转到对应viewPager界面
- PagerTitleStrip:不可点击
在eclipse开发时如果目标版本为API23那么会有不显示的问题
- 解决:更换v4包
解决: 在Activiry的oncreate方法中添加如下代码
((ViewPager.LayoutParams) tabStrip.getLayoutParams()).isDecor = true;
(我试了下我的不行但网上有说可以)
- studio开发貌似可以
MainActivity.java
package com.qf.day21_fragmentpagertabstrip_demo2;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private PagerTabStrip pagerTabStrip;
private PagerTitleStrip pagerTitleStrip;
private ViewPager viewPager;
private List list = new ArrayList();
private String[] titles ={"新闻","娱乐","军事","体育"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
//pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagertabStrip);
initData();
//setPagertabStrip();
viewPager.setAdapter(new MyFragmentpagerAdapter(getSupportFragmentManager()));
}
/**
* 设置书签
*/
public void setPagertabStrip(){
//设置文本颜色
pagerTabStrip.setTextColor(Color.RED);
//设置指示线颜色
pagerTabStrip.setTabIndicatorColor(Color.BLUE);
//设置背景颜色
pagerTabStrip.setBackgroundColor(Color.GREEN);
//设置是否完整绘制底线
pagerTabStrip.setDrawFullUnderline(false);
}
//初始化数据源
public void initData(){
for(int i=0;i<4;i++){
MyFragment myFragment = MyFragment.getInstance(i+1);
list.add(myFragment);
}
}
public class MyFragmentpagerAdapter extends FragmentPagerAdapter{
public MyFragmentpagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
//设置书签内容
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titles[position];
}
}
}
MyFragment.java
package com.qf.day21_fragmentpagertabstrip_demo2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MyFragment extends ListFragment {
private TextView tvShow;
private int index =0;
public static MyFragment getInstance(int index){
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
myFragment.setArguments(args);
return myFragment;
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Bundle bundle = getArguments();
if(bundle!=null){
index = bundle.getInt("index");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_layout, container, false);
tvShow = (TextView) v.findViewById(R.id.tv_show);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
switch (index) {
case 1:
tvShow.setText("您点击了书签1");
break;
case 2:
tvShow.setText("您点击了书签2");
break;
case 3:
tvShow.setText("您点击了书签3");
break;
case 4:
tvShow.setText("您点击了书签4");
break;
default:
break;
}
SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
loadNetWorkData(),
R.layout.item,
new String[]{"icon","title","content"},
new int[]{R.id.iv_item,R.id.title_item,R.id.content_item});
setListAdapter(adapter);
}
/**
* 假设从网络获取数据
* @return
*/
private List
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagerTitleStrip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
android.support.v4.view.PagerTitleStrip>
android.support.v4.view.ViewPager>
RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagerTitleStrip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
android.support.v4.view.PagerTitleStrip>
android.support.v4.view.ViewPager>
RelativeLayout>
item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/title_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_item"
android:text="name"
/>
<TextView
android:id="@+id/content_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_item"
android:text="aaa"
android:layout_alignBottom="@id/iv_item"
/>
RelativeLayout>