3

我的布局中有这些 lint 警告

Nested weights are bad for performance (11 items)
data.xml has more than 80 views, bad for performance

这是我的布局:

在此处输入图像描述

我的布局是显示数据。这些数据几乎显示为表格,但一些“行”必须为 3 或 4 TextViews,而其他“行”必须为 6 或 7。因为我希望其中一些TextViews具有完全相同的宽度,所以我创建了所有这些LinearLayout并使用android:weightSumandroid:layout_weight实现什么我想。

由于我无法删除某些视图(它们都是显示我的数据所必需的),我可以做些什么来提高布局的性能?

我试过使用RelativeLayout,但我不能使用android:weightSumand android:layout_weight

谢谢。

法沃拉斯

编辑

对不起。不明白 XML 如此重要。

这是巨大的代码:

http://pastebin.com/0mehGx2z

还有一些解释。

我有一个数据库,其中一张表有近 50 列。

我对数据库进行查询,此查询返回一个光标。结果显示在ListView. 用户选择这些结果之一,然后“发送”到此布局,如果数据显示在所有这些字段上

4

1 回答 1

2

在我看来,您实际上是在尝试重新创建 ListView 已经做的事情。您肯定希望使用 ListView 或 GridView,因为它们是针对此任务设计和优化的。使用自定义适配器,您可以显示自定义布局。此外。我想我记得读过RelativeLayout 比 LinearLayout 快。可能是因为它不必那么努力地确定子视图的位置。只是要考虑其他事情。

Google示例展示了如何使用 Loader 动态加载 ListView。如果您已经拥有数据,那么您可以轻松去除 Loader 功能。

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 while the 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 of the layout
        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        root.addView(progressBar);

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

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

        // Prepare the loader.  Either re-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 CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        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.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);
    }

    // Called when a previously created loader is reset, making the data unavailable
    public void onLoaderReset(Loader<Cursor> loader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to 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
    }
}
于 2012-08-10T16:35:13.303 回答