Fragment碎片片段(ViewPage+Fragment实现大致微信的界面滑动)

Fragment用途:

可以把两个Activity同是放到一个Activity里面。

今天来分享下用Fragment和ViewPage大致实现一个微信的界面滑动

首先要写五个界面

第一个是界面ViewPage

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.jane0615_test.MainActivity">
   <android.support.v4.view.ViewPager
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:id="@+id/vp">
   </android.support.v4.view.ViewPager>
 <RadioGroup
     android:orientation="horizontal"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/rg">
     <RadioButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="微信"
         android:drawableTop="@drawable/selector_1"
     android:button="@null"
     android:id="@+id/rb_1"/>
     <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="通讯录"
         android:drawableTop="@drawable/selector_1"
         android:button="@null"
         android:id="@+id/rb_2"/>
     <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="发现"
         android:drawableTop="@drawable/selector_1"
         android:button="@null"
         android:id="@+id/rb_3"/>
     <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="我"
         android:drawableTop="@drawable/selector_1"
         android:button="@null"
         android:id="@+id/rb_4"/>

 </RadioGroup>


</LinearLayout>
另外是四个分别是仿照微信里的三个界面微信、通讯录、发现、我

<?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="微信"
    android:background="#FFBBD3F4"/>
</LinearLayout>

<?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="通讯录"
    android:background="#33ff00ff"/>

</LinearLayout>

<?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="发现"
        android:background="#66ffff00"/>
</LinearLayout>

<?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="我"
    android:background="#66ff0000"/>
</LinearLayout>


再写个selector文件,传两张图片

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="false" android:drawable="@drawable/angle_end"></item>
    <item android:state_checked="true" android:drawable="@drawable/angle_start"></item>
</selector>

上面都是界面,

package com.example.jane0615_test;

import  android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15 0015.
 */

public class WeiXinFragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_weixin,null);
    }
}



package com.example.jane0615_test;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15 0015.
 */

public class ContactFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contact,null);
    }
}

package com.example.jane0615_test;

import  android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15 0015.
 */

public class FindFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find,null);
    }
}

package com.example.jane0615_test;

import  android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/15 0015.
 */

public class MyFragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my,null);
    }
}


package com.example.jane0615_test;

import android.support.annotation.IdRes;
import  android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Fragment> list=new ArrayList<>();
    private ViewPager vp;
    private int[] images={R.id.rb_1,R.id.rb_2,R.id.rb_3,R.id.rb_4};
    private RadioGroup rg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
        WeiXinFragment weiXinFragment=new WeiXinFragment();
        ContactFragment contactFragment=new ContactFragment();
        FindFragment findFragment=new FindFragment();
        MyFragment myFragment=new MyFragment();
        list.add(weiXinFragment);
        list.add(contactFragment);
        list.add(findFragment);
        list.add(myFragment);
        vp.setCurrentItem(0);
        rg.check(images[0]);
//        rg.setOnCheck;


        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            if(state==2){
                rg.check(images[vp.getCurrentItem()]);
            }
            }
        });

        vp.setAdapter(new My(getSupportFragmentManager()));
        }
        class My extends FragmentPagerAdapter{

            public My(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                return null;
            }

            @Override
            public int getCount() {
                return 0;
            }
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值