FragmentActivity_左右滑动的碎片

test1.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" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView 
            
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center"
            android:background="#4ADF2B"
            android:textColor="#ffffff"
           android:textSize="20dp"
            android:text="悦动"
            />
       
    </LinearLayout>
 <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
     <ImageView 
     
         android:layout_width="match_parent"
        android:background="@drawable/yuedong22"
         android:layout_height="match_parent"
         
         />
 </LinearLayout>
</LinearLayout>

 

 

Activity_text1.java要继承Fragment  跟上面test1.xml进行适配    

package com.example.speed_app;

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

public class Activity_text1 extends Fragment{
  @Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
      View view = inflater.inflate(R.layout.text1, container,false);
    return view;
}
}

 

上面的Activity_text1.java  和 test1.xml双双多个 这里就弄一个,我的项目弄了多个 因为都是一样的方法  

 

 上面准备好了

接下来才是我们要去实现的

 

caidan_dibu.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="horizontal" >
      <RadioGroup 
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:gravity="center">
         <RadioButton
             android:id="@+id/radioButton1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:button="@null"
             android:checked="true"
             android:drawableTop="@drawable/ledong"
             android:gravity="center"
             android:textColor="@drawable/color"
             android:text="悦动" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableTop="@drawable/tiaozhan"
        android:button="@null"
        android:textColor="@drawable/color"
        android:text="挑战" 
      
        android:gravity="center"
        />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:button="@null"
        android:drawableTop="@drawable/faxian"
        android:gravity="center"
        android:textColor="@drawable/color"
        android:text="发现" />
    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:button="@null"
        android:drawableTop="@drawable/quanz"
        android:text="圈子" 
        android:textColor="@drawable/color"
        android:gravity="center"
        />
    <RadioButton
       android:id="@+id/radioButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:button="@null"
         android:textColor="@drawable/color"
        android:drawableTop="@drawable/wo3"
        android:text="我" 
        android:gravity="center"
        />
    </RadioGroup>
</LinearLayout>

结果图:

 

 

 

 

home.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" >
   <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
    >
      
       <android.support.v4.view.ViewPager   //这个是一个v4包,这个包放在你要切换布局里面在下图的框里
          android:id="@+id/vp"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:flipInterval="30"
          android:persistentDrawingCache="animation" />
       </LinearLayout>
    <LinearLayout 
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
    <include layout="@layout/caidan_dibu"/>
   </LinearLayout> 
</LinearLayout>

 

 结果:

 

 

MyFragmentPagerAdapter.java 实现适配器

 

package com.example.speed_app;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
//FragmentPagerAdapter是Android-support-v4支持包里面出现的一个新的适配器
public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {

    private FragmentManager fragmentManager;
    private List<Fragment> fragmentList;
    public MyFragmentPagerAdapter(FragmentManager fragmentManager,List<Fragment> fragmentList) {
        super(fragmentManager);
        this.fragmentManager = fragmentManager;
        this.fragmentList = fragmentList;
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        return fragmentList.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return fragmentList.size();
    }

}

 

Activity_home.java 要继承FragmentActivity

package com.example.speed_app;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.RadioButton;
public class Activity_home extends FragmentActivity{
        private int currIndex;//记录滑动的第几个
        private ViewPager viewPager;//ViewPager滚动功能
        private List<Fragment> fragmentList;//一共有多少个Fragment集合
        private RadioButton radioButton1,radioButton2,radioButton3,radioButton4,radioButton5;
       
        
        @Override
       protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         super.onCreate(savedInstanceState);
         setContentView(R.layout.home);
         
         //z初始化 viewpager
         viewPager = (ViewPager) findViewById(R.id.vp);
         fragmentList = new ArrayList<Fragment>();
         Fragment fragment1 = new Activity_text1();//滑动页面的布局1
         Fragment fragment2 = new Activity_text2();//滑动页面的布局2
         Fragment fragment3 = new Activity_text3();//滑动页面的布局3
         Fragment fragment4 = new Activity_text4();//滑动页面的布局4
         Fragment fragment5 = new Activity_text5();//滑动页面的布局5
         
         fragmentList.add(fragment1);//添加到集合保存
         fragmentList.add(fragment2);//添加到集合保存
         fragmentList.add(fragment3);//添加到集合保存
         fragmentList.add(fragment4);//添加到集合保存
         fragmentList.add(fragment5);//添加到集合保存
         
         //FragmentManager能够实现管理activity中fragment. 通过调用activity的getFragmentManager()取得它的实例
         FragmentManager fragmentManager = getSupportFragmentManager();//V4的包
         
         //FragmentPagerAdapter类
         MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fragmentManager, fragmentList); 
         viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);//默认从第1个开始 也就是Activity_text1()这个Fragment开始
        
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            
            @Override
            public void onPageSelected(int arg0) {
                
                // TODO Auto-generated method stub
                //Animation animation = new TranslateAnimation(0,0,0,0);
                //animation.setFillAfter(true);
                currIndex = arg0;//获取目前滑动的坐标
                
                radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
                radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
                radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
                radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
                radioButton5 = (RadioButton) findViewById(R.id.radioButton5);
                
                switch (arg0) {
                case 0:
                    radioButton1.setChecked(true);//如果是0坐标,那radioButton1这个单选按钮为true,也就是点击状态
                    break;
                case 1:
                    radioButton2.setChecked(true);
                    break;
                case 2:
                    radioButton3.setChecked(true);
                    break;
                case 3:
                    radioButton4.setChecked(true);
                    break;
                case 4:
                    radioButton5.setChecked(true);
                    break;
                default:
                    break;
                }
                
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0) {
                
                
            }
        });
        
        
        //下面可以通过点击radioButton按钮实现切换页面
        radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
        radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
        radioButton5 = (RadioButton) findViewById(R.id.radioButton5);
        
        radioButton1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);//这个0代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text1()
                
            }
        });
        
        radioButton2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);//这个1代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text2()
                
                
            }
        });

        radioButton3.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);//这个2代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text3()
                    
            }
        });

        radioButton4.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        viewPager.setCurrentItem(3);//这个3代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text4()
        
         }
    });

        radioButton5.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(4);//这个4代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text5()
            }
        });
 }
        
}

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/896240130Master/p/6200556.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值