【Android基础入门〖11〗】Fragment


目录(?)[+]

一  左侧列表展示

1.1  布局 left_fragment.xml

  1. <ListView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@android:id/list"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@android:color/darker_gray"  
  6.     android:listSelector="@drawable/onitem_selected_bkcolor"/>  

1.2  ListSelector  onitem_selected_bkcolor.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item   
  4.         android:state_window_focused="false"  
  5.         android:drawable="@android:color/holo_green_dark"/>  
  6.     <item  
  7.         android:state_window_focused="true"  
  8.         android:drawable="@android:color/holo_green_light"/>  
  9. </selector>  

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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="30dp"  
  5.     android:gravity="center_vertical"  
  6.     android:paddingLeft="10dp"  
  7.     android:textColor="@android:color/black"/>  

1.4  自定义 LeftFragment

  1. package com.example.myfragments;  
  2. import android.app.Activity;  
  3. import android.app.ListFragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.ArrayAdapter;  
  9. import android.widget.ListView;  
  10. //自定义回调函数  
  11. interface onItemSeletedListener  
  12. {  
  13.     public void onItemSeleted(int position);   
  14. }  
  15. public class LeftFragment extends ListFragment {  
  16.     onItemSeletedListener mCallback;  
  17.       
  18.     String[] data = {"item0","item1","item2","item3","item4","item5","item6","item7","item8","item9","item10","item11","item12","item13","item14","item15","item16"};   
  19.       
  20.     @Override  
  21.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  22.             Bundle savedInstanceState) {  
  23.         // TODO 自动生成的方法存根  
  24.         return inflater.inflate(R.layout.left_fragment, container,false);  
  25.     }  
  26.     @Override  
  27.     public void onActivityCreated(Bundle savedInstanceState) {  
  28.         // TODO 自动生成的方法存根  
  29.         setListAdapter(new ArrayAdapter<String>(getActivity(),    
  30.                    R.layout.listitem, data));   
  31.         super.onActivityCreated(savedInstanceState);  
  32.     }  
  33.     @Override  
  34.     public void onListItemClick(ListView l, View v, int position, long id) {  
  35.         // TODO 自动生成的方法存根  
  36.         mCallback.onItemSeleted(position);          
  37.     }  
  38.       
  39.     @Override  
  40.     public void onAttach(Activity activity) {  
  41.         // TODO 自动生成的方法存根  
  42.         super.onAttach(activity);  
  43.         // This makes sure that the container activity has implemented    
  44.         // the callback interface. If not, it throws an exception    
  45.         try {    
  46.             mCallback = (onItemSeletedListener) activity;    
  47.         } catch (ClassCastException e) {    
  48.             throw new ClassCastException("必须实现 onItemSeletedListener");  
  49.         }    
  50.     }  
  51. }  

二  右侧内容展示

2.1  布局 right_fragment.xml  

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@android:color/background_dark"  
  6.     android:orientation="vertical"  
  7.     tools:ignore="HardcodedText,UselessParent" >  
  8.     <ScrollView   
  9.         android:scrollbars="vertical"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent">  
  12.         <TextView  
  13.             android:id="@+id/textview"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:text="请选择左侧边栏 :)"  
  17.             android:textColor="@android:color/holo_orange_dark"  
  18.             android:textSize="30sp" />  
  19.     </ScrollView>  
  20. </LinearLayout>  

2.1  自定义 RightFragment    

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

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

3.1  布局 main_layout.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"  
  6.     tools:ignore="DisableBaselineAlignment" >  
  7.     <fragment  
  8.         android:id="@+id/left_fragment"  
  9.         android:name="com.example.myfragments.LeftFragment"  
  10.         android:layout_width="70dp"  
  11.         android:layout_height="match_parent"/>  
  12.     <View   
  13.         android:layout_width="2dp"  
  14.         android:layout_height="match_parent"  
  15.         android:background="@android:color/background_dark"/>  
  16.     <fragment  
  17.         android:id="@+id/right_fragment"  
  18.         android:name="com.example.myfragments.RightFragment"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="match_parent"/>  
  21. </LinearLayout>  

3.2  MainActivity

  1. package com.example.myfragments;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.Window;  
  5. import android.view.WindowManager;  
  6. public class MainActivity extends Activity implements onItemSeletedListener{  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);     
  11.         requestWindowFeature(Window.FEATURE_NO_TITLE);     
  12.         setContentView(R.layout.main_layout);       
  13.           
  14. //        //添加  
  15. //        FragmentManager fragmentmanager=getFragmentManager();  
  16. //        FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction();  
  17. //        LeftFragment leftfragment=new LeftFragment();  
  18. //        fragmenttransaction.add(R.id.left_fragment, leftfragment);  
  19. //        fragmenttransaction.commit();  
  20. //        //删除  
  21. //        FragmentManager fragmentmanager=getFragmentManager();  
  22. //        FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction();  
  23. //        Fragment leftfragment=fragmentmanager.findFragmentById(R.id.left_fragment);  
  24. //        fragmenttransaction.remove(leftfragment);  
  25. //        fragmenttransaction.commit();  
  26. //        //替换  
  27. //        FragmentManager fragmentmanager=getFragmentManager();  
  28. //        FragmentTransaction fragmenttransaction=fragmentmanager.beginTransaction();  
  29. //        fragmenttransaction.replace(R.id.left_fragment, new LeftFragment());  
  30. //        fragmenttransaction.commit();  
  31.     }  
  32.     @Override  
  33.     public void onItemSeleted(int position) {  
  34.         RightFragment rightFragment=(RightFragment) getFragmentManager().findFragmentById(R.id.right_fragment);  
  35.           
  36.         if(rightFragment != null)  
  37.             rightFragment.update(position);  
  38.     }  
  39. }  
 

四  结果展示


      
 
    
转载请注明出处 :)http://blog.csdn.net/mkrcpp/article/details/11478011
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值