• Android列表视图(List View)


    Android列表视图(ListView)

     

    ListView是一个显示滚动项列表的示视图组(viewgroup),通过使用适配器(Adapter)把这些列表项自动插入到列表中。适配器比如从一个数组或是数据库查询获取到数据,然后转换每一项成为可放入到列表的视图。

     

    列表的显示需要三个元素:

    (1)    ListView:用来展示列表的view。

    (2)    适配器:用来把数据映射到ListView上。

    (3)    数据:具体的将被映射的字符串、图片或是基本组件。

     

     图1

    对于我们如何使用适配器来动态插入view,参考链接:

    http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

     

    1.      使用加载器(Using a Loader)

    为了实现异步加载数据,且为了避免在查询时阻塞APP主线程,从Android3.0开始引入CursorLoader,这是一种查询Cursor的标准方式。当CursorLoader接收到Cursor结果(查询结束),LoaderCallbacks接收到对onLoadFinished()方法的回调,此方法使用新的Cursor来更新适配器,然后列表视图显示这个结果。

     

    虽然CursorLoader的API是从Android3.0(API 11)开始引入的,但可以通过使用支持的库来使用:http://developer.android.com/tools/extras/support-library.html,但要求Android系统版本在1.6及之后的版本。

     

    public class ListViewLoader extends ListActivity
            implements LoaderManager.LoaderCallbacks<Cursor> {

        // This is the Adapter being used to display the list's data
        SimpleCursorAdapter mAdapter;

        // These are the Contacts rows that we will retrieve
        static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
                ContactsContract.Data.DISPLAY_NAME};

        // This is the select criteria
        static final String SELECTION = "((" +
                ContactsContract.Data.DISPLAY_NAME + " NOTNULL)AND (" +
                ContactsContract.Data.DISPLAY_NAME + " != ''))";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Create a progress bar to display whilethe list loads
            ProgressBar progressBar = new ProgressBar(this);
            progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.CENTER));
            progressBar.setIndeterminate(true);
            getListView().setEmptyView(progressBar);

            // Must add the progress bar to the root ofthe layout
            ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
            root.addView(progressBar);

            // For the cursor adapter, specify whichcolumns go into which views
            String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
            int[] toViews = {android.R.id.text1}; // The TextViewin simple_list_item_1

            // Create an empty adapter we will use todisplay the loaded data.
            // We pass null for the cursor, then updateit in onLoadFinished()
            mAdapter = new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_1, null,
                    fromColumns, toViews, 0);
            setListAdapter(mAdapter);

            // Prepare the loader.  Eitherre-connect with an existing one,
            // or start a new one.
            getLoaderManager().initLoader(0, null, this);
        }

        // Called when a new Loader needs to be created
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            // Now create and return a CursorLoaderthat will take care of
            // creating a Cursor for the data beingdisplayed.
            return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
                    PROJECTION, SELECTION, null, null);
        }

        // Called when a previously created loader has finished loading
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            // Swap the new cursor in.  (Theframework will take care of closing the
            // old cursor once we return.)
            mAdapter.swapCursor(data);
        }

        // Called when a previously created loader is reset, making the dataunavailable
        public void onLoaderReset(Loader<Cursor> loader) {
            // This is called when the last Cursorprovided to onLoadFinished()
            // above is about to be closed.  We needto make sure we are no
            // longer using it.
            mAdapter.swapCursor(null);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // Do something when a list item is clicked
        }
    }

     

    链接:

    Android开发者:

    http://developer.android.com/guide/topics/ui/layout/listview.html

     

    AndroidListView原理学习与优化总结

    http://mzh3344258.blog.51cto.com/1823534/889879

     

    androidListView详解

    http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

    Android的CursorLoader用法小结

    http://www.linuxidc.com/Linux/2013-05/84572.htm

     

  • 相关阅读:
    获取指定<文字行数>的<高度>是多少 TextKit
    JS 与 OC 交互
    PHP-note
    MySQL函数
    freeSWITCH之多平台测试通信
    freeSWITCH之安装
    PHP面向对象
    MySQL Optimization 优化原理
    理解常量指针与指针常量?
    InterView之PHP(2)
  • 原文地址:https://www.cnblogs.com/liang123/p/6325512.html
Copyright © 2020-2023  润新知