昨天写的这几篇博客,Android-fragment简介-fragment的简单使用Activity-fragment-ListView展示Android-fragment生命周期Android-fragment的替换, 都是讲解使用 android.app.Fragment 自身的Fragment,不是v4包的;

而今天的博客是专门讲解v4.app.Fragment(v4包的),全部都是要导入v4包,使用v4包的Fragment有个好处就是可以兼容低版本

以前的导包:

import android.app.Fragment;
import android.app.FragmentTransaction;

现在的导包:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

Activity的代码:

package liudeli.activity.fragment;

import android.graphics.Color;
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.view.View;
import android.widget.Button; import liudeli.activity.R; /**
* 全部使用v4.app.Fragment支持包来实现
* 既然用了全部使用v4.app.Fragment支持包来实现,所以Activity必须是FragmentActivity才能识别布局的<fragment
*/
public class MyTestFragmentActivity4 extends FragmentActivity implements View.OnClickListener { private Button msg;
private Button persons;
private Button my; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_fragment4); initView();
initChangeFragment();
initListener();
} private void initView() {
msg = findViewById(R.id.bt_msg);
persons = findViewById(R.id.bt_persons);
my = findViewById(R.id.bt_my);
} /**
* 初始化默认切换到 消息Fragment
*/
private void initChangeFragment() {
/**
* 得到FragmentManager 要用 v4支持包里面的getSupportFragmentManager
*/
FragmentManager manager = getSupportFragmentManager();
// 开始事务 得到事务
FragmentTransaction fragmentTransaction = manager.beginTransaction();
// 替换操作
fragmentTransaction.replace(R.id.frame_layout, new MsgFragment());
// 提交
fragmentTransaction.commit(); setButton(0);
} private void initListener() {
msg.setOnClickListener(this);
persons.setOnClickListener(this);
my.setOnClickListener(this);
} @Override
public void onClick(View v) {
/**
* 得到FragmentManager 要用 v4支持包里面的getSupportFragmentManager
*/
FragmentManager manager = getSupportFragmentManager();
// 开始事务 得到事务
FragmentTransaction fragmentTransaction = manager.beginTransaction(); Fragment fragment = null; switch (v.getId()) {
case R.id.bt_msg:
fragment = new MsgFragment();
setButton(0);
break;
case R.id.bt_persons:
setButton(1);
fragment = new PersonsFragment();
break;
case R.id.bt_my:
setButton(2);
fragment = new MyWoFragment();
break;
}
// 替换操作
fragmentTransaction.replace(R.id.frame_layout, fragment);
// 提交
fragmentTransaction.commit();
} /**
* 设置三个按钮的颜色
* @param value
*/
private void setButton(int value) {
switch (value) {
case 0:
msg.setTextColor(Color.RED);
persons.setTextColor(Color.BLACK);
my.setTextColor(Color.BLACK);
break;
case 1:
msg.setTextColor(Color.BLACK);
persons.setTextColor(Color.RED);
my.setTextColor(Color.BLACK);
break;
case 2:
msg.setTextColor(Color.BLACK);
persons.setTextColor(Color.BLACK);
my.setTextColor(Color.RED);
break;
} }
}

Activity布局的代码:

<?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已经先填充了,剩下的控件我全部来填充 -->
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
> </FrameLayout> <!-- 我的layout_weight默认为0,我先填充我的控件 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_below="@id/frame_layout"> <Button
android:id="@+id/bt_msg"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="消息"
android:gravity="center"
android:textColor="@android:color/black"
/> <Button
android:id="@+id/bt_persons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="好友"
/> <Button
android:id="@+id/bt_my"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="我的"
/> </LinearLayout> </LinearLayout>

第一个Fragment的代码:

package liudeli.activity.fragment;

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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast; public class MsgFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return new ListView(getActivity()); // Fragment不能使用this
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); final String[] data = new String[]{
"你有一条消息1",
"你有一条消息2",
"你有一条消息3",
"你有一条消息4",
"你有一条消息5",
"你有一条消息6",
"你有一条未读消息6",
"你有一条未读消息7",
"你有一条未读消息8",
}; ListView listView = (ListView)view;
ListAdapter listAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
data);
listView.setAdapter(listAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
}
});
}
}

第二个Fragment的代码:

package liudeli.activity.fragment;

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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast; public class PersonsFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return new ListView(getActivity()); // Fragment不能使用this
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); final String[] data = new String[]{
"张三",
"李四",
"王五",
"赵六",
"王八",
"朱九",
"厨十",
"阿名",
"雄霸",
}; ListView listView = (ListView)view;
ListAdapter listAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
data);
listView.setAdapter(listAdapter); // ListVIew 设置可以解决,Item长按无反应的问题: android:descendantFocusability="blocksDescendants"
// listView.setDescendantFocusability(2); /*listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
return true;
}
});*/ listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
}
});
}
}

