如图是效果图

当  点击下面 不同 的按钮 进入 不同的界面

其中 要一个 主布局当做容器 , 和3个不同的 布局来对应下面的3个按钮界面

主程序的 代码和布局如下

 import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button; import com.example.huanshop.activity.R;
import com.example.huanshop.activity.main.MyShopFragment;
import com.example.huanshop.base.BaseFragmentActivity; public class ProducteTimeLimit extends BaseFragmentActivity {
private int index;
// 当前fragment的index
private int currentTabIndex;
private TimeLimitShopFragment timeLimitShop;
private TimeLimitQiangShop timeLimitQiangShop;
private MyShopFragment myShopFragment;
private Fragment[] fragments;
private Button[] btnTabs; @Override
protected void setContentView() {
// TODO Auto-generated method stub
setContentView(R.layout.timeshop);
} @Override
protected void setFindViewById() {
// TODO Auto-generated method stub
btnTabs = new Button[3];
btnTabs[0] = (Button) findViewById(R.id.timelimited);
btnTabs[1] = (Button) findViewById(R.id.lastshop);
btnTabs[2] = (Button) findViewById(R.id.brandshop);
// 把第一个设置成默认选中状态
btnTabs[0].setSelected(true); } @Override
protected void setControll() {
// TODO Auto-generated method stub
timeLimitShop = new TimeLimitShopFragment();
timeLimitQiangShop = new TimeLimitQiangShop();
myShopFragment = new MyShopFragment();
fragments = new Fragment[] { timeLimitShop, timeLimitQiangShop,
myShopFragment };
// 添加显示第一个fragment
getSupportFragmentManager().beginTransaction()
.add(R.id.timeshopfragment, timeLimitShop)
.add(R.id.timeshopfragment, timeLimitQiangShop)
.add(R.id.timeshopfragment, myShopFragment)
.hide(timeLimitQiangShop).hide(myShopFragment)
.hide(myShopFragment).show(timeLimitShop).commit();
} @Override
protected void setListener() {
// TODO Auto-generated method stub } public void onTabClicked(View view) {
switch (view.getId()) {
case R.id.timelimited:
index = 0;
break;
case R.id.lastshop:
index = 1;
break;
case R.id.brandshop:
index = 2;
break;
}
if (currentTabIndex != 0) {
btnTabs[0].setSelected(false);
}
if (currentTabIndex != index) {
FragmentTransaction trx = getSupportFragmentManager()
.beginTransaction();
trx.hide(fragments[currentTabIndex]); if (!fragments[index].isAdded()) {
trx.add(R.id.timeshopfragment, fragments[index]);
}
trx.show(fragments[index]).commit();
}
btnTabs[currentTabIndex].setSelected(false);
// 把当前tab设为选中状态
btnTabs[index].setSelected(true);
currentTabIndex = index;
} }

主代码的布局文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/all_layout_style" > <LinearLayout
android:id="@+id/timebtton1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="@color/bottom_bar_normal_bg"
android:gravity="center_vertical"
android:orientation="vertical" > <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#999999" /> <LinearLayout
android:id="@+id/main_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bottom_bar_normal_bg"
android:gravity="center_vertical"
android:orientation="horizontal" > <RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" > <Button
style="@style/main_btn"
android:id="@+id/timelimited"
android:clickable="true"
android:drawableTop="@drawable/tab_home_bg"
android:text="限时购" />
</RelativeLayout> <RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" > <Button
style="@style/main_btn"
android:id="@+id/lastshop"
android:drawableTop="@drawable/tab_classify_bg"
android:text="最后疯抢" />
</RelativeLayout> <RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" > <Button
style="@style/main_btn"
android:id="@+id/brandshop"
android:drawableTop="@drawable/tab_classify_bg"
android:text="品牌促销" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<!-- --> <RelativeLayout
android:id="@+id/timeshopfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/timebtton1" >
</RelativeLayout> </RelativeLayout>

其中3个fragment这里值给出一个

 import java.util.List;

 import com.android.http.LoadControler;
