用法:1、列举已阅读的section, artice, class, lesson等标题。2、检索
Common Layouts
LinearLayout会自动创建scroll bar。
Building Layouts with an Adapter
ArrayAdapter 用法//1、准备数据,创建 Adpater String[] myStringArray = getMyStringArray(); ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myStringArray); //2、(可选) /*ArrayAdapterm默认创建TextView,显示字符串。要显示别的东西,例如image, bool,需子类化ArrayAdapter,重定义 ArrayAdapter.getView()*/ //3、绑定adpater和AdapterView ListView listView = (ListView) findViewById(R.id.listview); listView.setAdapter(adapter);SimpleCursorAdapter 用法
//1、得到Cursor(也可先不获取cursor,用null,等到数据异步加载完成后再,再用changeCursor()设置cursor) Cursor cursor = db.query(...); String[] columns = {"name", "phone"}; String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; int[] toViews = {R.id.display_name, R.id.phone_number}; //2、创建Adapter SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.person_name_and_number, cursor, fromColumns, toViews, 0); //3、ViewBinder(可选)若有SimpleCursorAdapter不支持的View,可创建 // SimpleCursorAdapter.ViewBinder,负责将Cursor中的column,绑定到该view。 // binder也可以改变SimpleCursorAdapter默认绑定方式。 SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() { public boolean setViewValue(View view, Cursor cursor, int columnIndex) { /*返回false,表示这里没有处理,还是用默认的方式。返回true,表示使用这里的方式*/ String columnName = cursor.getColumnName(columnIndex); if(columnName.equals("something")) { SomeView sv = (SomeView)view; //get data from cursor, and deliver to view return true; } return false; }}; adapter.setViewBinder(binder); //4、setAdapter: 绑定到ListView ListView listView = getListView();listView.setAdapter(adapter); //数据改变后,调用ArrayAdapter.notifyDataSetChanged(),刷新数据显示。 // Create a message handling object as an anonymous class. private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { // Do something in response to the click } }; listView.setOnItemClickListener(mMessageClickedHandler); //5、(此处代码在其他位置) ListView lv = getListView(); lv.changeCursor(cursor); //旧的cursor会被关闭
Accessing Resources
Loaders
基本功能:异步加载数据、数据更新后自动重新查询、configuration change之后,自动重新连接cursor。
Dialogs
Dialog是基类一般不用,选用: 1、AlertDialog 2、DialogFragment
DialogFragment更好。不要用Progress Dialog (用 ProgressBar)
要使用fragment来管理dialog,需要确保activity是继承自 FragmentActivity(Android 3.0以下版本,高于3.0的还是用标准Activity类)
Creating a Dialog Fragment
用法:1、继承DialogFragment 2、定义 onCreateDialog() [根据需要定义其他callback] 3、新建dialog fragment, 调用 show()
public class FireMissilesDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // FIRE ZE MISSILES! } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Create the AlertDialog object and return it return builder.create(); } }
Building an Alert Dialog
AlertDialog分为三个区域:1、title(content area很小时常常不用) 2、content area 3、button bar(3 buttons at most)
用AlertDialog.Builder来创建dialog:1、new AlertDialog.Builder() 2、builder.setXXX() 3、AlertDialog dialog = builder.create()
// 1. Instantiate an AlertDialog.Builder with its constructor AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // 2. Chain together various setter methods to set the dialog characteristics builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title); // 3. Get the AlertDialog from create() AlertDialog dialog = builder.create();Building an Alert Dialog -> Adding Buttons 按钮类型:Positive、Negative、Neutral
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User clicked OK button } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Set other dialog properties ... AlertDialog dialog = builder.create();
setItems(), setSingleChoiceItems(), setMultiChoiceItems()
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.pick_color); .setItems(R.array.colors_array, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // The 'which' argument contains the index position // of the selected item } });注:设置list除了setItems() 也可用 setAdapter()
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { mSelectedItems = new ArrayList(); // Where we track the selected items AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.pick_toppings) // Specify the list array, the items to be selected by default (null for none), // and the listener through which to receive callbacks when items are selected .setMultiChoiceItems(R.array.toppings, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { mSelectedItems.add(which); } else if (mSelectedItems.contains(which)) { mSelectedItems.remove(Integer.valueOf(which)); } } }) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // User clicked OK, so save the mSelectedItems results somewhere // or return them to the component that opened the dialog ... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { ... } }); return builder.create(); }
Creating a Custom Layout
步骤:按照以上AlertDialog.Builder用法, 将setItems() 改为setView() 设置custom layout。
该layout只占用content area,不占用title, button bar。
一般需要用LayoutInflater,加载layout
public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because it is going in the dialog layout builder.setView(inflater.inflate(R.layout.dialog_signin, null)) .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // sign in the user ... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { LoginDialogFragment.this.getDialog().cancel(); } }); return builder.create(); }
Passing Events Back to the Dialog's Host
方法:1、在DialogFragment中,设计一个interface,定义callback 2、host 实现这个接口及callback
3、创建DialogFragment时,想办法让dialog得到host引用(例如,通过Fragment.onAttach() 或 getActivity())。
4、点击事件的代码通过接口访问host component,把对象传过去
注:其实就是让dialog引用创建它的host(跟以前的做法一样),因为使用了interface,比较灵活。
注:没有特别的API,属编码设计技巧,看原文。
Showing a Dialog
方法:1. new DialogFragment -> 2.show()
提示:可以通过 FragmentActivity.getSupportedFragmentManager() or Fragment.getFragmentManager() 获得 FragmentManager
调用show()之后,这个fragment 其实被 FragmentManager管理起来了(会被自动删除)。不考虑复用以前的fragment。
public void confirmFireMissiles() { DialogFragment newFragment = new FireMissilesDialogFragment(); newFragment.show(getSupportFragmentManager(), "missiles"); }Showing a Dialog Fullscreen or as an embeded Fragment
让一部分UI既作为dialog,又作为 fullscreen / embeded fragment
//TODO
原文链接
Showing an activity as a dialog on large screens
(正好与上面相反) 把activity作为dialog,只需把activity的theme设为 Theme.Holo.DialogWhenLarge 或者Theme.Holo.Dialog,不用写代码。
Theme.Holo.DialogWhenLarge 只有在屏幕大的设备上运行时,activity作为dialog显示。
Theme.Holo.Dialog 一律将activity作为dialog。
<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >
Dismissing a Dialog
涉及DialogFragment的几个函数: dismiss() onDismiss() cancel() onCancel()
Pickers
picker就是下面选时间和日期的widget
Creating a Time Picker
跟Dialogs文档中创建对话框的方法完全一样,要求:
1、在onCreateDialog()中返回 DatePickDialog / TimePickerDialog。
2、host实现一个TimePickerDialog.OnTimeSetListener 接口,响应dialog完成时的callback。
原文链接(注:演示代码是fragment自己实现那个接口的,这个意义不大,要像Dialogs中的那样,让host实现这个接口。fragment通过onAttach()获得host)
Creating a Date Picker
同上
(1、概述、解释、介绍、原理、工作机制等内容一概省略。2、用法、注意事项。3、来源:Android Developers)
备注:csdn编辑器不使用复制粘贴,格式很容易混乱,经常自动插入一些tag。
为什么英文难理解,个人分析得出的原因:
1、过度追求准确,强调语法:第一、三人称。。。。 单数复数。。。时态。。。。
2、主次不清,重要信息常位于句子后半部。。。常把重要信息作为定语从句,前半句显得毫无意义没多少信息。
3、常常动词名词不分,动词常常直接作为名词,表示抽象的概念。
4、习语、短语多,从字面上无法直接理解涵义,这些短语的真正含义与单词本身含义相差甚远。