第三个Fragment的代码:

package liudeli.activity.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter; public class MyWoFragment extends android.support.v4.app.Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return new GridView(getActivity()); // Fragment不能使用this
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); String[] data = new String[]{
"我的账号",
"我的社交",
"我的简洁",
"我的钱包",
"我的设置",
"退出账号",
"重置账号"
}; GridView gridView = (GridView)view; // 设置三列
gridView.setNumColumns(3); ListAdapter listAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
data);
gridView.setAdapter(listAdapter);
}
}

效果:

Android-fragment的替换-V4支持包的更多相关文章

  1. Android-fragment-ListView展示-v4支持包

    昨天写的这几篇博客,Android-fragment简介-fragment的简单使用,Activity-fragment-ListView展示,Android-fragment生命周期,Android ...

  2. 如何在Android Studio中添加RecyclerView-v7支持包

    1.打开SDK Manager,在Extras树下找到Android Support Library,下载好支持包.RecyclerView在v7-21版本就出来了.我这里不用更新了,说明是最新的,怎 ...

  3. Android最新支持包Design简介

    Android 5.0 Lollipop是曾经最著名的Android发布之一,这样说很大一部分原因是材料设计的引入,而材料设计则是一种刷新了整个Android体验的设计语言.这个详细说明是开始适应材料 ...

  4. Android Fragment 生命周期及其API使用(建议使用自定义View替换Fragment)

    我为什么不主张使用Fragment Fragment:( Fragment就相当于一个有生命周期的View,它的生命周期被所在的Activity的生命周期管理 ) 生命周期回调说明: onAttach ...

  5. android v4兼容包

    一句话解释android兼容包就是:支持更多的组件,样式更好看了.好粗糙的解释啊! 我们都知道Android一些SDK比较分裂,为此google官方提供了Android Support Library ...

  6. Android Fragment 生命周期及其正确使用(建议使用自定义View替换Fragment)

    使用Fragment 官方例子中显示: 例如:一个学生Fragment,需要传入studentId,进行http请求显示,那么setArguments后防止杀掉Fragment后,参数为0,显示不了数 ...

  7. Android support library支持包常用控件介绍(一)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现Material Design设计效果,官方给出了Android support design library 支 ...

  8. Android support library支持包常用控件介绍(二)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...

  9. V4 V7 V13支持包的区别(转)

    三者均为支持包,可以让低版本系统使用高版本特性,支持最小版本有差异 V4支持1.6以上 V7支持2.1以上 V13支持3.2以上 V7依赖V4 转自:

随机推荐

  1. function方法控制是否隐藏部分内容

    $(document).ready(function() { $('input[type=radio][name=IE]').change(function() { if (this.value == ...

  2. DOS中命令的格式

    ---------------siwuxie095 一.DOS中,命令使用格式的一般形式 用中文表达的形式为: [路径]  关键字  [盘符]  [路径]  文件名  [扩展名]  (参数)  [参数 ...

  3. python学习——urlparse模块

    urlparse模块: 1.urlparse() 具体程序及结果如下: >>> url = 'http://i.cnblogs.com/EditPosts.aspx?opt=1'&g ...

  4. socket接口详解

    1. socket概述 socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信. socket起源于UNIX,在Unix一切 ...

  5. Spring框架管理开源的连接池

    1. 管理DBCP连接池 * 先引入DBCP的2个jar包 * com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar * com.spring ...

  6. PS大神的作品,每张都是科幻大片!

    相信大家在网上一定见过 各种PS的作品 但是要想成为“PS大神”, 不仅仅要会P图, 最关键的就是脑洞! 同样的马路破坏效果 在大神操作后变成了大片! 摩托车换成了骏马 这效果果然不一般! 这个绝对牛 ...

  7. mysql 执行多线程临时方案

    sqr::IDatabase *db=NULL;IDbConnection *conn = NULL;int main(int argc, char* argv[]) { db = GetDataba ...

  8. Fully Update And Upgrade Offline Debian-based Systems

    Let us say, you have a system (Windows or Linux) with high-speed Internet connection at work and a D ...

  9. C++11的一般概念——The C++ standard library, 2nd Edition 笔记(二)

    ( 原书第四章,General Concepts) 只介绍新内容,关于头文件格式和后缀等C++03已经规范化的内容,不再赘述. namespace std:新的std子空间包括: std::tr1, ...

  10. 【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法(一)

    Java原生的API可用于发送HTTP请求 即java.net.URL.java.net.URLConnection,JDK自带的类: 1.通过统一资源定位器(java.net.URL)获取连接器(j ...