tablayout布局之再探究

来源:互联网 发布:什么叫网络直播 编辑:程序博客网 时间:2024/06/03 18:13

一.目的:tablayout是最起初的布局,很基础,但很重要。有必要讨论。

二.问题:怎么实现一个tablayout布局,例如:电话簿窗口,教务系统等等

三.实现:依次的实现逻辑。

四.总体思想:tablayout+viewpager+fragment+adapter

(1)在主布局laouut_main(依据个人)加入tablayout布局。

<android.support.design.widget.TabLayout

(2)试想一下,你滚动窗口是否有界面跟你一起流浪,称之为viewpager。然而我们看到的不仅仅是空白页。我们需要添加一定的内容,方便辨别。那里面的内容怎么得来的呢。

想必大家都知道,viewpager自己设置自己的视图view。

这里我们设置了四个fragment,也就是四个页面。列举其中的一个如下(需要重写方法oncreateview),false变量的含义是取消了父布局,用自己构建的:

public class fragment1 extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View v=inflater.inflate(R.layout.layout_fragment1,container,false);        return v;    
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"><TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:text="1"/></LinearLayout>

上面也是其中的布局文件。主活动整体这样:

package com.example.jf.tablayouttest;import android.graphics.PorterDuff;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.TableLayout;import android.widget.TextView;import com.example.jf.tablayouttest.fragment.fragment1;import com.example.jf.tablayouttest.fragment.fragment2;import com.example.jf.tablayouttest.fragment.fragment3;import com.example.jf.tablayouttest.fragment.fragment4;import java.util.ArrayList;import java.util.List;import java.util.zip.Inflater;public class MainActivity extends AppCompatActivity {    List<Fragment> fragments;    ViewPager vp;    TabLayout tab;    ArrayList<TabItem>items;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tab = (TabLayout) findViewById(R.id.tab);        vp = (ViewPager) findViewById(R.id.vp);        //必须初始化fragments的所有值。        fragments = new ArrayList<>();        fragment1 f1 = new fragment1();        fragment2 f2 = new fragment2();        fragment3 f3 = new fragment3();        fragment4 f4 = new fragment4();        fragments.add(f1);        fragments.add(f2);        fragments.add(f3);        fragments.add(f4);        items=new ArrayList<>();        items.add(new TabItem("精彩评论0",f1));        items.add(new TabItem("精彩评论1",f2));        items.add(new TabItem("精彩评论2",f3));        items.add(new TabItem("精彩评论3",f4));       MyAdapter myadpter=new MyAdapter(getSupportFragmentManager());       vp.setAdapter(myadpter);        tab.setupWithViewPager(vp);    }    class MyAdapter extends FragmentPagerAdapter {        public MyAdapter(FragmentManager fm) {            super(fm);        }        @Override        public int getCount() {            return items.size();        }        @Override        public Fragment getItem(int position) {            return items.get(position).getF();        }        @Override        public CharSequence getPageTitle(int position) {            return items.get(position).getTitle();        }    }    class TabItem {        String title;        Fragment f;        public TabItem(String title, Fragment f) {            this.title = title;            this.f = f;        }        public String getTitle() {            return title;        }        public void setTitle(String title) {            this.title = title;        }        public Fragment getF() {            return f;        }        public void setF(Fragment f) {            this.f = f;        }    }

原创粉丝点击