Android-Fragment(模微信界面)

本文介绍如何在Android应用中使用Fragment实现左右滑动效果,并通过底部按钮切换不同的Fragment。涉及四个Fragment的布局文件及MainActivity的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 代码实现fragment左右滑动改变按钮样式,点击按钮切换fragment




在android Studio 


四个布局文件 和actity


1.weixin_Fragment.xml

--------------------------------------------------------------------

<?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="wrap_content"
         android:background="@drawable/weixin"
         android:textSize="30dp"
         />
</LinearLayout>
activity
------------------------------------------
public class Fragment_WeiXin extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.weixin_fragment,null
        );
    }
}


2.contacts_fragment.xml
------------------------------------------
<?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="wrap_content"
         android:background="@drawable/contacts"
         />
</LinearLayout>

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




3.find_fragment.xml

------------------------------------------------------------------

<?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="wrap_content"
         android:background="@drawable/find"/>
</LinearLayout>


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



4.me_fragment
--------------------------------------
<?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="wrap_content"
         android:background="@drawable/me"/>
</LinearLayout>
activity
----------------------------------------
public class Fragment_Me extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.me_fragment,null);
    }
}


5.主界面(activity) 和布局文件


5.1 activity_main.xml

<?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.g160628_android_10_1_fragment.MainActivity"
    android:weightSum="1">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/vp_ViewPager_page"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

    <RadioGroup
        android:id="@+id/rg_radiogroup_radio"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:button="@null"
            android:drawableTop="@drawable/button_one"
            android:padding="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rb_RadioButton_one"
            android:text="微信"/>

        <RadioButton
            android:button="@null"
            android:drawableTop="@drawable/button_tow"
            android:padding="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rb_RadioButton_tow"
            android:text="通讯录"/>

        <RadioButton
            android:button="@null"
            android:drawableTop="@drawable/button_three"
            android:padding="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rb_RadioButton_three"
            android:text="发现"/>

        <RadioButton
            android:drawableTop="@drawable/button_four"
            android:button="@null"
            android:padding="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rb_RadioButton_four"
            android:text="我"/>
    </RadioGroup>
</LinearLayout>
5.2 MainActivity
----------------------------------------
package com.example.g160628_android_10_1_fragment;

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 ViewPager vp;
    private List<Fragment> list=new ArrayList<>();
    private RadioGroup rg;
    int data[]={R.id.rb_RadioButton_one,R.id.rb_RadioButton_tow,R.id.rb_RadioButton_three,R.id.rb_RadioButton_four};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp_ViewPager_page);
        Fragment_WeiXin fragment_weiXin=new Fragment_WeiXin();
        Fragment_Contacts fragment_contacts=new Fragment_Contacts();
        Fragment_Find fragment_find=new Fragment_Find();
        Fragment_Me fragment_me=new Fragment_Me();
        list.add(fragment_weiXin);
        list.add(fragment_contacts);
        list.add(fragment_find);
        list.add(fragment_me);
        vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
        rg = (RadioGroup) findViewById(R.id.rg_radiogroup_radio);
        //第一次进来
        vp.setCurrentItem(0);
        rg.check(data[0]);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

                for (int i = 0; i < data.length; i++) {
                    if(checkedId==data[i]){
                        vp.setCurrentItem(i);
                        break;
                    }
                }
            }
        });

        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) {
//                state==1;
//                state==2;滑动完毕
//                state==0;
                 if(state==2){
                     rg.check(data[vp.getCurrentItem()]);
                 }
            }
        });


    }


    class MyAdapter extends FragmentPagerAdapter{

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

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}





在drawable里新建一个选择器(因为代码相同就只写一遍)

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值