我一直在这个网站上寻找这个解决方案,但无论如何我似乎无法解决这个问题。基本上我在ActionBarActivity中创建了幻灯片菜单,当单击幻灯片菜单上的其中一个项目(从ListView构造)时,它会显示TextView的值。但是当方向改变(手机旋转)时,内容被重置。我是API级别19(KitKat)
所有代码:
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private TextView textview;
private ListView navList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navList = (ListView) findViewById(R.id.navList);
textview = (TextView) findViewById(R.id.textview);
isiElementList(); // isi elemen2 list, dan add masing2 click event.
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
}
private void isiElementList() {
String[] osArray = {"Android", "iOS", "Windows"};
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, osArray);
navList.setAdapter(mAdapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = navList.getItemAtPosition(position).toString();
textview.setText(text);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Activate the navigation drawer toggle
// Pass elemen2 ListView ke Drawer
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我试过了:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
此外:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}
没有工作,其他一些解决方案也尝试过,但没有运气。感谢
答案 0 :(得分:1)
您是否尝试将其放入您的清单xml for Activity:
<activity
android:name=".SomeActivity"
android:configChanges="orientation|screenSize|screenLayout"/>
对于更新的问题,您没有在onCreate()活动方法中初始化mDrawerToggle:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
更多信息:
http://developer.android.com/training/implementing-navigation/nav-drawer.html#ListItemClicks
答案 1 :(得分:1)
Android无法记住您在旋转设备时选择的项目。您必须让它记住该项目,并在重新创建活动时做出相应的反应。
在这个例子中,我使用了所选的项目索引,因为数据库(mAdapter的数组)没有变化 - 索引是稳定的。
private int mSelectedItem; // We remember the selected item index here until config change.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setup new layout based on current configuration.
setContentView(R.layout.activity_main);
// Load view variables after setContentView call.
navList = (ListView) findViewById(R.id.navList);
textview = (TextView) findViewById(R.id.textview);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
// Setup database.
isiElementList(); // isi elemen2 list, dan add masing2 click event.
// Recreate state if applicable.
if (savedInstanceState != null) {
// Get selected item index from saved state.
int selectedItem = savedInstanceState.getInt("selectedItem", 0);
// Do the same thing as if the item was just selected.
onSelectedItem(selectedItem);
}
}
private void isiElementList() {
String[] osArray = {"Android", "iOS", "Windows"};
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, osArray);
navList.setAdapter(mAdapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// React to item selection.
onSelectedItem(position);
}
});
}
private void onSelectedItem(int position) {
// Remember index of selected item.
mSelectedItem = position;
// Do something based on this index. Database must be ready at this point!
String text = navList.getItemAtPosition(position).toString();
textview.setText(text);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Persist selected item index across orientation changes.
outState.putInt("selectedItem", mSelectedItem);
}
没有onConfigurationChanged
,这不会让你陷入困境。