您的位置:首页 > 移动开发 > Android开发

Android中Fragment点击切换与添加ViewPager滑动切换

2017-06-13 20:24 621 查看
我想大家对这些都不陌生了吧,我还是大概说一下吧,哈哈哈哈。。。。。。

首先介绍一下Fragment是碎片,

为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像Activity一样包含布局。Fragment通常是嵌套在Activity中使用的,现在想象这种场景:有两个Fragment,Fragment 1包含了一个ListView,每行显示一本书的标题。Fragment 2包含了TextView和ImageView,来显示书的详细内容和图片。

而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!

另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!

别的不再多说,直接贴代码。。。。。。

这里写代码片
xml布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="@+id/mcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >

<Button
android:id="@+id/mBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />

<Button
android:id="@+id/mBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
</LinearLayout>

</RelativeLayout>


这里写代码片
fragmenta代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/mTv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="碎片一"
android:textSize="20sp" />

</RelativeLayout>


这里写代码片
fragmentb代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/mTv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="20sp"
android:text="碎片二" />

</RelativeLayout>


这里写代码片
Fragment_a代码
package com.example.cn.bgs.fragmentviewpagerdemoa;

import android.R.color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Fragment_a extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=View.inflate(getActivity(),R.layout.fragmenta, null);
return v;
}

}


这里写代码片
Fragment_b代码
package com.example.cn.bgs.fragmentviewpagerdemoa;

import android.R.color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Fragment_b extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=View.inflate(getActivity(),R.layout.fragmentb, null);
return v;
}

}


这里写代码片
MainActivity代码
package com.example.cn.bgs.fragmentviewpagerdemoa;

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

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity implements OnClickListener,
OnPageChangeListener {
private Button mBtn1, mBtn2;
private Fragment_a fa;
private Fragment_b fb;
private FragmentManager manager;
private ViewPager pager;
private List<Fragment> list=new ArrayList<Fragment>();
private MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//      manager = getSupportFragmentManager();
//      FragmentTransaction transaction = manager.beginTransaction();
//      fa = new Fragment_a();
//      transaction.add(R.id.mcontainer, fa);
//      transaction.commit();
initView();
}

private void initView() {
mBtn1 = (Button) findViewById(R.id.mBtn1);
mBtn2 = (Button) findViewById(R.id.mBtn2);

list.add(new Fragment_a());
list.add(new Fragment_b());

pager = (ViewPager) findViewById(R.id.pager);

mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
pager.setOnPageChangeListener(this);

adapter = new MyAdapter(getSupportFragmentManager(), list);
pager.setAdapter(adapter);

}

// @Override
// public void onClick(View v) {
// hideAll();
// FragmentTransaction transaction = manager.beginTransaction();
// switch (v.getId()) {
//
// case R.id.mBtn1:
// if (fa == null) {
// fa = new Fragment_a();
// transaction.add(R.id.mcontainer, fa);
// } else {
// transaction.show(fa);
// }
//
// break;
// case R.id.mBtn2:
// if (fb == null) {
// fb = new Fragment_b();
// transaction.add(R.id.mcontainer, fb);
// } else {
// transaction.show(fb);
// }
// break;
// }
// transaction.commit();
//
// }
//
// public void hideAll() {
// FragmentTransaction transaction = manager.beginTransaction();
// if (fa != null) {
// transaction.hide(fa);
// }
// if (fb != null) {
// transaction.hide(fb);
// }
// transaction.commit();
// }
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mBtn1:
mBtn1.setTextColor(Color.GREEN);
pager.setCurrentItem(0, false);
break;
case R.id.mBtn2:
mBtn2.setTextColor(Color.GREEN);
pager.setCurrentItem(1, false);
break;
}

}

class MyAdapter extends FragmentPagerAdapter {

private List<Fragment> list;

public MyAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;

}

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

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

}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageSelected(int arg0) {
clear();
switch (arg0) {
case 0:
mBtn1.setTextColor(Color.GREEN);
pager.setCurrentItem(0, false);
break;
case 1:
mBtn2.setTextColor(Color.GREEN);
pager.setCurrentItem(1, false);
break;
}

}

private void clear() {
mBtn1.setTextColor(Color.GRAY);
mBtn2.setTextColor(Color.GRAY);
}

}
红色注销的是切换的代码


别的不再多说,效果也就这样



好了,一切都结束了。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android fragment viewpager
相关文章推荐