一  左侧标题列表

1.1  布局 left_fragment.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:listSelector="@drawable/onitem_selected_bkcolor"/>

1.2  ListSelector  onitem_selected_bkcolor.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_window_focused="false"
        android:drawable="@android:color/holo_green_dark"/>
    <item
        android:state_window_focused="true"
        android:drawable="@android:color/holo_green_light"/>
</selector>

1.3  自定义 ListItem 布局 代替  android.R.layout.simple_list_item_1

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:gravity="center_vertical"
    android:paddingLeft="10dp"
    android:textColor="@android:color/black"/>

1.4  自定义 LeftFragment

package com.example.myfragments;

 
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
 
//自定义回调函数
interface onItemSeletedListener
{
    public void onItemSeleted(int position); 
}
 
public class LeftFragment extends ListFragment {
 
    onItemSeletedListener mCallback;
    
    String[] data = {"item0","item1","item2","item3","item4","item5","item6","item7","item8","item9","item10","item11","item12","item13","item14","item15","item16"}; 
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO 自动生成的方法存根
        return inflater.inflate(R.layout.left_fragment, container,false);
    }
 
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO 自动生成的方法存根
        setListAdapter(new ArrayAdapter<String>(getActivity(),  
                   R.layout.listitem, data)); 
        super.onActivityCreated(savedInstanceState);
    }
 
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO 自动生成的方法存根
        mCallback.onItemSeleted(position);        
    }
    
    @Override
    public void onAttach(Activity activity) {
        // TODO 自动生成的方法存根
        super.onAttach(activity);
        // This makes sure that the container activity has implemented  
        // the callback interface. If not, it throws an exception  
        try {  
            mCallback = (onItemSeletedListener) activity;  
        } catch (ClassCastException e) {  
            throw new ClassCastException("必须实现 onItemSeletedListener");
        }  
    }
}
            
 

二  右侧内容展示

2.1  布局 right_fragment.xml

<LinearLayout 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"
    android:background="@android:color/background_dark"
    android:orientation="vertical"
    tools:ignore="HardcodedText,UselessParent" >
 
    <ScrollView 
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="请选择左侧边栏 :)"
            android:textColor="@android:color/holo_orange_dark"
            android:textSize="30sp" />
 
    </ScrollView>

</LinearLayout>

2.1  自定义 RightFragment

package com.example.myfragments;

 
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
public class RightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO 自动生成的方法存根
        return inflater.inflate(R.layout.right_fragment, container,false);
    }
    
    //更新数据
    public void update(int position)
    {
        TextView textview=(TextView)getActivity().findViewById(R.id.textview);
        textview.setText("您选择了:" + String.valueOf(position)+"\n--------------"
                +"\n大江东去浪淘尽,\n千古风流人物,\n故垒西边,\n人道是,\n三国周郎赤壁,\n乱石穿空,\n惊涛拍岸,\n卷起千堆雪,\n江山如画,\n一时多少豪杰。"
                +"\n遥想公瑾当年,\n小乔初嫁了,\n雄姿英发,\n羽扇纶巾,\n谈笑间,\n樯橹灰飞烟灭,\n故国神游,\n多情应笑我,\n早生华发,\n人间如梦,\n一樽还酹江月。");
    }
}
 

三  添加到 main_layout.xml 中,附着于 Activity 显示

3.1  布局 main_layout.xml

<LinearLayout 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"
    android:orientation="horizontal"
    tools:ignore="DisableBaselineAlignment" >
 
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.myfragments.LeftFragment"
        android:layout_width="70dp"
        android:layout_height="match_parent"/>
    <View 
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="@android:color/background_dark"/>
 
    <fragment
        android:id="@+id/right_fragment"
        android:name="com.example.myfragments.RightFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

</LinearLayout>

3.2  MainActivity

package com.example.myfragments;

 
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
 
