美文网首页
ViewPager+FragmentPageAdapter实现可

ViewPager+FragmentPageAdapter实现可

作者: A_Coder | 来源:发表于2016-11-08 20:14 被阅读0次

    首先创建首页布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#C3C3C3"
        tools:context="news.huoren.com.themap.MainActivity">
    
        <include layout="@layout/top"/>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/id_viewpager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            ></android.support.v4.view.ViewPager>
    
        <include layout="@layout/bottom" />
    </LinearLayout>
    

    底部栏布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:background="@drawable/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="55dp">
    
        <LinearLayout
            android:id="@+id/id_tab_weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageButton
                android:id="@+id/id_tab_weixin_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#000"
                android:clickable="false"
                android:src="@mipmap/tab_weixin_pressed"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="微信"
                android:textColor="#ffffff"/>
    
        </LinearLayout>
        <LinearLayout
            android:id="@+id/id_tab_frd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageButton
                android:id="@+id/id_tab_frd_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#000"
                android:clickable="false"
                android:src="@mipmap/tab_find_frd_normal"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="朋友"
                android:textColor="#ffffff"/>
    
        </LinearLayout>
        <LinearLayout
            android:id="@+id/id_tab_address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageButton
                android:id="@+id/id_tab_address_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#000"
                android:clickable="false"
                android:src="@mipmap/tab_address_normal"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="通讯录"
                android:textColor="#ffffff"/>
    
        </LinearLayout>
        <LinearLayout
            android:id="@+id/id_tab_setting"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageButton
                android:id="@+id/id_tab_setting_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#000"
                android:clickable="false"
                android:src="@mipmap/tab_settings_normal"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="微信"
                android:textColor="#ffffff"/>
    
        </LinearLayout>
    
    </LinearLayout>
    

    主界面的Activity:

    package news.huoren.com.themap;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.LinearLayout;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        private ViewPager viewPager;
        private FragmentPagerAdapter adapter;
        private List<Fragment> fragmentList;
    
        private LinearLayout mTabWeixin;
        private LinearLayout mTabFrd;
        private LinearLayout mTabAddress;
        private LinearLayout mTabSetting;
    
        private ImageButton mImgFrd;
        private ImageButton mImgAddress;
        private ImageButton mImgSetting;
        private ImageButton mImgWeixin;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
            initEvent();
        }
    
        private void initEvent() {
            mTabWeixin.setOnClickListener(this);
            mTabFrd.setOnClickListener(this);
            mTabAddress.setOnClickListener(this);
            mTabSetting.setOnClickListener(this);
        }
    
        private void initView() {
            viewPager = (ViewPager) findViewById(R.id.id_viewpager);
    
            mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
            mTabAddress = (LinearLayout) findViewById(R.id.id_tab_address);
            mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
            mTabSetting = (LinearLayout) findViewById(R.id.id_tab_setting);
    
            mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);
            mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
            mImgAddress = (ImageButton) findViewById(R.id.id_tab_address_img);
            mImgSetting = (ImageButton) findViewById(R.id.id_tab_setting_img);
    
            fragmentList = new ArrayList<Fragment>();
            Fragment mtab1 = new WeixinFragment();
            Fragment mtab2 = new FrdFragment();
            Fragment mtab3 = new AddressFragment();
            Fragment mtab4 = new SettingFragment();
            fragmentList.add(mtab1);
            fragmentList.add(mtab2);
            fragmentList.add(mtab3);
            fragmentList.add(mtab4);
            adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int position) {
                    return fragmentList.get(position);
                }
    
                @Override
                public int getCount() {
                    return fragmentList.size();
                }
            };
            viewPager.setAdapter(adapter);
            //设置viewpager的page切换监听
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
                //同步底部栏tab切换page
                @Override
                public void onPageSelected(int position) {
                    int currentViewpage = viewPager.getCurrentItem();
                    setTab(currentViewpage);
    
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
        }
    
        //设置点击的tab切换图标
        private void setTab(int currentViewpage) {
            resetImg();
            switch (currentViewpage) {
                case 0:
                    mImgWeixin.setImageResource(R.mipmap.tab_weixin_pressed);
                    break;
                case 1:
                    mImgFrd.setImageResource(R.mipmap.tab_find_frd_pressed);
                    break;
                case 2:
                    mImgAddress.setImageResource(R.mipmap.tab_address_pressed);
                    break;
                case 3:
                    mImgSetting.setImageResource(R.mipmap.tab_settings_pressed);
                    break;
            }
    
        }
        //初始化底部tab
        private void resetImg() {
            mImgWeixin.setImageResource(R.mipmap.tab_weixin_normal);
            mImgFrd.setImageResource(R.mipmap.tab_find_frd_normal);
            mImgAddress.setImageResource(R.mipmap.tab_address_normal);
            mImgSetting.setImageResource(R.mipmap.tab_settings_normal);
        }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.id_tab_weixin:
                    select(0);
                    break;
                case R.id.id_tab_frd:
                    select(1);
                    break;
                case R.id.id_tab_address:
                    select(2);
                    break;
                case R.id.id_tab_setting:
                    select(3);
                    break;
            }
        }
        //点击设置相应的图标和viewPager
        private void select(int i) {
            setTab(i);
            viewPager.setCurrentItem(i);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:ViewPager+FragmentPageAdapter实现可

          本文链接:https://www.haomeiwen.com/subject/omxzuttx.html