布局方向发生变化而无需 Activity 交互
自从使用 Android 以来,我很好奇如何让 Android 完成有关方向更改(布局与布局区域)的所有操作。
目前,我必须将正确数量的数据库列和视图提供给游标适配器。这是正确的方法还是我错过了什么?你们是怎么做到的?
请看一下两个 SimpleCursorAdapter,我在其中提供了两个现有布局的相同布局名称(布局中一个,布局域中一个)。唯一的区别是附加的数据库列“type”和附加视图“R.id.activities_row_text3”。
这是正确的方法吗?
Cursor cursor;
SimpleCursorAdapter simpleCursorAdapter = null;
if ((cursor = db.fetchActivities(connection)) != null) {
startManagingCursor(cursor);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
simpleCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.activities_row,
cursor,
new String[] {
"name",
"time" },
new int[] {
R.id.activities_row_text1,
R.id.activities_row_text2 });
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
simpleCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.activities_row,
cursor,
new String[] {
"name",
"time",
"type" },
new int[] {
R.id.activities_row_text1,
R.id.activities_row_text2,
R.id.activities_row_text3 });
}
if (simpleCursorAdapter != null) {
setListAdapter(simpleCursorAdapter);
}
}
Since working with Android I'm curious about how I can let Android do everything in respect to orientation change (layout vs. layout-land).
Currently I have to feed the correct number of db columns and views to the cursor adapter. Is this the correct way or do I miss something? How do you guys do that?
Please have a look at the two SimpleCursorAdapter where I feed the same layout name of both existing layouts (there's on in layout and one in layout-land). The only difference is the additional db column "type" and the additional view "R.id.activities_row_text3".
Is this the correct way?
Cursor cursor;
SimpleCursorAdapter simpleCursorAdapter = null;
if ((cursor = db.fetchActivities(connection)) != null) {
startManagingCursor(cursor);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
simpleCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.activities_row,
cursor,
new String[] {
"name",
"time" },
new int[] {
R.id.activities_row_text1,
R.id.activities_row_text2 });
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
simpleCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.activities_row,
cursor,
new String[] {
"name",
"time",
"type" },
new int[] {
R.id.activities_row_text1,
R.id.activities_row_text2,
R.id.activities_row_text3 });
}
if (simpleCursorAdapter != null) {
setListAdapter(simpleCursorAdapter);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好在启动 Activity 时仅创建一次
simpleCursorAdapter
。然后,当方向发生变化时,您可以使用方法SimpleCursorAdapter.changeCursorAndColumns()
(或bindView()
)。您可能需要使列表无效才能看到更改 (notifyDataSetInvalidated()
)。为了避免在发生方向更改时重新启动 Activity,请参阅自行处理方向更改
It would be better to create
simpleCursorAdapter
only once, while starting Activity. Then you can use methodSimpleCursorAdapter.changeCursorAndColumns()
(orbindView()
) when change orientation occurs. You may need to invalidate list to see changes (notifyDataSetInvalidated()
).To avoid restarting activity when orientation change occurs, see Handling orientation changes yourself