21 PagerTabStrip-PagerTitleStrip-viewPager

  • 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<Fragment> list = new ArrayList<Fragment>();

    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<Map<String,Object>> loadNetWorkData(){

        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for(int i=0;i<20;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("icon", R.drawable.ic_launcher);
            map.put("title", "郭XX大战曹XXX"+i+"tab"+index);
            map.put("content", "降龙十八掌赢"+i+"tab"+index);
            list.add(map);

        }

        return list;

    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
    }

}

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.PagerTabStrip
            android:id="@+id/pagertabStrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

        </android.support.v4.view.PagerTabStrip> -->
        <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.PagerTabStrip
            android:id="@+id/pagertabStrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

        </android.support.v4.view.PagerTabStrip> -->
        <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

<?xml version="1.0" encoding="utf-8"?>
<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>

posted on 2016-09-20 19:25  木鱼哥  阅读(221)  评论(0编辑  收藏  举报

导航