安卓LinearLayout性能

3

我在布局中有这些lint警告

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

这是我的布局:

enter image description here

我的布局是用来展示数据的。这些数据几乎都以表格的形式显示,但有些“行”必须使用3或4个TextView,而其他的则有6或7个。由于我想要某些TextView具有完全相同的宽度,我已经创建了所有这些LinearLayout,并使用android:weightSum和android:layout_weight来实现我想要的效果。
由于我不能删除一些视图(它们都需要显示我的数据),我该怎么做来提高布局的性能?
我尝试过使用RelativeLayout,但是在那种情况下,我无法使用android:weightSum和android:layout_weight。
谢谢。
Favolas
编辑
抱歉。没意识到XML如此重要。
这是巨大的代码:

http://pastebin.com/0mehGx2z

更多解释。

我有一个包含近50个列的表格数据库。

我向数据库发出查询,该查询返回一个游标。结果显示在ListView上。用户选择其中一个结果,然后被“发送”到此布局,数据将显示在所有这些字段上


3
您的布局的XML格式对于我们来说更易于阅读和分析。您能否发布它,而不是树形结构? - prolink007
2
尽可能使用适配器来加载屏幕上必要的视图。这将提高性能。@prolink007,这将是巨大的.. - IAmGroot
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
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接