import com.android.http.RequestManager;
import com.android.http.RequestManager.RequestListener;
import com.example.huanshop.activity.R;
import com.example.huanshop.activity.main.HomeFragment;
import com.example.huanshop.adapter.ProducteTimeAdapter;
import com.example.huanshop.entity.Product;
import com.example.huanshop.util.HttpUtil;
import com.example.huanshop.widget.MyListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import android.content.Intent;
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.AdapterView.OnItemClickListener;
import android.widget.Toast; public class TimeLimitQiangShop extends Fragment implements RequestListener {
private MyListView listView;
private ProducteTimeAdapter listAdapter;
private List<Product> products;
private LoadControler load;
private ImageLoader loader;
private String urlString = HttpUtil.BASEURL + "Product/GetByIsSpecial/4"; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.producttimeqiang, container, false);
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setFindViewById();
setListener();
setControll();
} private void setFindViewById() {
// TODO Auto-generated method stub
loader = ImageLoader.getInstance();
loader.init(ImageLoaderConfiguration.createDefault(getActivity()));
listView = (MyListView) getActivity().findViewById(R.id.timeqiang);
} private void setControll() {
// TODO Auto-generated method stub
load = RequestManager.getInstance().get(urlString, this, 2);
} private void setListener() {
// TODO Auto-generated method stub
listView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
HomeFragment.kucun = products.get(position).getKucun() + "";
HomeFragment.Title = products.get(position).getTitle();
HomeFragment.SmallPic = products.get(position).getSmallPic();
HomeFragment.Contens = products.get(position).getContens();
HomeFragment.Price = products.get(position).getPrice() + "";
HomeFragment.SalePrice = products.get(position).getSalePrice()
+ "";
HomeFragment.BuyCount = products.get(position).getBuyCount()
+ "";
HomeFragment.producteId = products.get(position).getId() + "";
Intent intent = new Intent(getActivity(),
ProducteDetailsActivity.class);
// intent.putExtra("positon", position);
startActivity(intent);
}
});
} @Override
public void onError(String arg0, String arg1, int arg2) {
// TODO Auto-generated method stub } @Override
public void onRequest() {
// TODO Auto-generated method stub } @Override
public void onSuccess(String arg0, String arg1, int arg2) {
// TODO Auto-generated method stub
switch (arg2) {
case 2:
products = new Gson().fromJson(arg0,
new TypeToken<List<Product>>() {
}.getType());
if (products.size() == 0) {
Toast.makeText(getActivity(), "没有产品", 1000).show();
} else {
listAdapter = new ProducteTimeAdapter(getActivity(), products);
listView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
}
}
}

FragmentActivity的简单使用的更多相关文章

  1. 简单研究Loader笔记

    2015-11-11 18:25:34 1. Loader是什么? /** * Static library support version of the framework's {@link and ...

  2. TabLayout 简单使用。

    先上效果图 在使用TabLayout 之前需要导入design包. 我使用的是android studio 只要在build.gradle中加入 compile 'com.android.suppor ...

  3. android FragmentActivity+FragmentTabHost+Fragment框架布局

    这周比较闲,计划系统的学习一下android开发,我本是一名IOS程序员,对手机开发还是有自己的一套思路的, 固这套思路用到我当前学android上了,先选择从Main页面的tabbar部分代码入手, ...

  4. 【Android UI设计与开发】4.底部菜单栏(一)Fragment介绍和简单实现

    TabActivity在Android4.0以后已经被完全弃用,取而代之的是Fragment.Fragment是Android3.0新增的概念,Fragment翻译成中文是碎片的意思,不过却和Acti ...

  5. ☆ fragment和fragmentactivity解析 (转)

    一.为什么要使用Fragment  1.当我们需要动态的多界面切换的时候,就需要将UI元素和Activity融合成一 个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单 ...

  6. 【原创】StickHeaderListView的简单实现,解决footerView问题

    1.前言: 前几天用了GitHub上se.emilsjolander.stickylistheaders这个组件,然后发现这个组件的listview不能添加footerView,加了footer后,滑 ...

  7. Android FragmentActivity+viewpager的使用

    使用场景,打算设计一个“底部菜单栏+其余可滑动的页面”的简单的功能. package com.lanyuweng.mibaby; import android.content.Intent; impo ...

  8. Android Fragment 简单实例

    Android上的界面展示都是通过Activity实现的.Activity实在是太经常使用了.我相信大家都已经很熟悉了,这里就不再赘述. 可是Activity也有它的局限性,相同的界面在手机上显示可能 ...

  9. Fragment的简单使用

    最近一直有点忙,刚刚看到一个朋友的留言提到Fragment中加载ListView的问题,这里写了一个非常简单的测试,至于对Fragment的增.删.改实现动态布局构建灵活的UI,以后有时间在讨论: M ...

随机推荐

  1. scrapy的 安装 及 流程 转

    安装 linux 和 mac 直接  pip install scrapy 就行 windows 安装步骤         a. pip3 install wheel       b. 下载twist ...

  2. struts2学习笔记(五)—— OGNL

    一.OGNL概述 1.1 什么是OGNL OGNL的全称是对象图导航语言( object-graph Navigation Language),它是一种功能强大的开源表达式语言,使用这种表达式语言,可 ...

  3. hdu1711 Number Sequence kmp模板

    题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...

  4. setlocal enabledelayedexpansion 解释

    看字面的意思是:设置本地为延迟扩展.其实也就是:延迟变量,全称"延迟环境变量扩展", 在cmd执行命令前会对脚本进行预处理,其中有一个过程是变量识别过程,在这个过程中,如果有两个% ...

  5. java语言的各种输入情况-ACM

    1.只输入一组数据: Scanner s=new Scanner(System.in);int a=s.nextInt();int b=s.nextInt(); 2.输入有多组数据,没有说明输入几组数 ...

  6. C/S架构与B/S架构的区别

    什么是C/S结构和B/S结构? C/S结构 C/S结构是指Client/Server (客户机/服务器) 结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了 ...

  7. 远程上传下载文件-Xftp5

    Xftp5下载 链接:https://pan.baidu.com/s/1Wzso_Q7mPy5uGOUlripEWg 密码:xfx9 安装选择家庭版 由于21端口没有开,所以不能选FTP,  选择SF ...

  8. 解决 下载额外数据文件失败 以下软件包要求安装后下载附加数据,但其数据无法下载或无法处理 ttf-mscorefonts-installer

    ubuntu 14.04 今天安装完 wine,之后会出现这个问题 原因应该是需要的字体无法下载 那你需要手动下载, 到这个地址下载 http://sourceforge.net/projects/c ...

  9. 信号和槽:Qt中最差劲的创造

    不要被这个标题唬住了,实际上我是非常认可Qt的.在C++实现的开源产品中没有哪一个的API风格比得上Qt,拥有高度一致性,符合常识,符合直觉,几乎不用学就可以直接上手.或许是由于我们摆脱不了马太效应的 ...

  10. 记一次坑爹的jconsole使用

    之前一直是用jstat来监控GC的,后来发现原来有个自带的jconsole,是可始化界面的,而且也是oracle公司自带的工具,与是拿来用一下,发现蛮好用的. 然而,在一次复现实验中,发现原来能复现的 ...