public class MainActivity extends Activity implements onItemSeletedListener{
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   
        requestWindowFeature(Window.FEATURE_NO_TITLE);   
        setContentView(R.layout.main_layout);     
        
//        //添加
//        FragmentManager fragmentmanager=getFragmentManager();
//        FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction();
//        LeftFragment leftfragment=new LeftFragment();
//        fragmenttransaction.add(R.id.left_fragment, leftfragment);
//        fragmenttransaction.commit();
//        //删除
//        FragmentManager fragmentmanager=getFragmentManager();
//        FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction();
//        Fragment leftfragment=fragmentmanager.findFragmentById(R.id.left_fragment);
//        fragmenttransaction.remove(leftfragment);
//        fragmenttransaction.commit();
//        //替换
//        FragmentManager fragmentmanager=getFragmentManager();
//        FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction();
//        fragmenttransaction.replace(R.id.left_fragment, new LeftFragment());
//        fragmenttransaction.commit();
    }
 
    @Override
    public void onItemSeleted(int position) {
        RightFragment rightFragment=(RightFragment) getFragmentManager().findFragmentById(R.id.right_fragment);
        
        if(rightFragment != null)
            rightFragment.update(position);
    }
}
 

四  结果展示

      
 
    
转载请注明出处 :)

 
 

Android 之 Fragment的更多相关文章

  1. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  2. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  3. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  4. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  5. android之Fragment基础详解(一)

      一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...

  6. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  7. android之fragment的使用

    android中的fragment与html中的div很类似,下图中通过左边的按键可以控制右边的显示内容.右边的内容就是一个fragment,通过点击按键来控制fragment的实现. 工程目录 需要 ...

  8. Android使用Fragment定义弹出数字键盘

    fragment主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...

  9. Android ViewPager Fragment使用懒加载提升性能

     Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...

  10. 33.Android之Fragment学习

    Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

随机推荐

  1. 无法将类型“ASP.login_aspx”转换为“System.Web.UI.WebControls.Login”

    今天碰上了一个很傻的问题,起码我认为是这样. 项目中首页名是:Login.aspx,编译.运行都没有出现问题. 于是打包发布网站,各项内容都配置好后,问题出现了.一运行首页面就出现下面这个错误: 编译 ...

  2. 多少遍ner让他加56看6

    http://www.huihui.cn/share/8112372 http://www.huihui.cn/share/8112363 http://www.huihui.cn/share/811 ...

  3. 搭建你的持续集成server - CruiseControl step by step(1)

    CruiseControl是CIserver的老者,诞生已是多年,在很多方面,CruiseControlserver已经成为持续集成实践的同义词.而如今,CruiseControl已发展成为一个家族式 ...

  4. HDU 5091 线段树扫描线

    给出N个点.和一个w*h的矩形 给出N个点的坐标,求该矩形最多能够覆盖多少个点 对每一个点point(x.y)右边生成相应的点(x+w,y)值为-1: 纵向建立线段树,从左到右扫描线扫一遍.遇到点则用 ...

  5. Android Testing(1) 浅尝Android测试的奥秘

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 仅供学习和交流使用,翻译不好勿喷,请只摘除不合适的地方 Testing The Android fram ...

  6. Javascript-one

    今天,学习Javascript第一天,学习了一些基本的概念,下面就对今天所学的知识进行一个整理,回顾吧! 首先,将Javascript代码包含在(X)html文档中,主要的方法是使用<scrip ...

  7. BZOJ 1911: [Apio2010]特别行动队( dp + 斜率优化 )

    sum为战斗力的前缀和 dp(x) = max( dp(p)+A*(sumx-sump)2+B*(sumx-sump)+C )(0≤p<x) 然后斜率优化...懒得写下去了... ------- ...

  8. BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )

    线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...

  9. Memcache 查看列出所有key方法

    参考博文: Memcache 查看列出所有key方法 1. cmd上登录memcache telnet 127.0.0.1 11211  2. 列出所有keys stats items // 这条是命 ...

  10. python——登陆接口设计(循环方法)

    近日重新整理了登陆接口设计程序,感觉以前的代码没有注释,让园子的其他童鞋读起来比较费劲.也没有流程图和程序运行说明. 1.流程图 2.user_file.txt&lock_file.txt文件 ...