360SDN.COM

tab界面实现方式之ViewPager+TabPageIndicator

来源:  2017-07-01 11:09:18    评论:0点击:

今天我来说说ViewPager+tabPageIndicator来实现tab界面。首先tabPageIndicator是在第三方的library包里面-- viewPagerlibrary,在githup上可以下载,我们需要将它导入eclipse中,然后在关联它。当我们使用这个包的时候,有一个问题需要注意:可能主工程中会报错,但是我们又不知道是哪里错了,最好看看libs下的v4包,一定要保证主工程和library中的v4包要么一模一样,要么只有一个,所以要么将其中一个v4包用另一个v4覆盖,或者直接删除一个包。

好的,现在直接贴代码

MainActivity布局文件代码

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.android_tab4.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include layout="@layout/top" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <com.viewpagerindicator.TabPageIndicator
            android:id="@+id/TabPageIndicator"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </com.viewpagerindicator.TabPageIndicator>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </LinearLayout>

</LinearLayout>
 

MainActivity代码

Java代码

package com.example.android_tab4;

import com.example.Util.Myadapter;
import com.viewpagerindicator.TabPageIndicator;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Window;

public class MainActivity extends FragmentActivity {
    private ViewPager viewpager = null;
    private TabPageIndicator tabPageIndicator = null;
    private Myadapter myadapter = null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView()
    {
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        tabPageIndicator = (TabPageIndicator) findViewById(R.id.TabPageIndicator);
        myadapter = new Myadapter(getSupportFragmentManager());
        viewpager.setAdapter(myadapter);
        tabPageIndicator.setViewPager(viewpager, 0);
    }
}
 

MyFragment代码

JAVA代码


package com.example.Util;

import com.example.android_tab4.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment{
    String string = null;
    public MyFragment(int position)
    {
        string = Myadapter.string[position];
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        
        TextView textview = (TextView) view.findViewById(R.id.textview);
        textview.setText(string);
        return view;
    }
}
 

MyFragment布局文件代码

xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        />

</LinearLayout>

 

MyAdapter代码

JAVA代码


package com.example.Util;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class Myadapter extends FragmentPagerAdapter{
    public  static String string[] = { "课程", "问答", "求课", "学习", "计划" };
    public Myadapter(FragmentManager fm) {
        super(fm);
    }

    public Fragment getItem(int arg0) {
        MyFragment myfragment = new MyFragment(arg0);
        return myfragment;
    }
    public int getCount() {
        return string.length;
    }
    public CharSequence getPageTitle(int position) {

        return string[position];
    }

}
 

top.xml代码

xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#07a7a5"
    android:orientation="horizontal" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/idx_logo"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="慕课网"
        android:gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:textColor="#ffffff"
        android:textSize="16sp"
        android:textStyle="bold"
        />
</LinearLayout>

 

 

 

 

 

为您推荐

友情链接 |九搜汽车网 |手机ok生活信息网|ok生活信息网|ok微生活
 Powered by www.360SDN.COM   京ICP备11022651号-4 © 2012-2016 版权