gpt4 book ai didi

android LinearLayout 性能

转载 作者:太空狗 更新时间:2023-10-29 13:34:40 26 4
gpt4 key购买 nike

我的布局中有这些 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 个 TextViews,而其他的有 6 或 7 个。因为我希望其中一些 TextViews 具有完全相同的宽度我已经创建了所有这些 LinearLayout 并使用 android:weightSumandroid:layout_weight 来实现我想要的。

由于我无法删除某些 View (它们都是显示我的数据所必需的),我该怎么做才能提高布局的性能?

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

谢谢。

法沃拉斯

编辑

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

这是巨大的代码:

http://pastebin.com/0mehGx2z

一些更多的解释。

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

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

最佳答案

在我看来,您实际上是在尝试重新创建 ListView 已经完成的工作。您肯定希望使用 ListView 或 GridView,因为它们是为此任务设计和优化的。使用自定义适配器,您可以显示自定义布局。此外。我想我记得读过RelativeLayout is faster than LinearLayout .可能是因为它不必如此努力地确定 subview 的位置。只是要考虑其他事情。

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

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
}
}

关于android LinearLayout 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11904987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com