androidfragment学习笔记Word文档格式.docx

上传人:b****3 文档编号:6454801 上传时间:2023-05-06 格式:DOCX 页数:11 大小:165.27KB
下载 相关 举报
androidfragment学习笔记Word文档格式.docx_第1页
第1页 / 共11页
androidfragment学习笔记Word文档格式.docx_第2页
第2页 / 共11页
androidfragment学习笔记Word文档格式.docx_第3页
第3页 / 共11页
androidfragment学习笔记Word文档格式.docx_第4页
第4页 / 共11页
androidfragment学习笔记Word文档格式.docx_第5页
第5页 / 共11页
androidfragment学习笔记Word文档格式.docx_第6页
第6页 / 共11页
androidfragment学习笔记Word文档格式.docx_第7页
第7页 / 共11页
androidfragment学习笔记Word文档格式.docx_第8页
第8页 / 共11页
androidfragment学习笔记Word文档格式.docx_第9页
第9页 / 共11页
androidfragment学习笔记Word文档格式.docx_第10页
第10页 / 共11页
androidfragment学习笔记Word文档格式.docx_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

androidfragment学习笔记Word文档格式.docx

《androidfragment学习笔记Word文档格式.docx》由会员分享,可在线阅读,更多相关《androidfragment学习笔记Word文档格式.docx(11页珍藏版)》请在冰点文库上搜索。

androidfragment学习笔记Word文档格式.docx

ListFragments

类似于ListActivity的效果,并且还提供了ListActivity类似的onListItemCLick和setListAdapter等功能。

PreferenceFragments

类似于PreferenceActivity.可以创建类似IPAD的设置界面。

Fragments的详细使用 

首先先来看一张DEMO效果图:

左边点击时,右边的字符会与左边选中的项的字符相同。

与IPAD上的设置界面很相似,这一点是否借鉴了ipad上的UI呢?

相就的XML文件:

<

?

xml 

version="

1.0"

encoding="

utf-8"

>

LinearLayout 

xmlns:

android="

android:

layout_width="

match_parent"

layout_height="

orientation="

horizontal"

fragment 

class="

com.xuzhi.fragment.FragmentDemoActivity$TitlesFragment"

id="

@+id/titles"

layout_weight="

1"

0px"

/>

FrameLayout 

@+id/details"

background="

attr/detailsElementBackground"

/FrameLayout>

/LinearLayout>

主界面代码(己做注释):

package 

com.xuzhi.fragment;

import 

android.app.Activity;

android.app.AlertDialog;

android.app.Fragment;

android.app.FragmentTransaction;

android.app.ListFragment;

android.os.Bundle;

android.util.TypedValue;

android.view.LayoutInflater;

android.view.View;

android.view.ViewGroup;

android.widget.ArrayAdapter;

android.widget.ListView;

android.widget.ScrollView;

android.widget.TextView;

public 

class 

FragmentDemoActivity 

extends 

Activity 

{

static 

String[] 

array 

"

text1,"

 

text2"

text3"

text4"

text5,"

text6"

text7"

text8"

};

/** 

Called 

when 

the 

activity 

is 

first 

created. 

*/

@Override

void 

onCreate(Bundle 

savedInstanceState) 

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

TitlesFragment 

ListFragment 

boolean 

mDualPane;

int 

mCurCheckPosition 

0;

// 

TODO 

Auto-generated 

method 

stub

System.out.println("

Fragment-->

onCreate"

);

View 

onCreateView(LayoutInflater 

inflater, 

ViewGroup 

container,

Bundle 

onCreateView"

return 

super.onCreateView(inflater, 

container, 

savedInstanceState);

onPause() 

super.onPause();

onPause"

onStop() 

super.onStop();

onStop"

onAttach(Activity 

activity) 

super.onAttach(activity);

onAttach"

onStart() 

super.onStart();

onStart"

onResume() 

super.onResume();

onResume"

onDestroy() 

super.onDestroy();

onDestroy"

onActivityCreated(Bundle 

super.onActivityCreated(savedInstanceState);

onActivityCreted"

setListAdapter(new 

ArrayAdapter<

String>

(getActivity(),

android.R.layout.simple_list_item_1, 

array));

detailsFrame 

getActivity().findViewById(R.id.details);

mDualPane 

!

null

&

detailsFrame.getVisibility() 

== 

View.VISIBLE;

if 

(savedInstanceState 

null) 

savedInstanceState.getInt("

curChoice"

0);

//从保存的状态中取出数据

(mDualPane) 

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

showDetails(mCurCheckPosition);

onSaveInstanceState(Bundle 

outState) 

super.onSaveInstanceState(outState);

outState.putInt("

mCurCheckPosition);

//保存当前的下标

onListItemClick(ListView 

l, 

v, 

position, 

long 

id) 

super.onListItemClick(l, 

id);

showDetails(position);

showDetails(int 

index) 

index;

getListView().setItemChecked(index, 

true);

DetailsFragment 

details 

(DetailsFragment) 

getFragmentManager()

.findFragmentById(R.id.details);

(details 

null 

|| 

details.getShownIndex() 

DetailsFragment.newInstance(mCurCheckPosition);

//得到一个fragment 

事务(类似sqlite的操作)

FragmentTransaction 

ft 

.beginTransaction();

ft.replace(R.id.details, 

details);

//将得到的fragment 

替换当前的viewGroup内容,add则不替换会依次累加

ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

//设置动画效果

mit();

//提交

else 

new 

AlertDialog.Builder(getActivity()).setTitle(

android.R.string.dialog_alert_title).setMessage(

array[index]).setPositiveButton(android.R.string.ok,

null).show();

/**

作为界面的一部分,为fragment 

提供一个layout

@author 

terry

*

Fragment 

newInstance(int 

DetailsFragment();

args 

Bundle();

args.putInt("

index"

index);

details.setArguments(args);

details;

getShownIndex() 

getArguments().getInt("

(container 

null)

null;

ScrollView 

scroller 

ScrollView(getActivity());

TextView 

text 

TextView(getActivity());

padding 

(int) 

TypedValue.applyDimension(

TypedValue.COMPLEX_UNIT_DIP, 

4, 

getActivity()

.getResources().getDisplayMetrics());

text.setPadding(padding, 

padding, 

padding);

scroller.addView(text);

text.setText(array[getShownIndex()]);

scroller;

}

注意:

1.如果你想在Fragment里面创建menu,则必须在onCreate的时候设置让它可以存在optionMenu才可以创建,代码为:

setHasOptionsMenu(true);

之后的操作即可以像平常Android的menu用法一样,代码为:

onCreateOptionsMenu(Menu 

menu, 

MenuInflater 

inflater) 

super.onCreateOptionsMenu(menu, 

inflater);

menu.add("

Menu 

1a"

).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

1b"

onOptionsItemSelected(MenuItem 

item) 

Toast.makeText(getActivity(), 

i

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2