当前位置:操作系统 > 安卓/Android >>

Android 之 Fragment

一  左侧标题列表
     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.易做图_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;
//自定义回调函数
inte易做图ce 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 inte易做图ce. 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 po
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,