一  左侧标题列表

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. querySelector和querySelectorAll方法介绍

    module dom { [Supplemental, NoInterfaceObject] interface NodeSelector { Element querySelector(in DOM ...

  2. Qt setStyleSheet 添加背景色/背景图片(取消背景色,读取本地文件作为背景色)

    容易搞定,mainWindow 是一个QWidget.// 设置背景色为蓝色mainWindow.setStyleSheet("background-color:blue;"); ...

  3. Android dp和sp的用法汇总

    1 > dp 是跟像素密度无关的单位,也就是说在相同尺寸.但不同的分辨率的手机上,用dp标识的东西,显示的大小是一样的. sp是用于标识字体的,它不仅跟屏幕尺寸有关,还跟设置的系统字体大小有关. ...

  4. iOS中谓词的使用

    Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...

  5. jvm Classload method介绍

    1,jvm Classload默认几个重要方法介绍 findClass:Finds and loads the class with the specified name from the URL s ...

  6. ios8.1上运行程序,程序界面只显示一部分

    在ios 9.1上运行程序没问题 但是在8.1上运行发现模拟器上只显示了程序的一小部分界面,没有显示完全. 结果发现由以下代码设置问题引起的 - (BOOL)application:(UIApplic ...

  7. N皇后( DFS,推荐)

    N皇后问题 Description   在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.          你的任 ...

  8. ubuntu安装ulipad

    以下内容部分我是从其他地方找的,并且做了适当的修改,亲身测试可以安装成功   在安装ulipad之前,先安装一个超级好用的Python的交互式Shell--iPython.iPython功能很强大, ...

  9. UVa401 Palindromes

    #include <stdio.h>#include <string.h> char mirror(char c){    static const char m[] = &q ...

  10. [Swust OJ 1132]-Coin-collecting by robot

          题目链接:          http://acm.swust.edu.cn/problem/1132/ Time limit(ms): 1000 Memory limit(kb): 65 ...