所以,我有这个奇怪的问题:我正在尝试使用包含用于滑出选择组件的ListView的DrawerLayout和一个包含特定片段的FrameLayout,具体取决于单击的ListView项。我已经在网上搜索答案,我已经阅读了书籍并观看了教程,我终于设法以一种我最终理解的方式做到这一点:很棒,你说!没那么快!
我留下了一个小小的问题虽然这让我的强迫症变得疯狂:ic_drawer图标(响应滑出的三垂直线的东西和抽屉的滑入)一旦我停止响应按下加载片段的ListView项,即。它保持在“抽屉打开”位置(部分隐藏在字面意义上)。我已尝试在代码中的各个点使用syncState()方法,但没有运气。
包含抽屉布局的Activity的代码如下。请记住,其他一切都完美无缺 - 只要ic_drawer图标在我按下listview项目并且出现相关片段时就停止响应抽屉的滑出和滑入状态:
package com.example.jsonbourne;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class DrawerActivity extends Activity {
private ActionBarDrawerToggle mDrawerToggle;
private ListView mList;
private DrawerLayout mDrawer;
private String[] listCells1 = { "Profile", "Messages", "Log In", "Register" };
private String[] listCells2 = { "Profile", "Messages", "Log Out" };
final String[] fragments1 = { "com.example.jsonbourne.ProfileFragment",
"com.example.jsonbourne.MessageFragment",
"com.example.jsonbourne.LoginFragment",
"com.example.jsonbourne.RegisterFragment" };
final String[] fragments2 = null;
public void onCreate(Bundle firebug) {
super.onCreate(firebug);
setContentView(R.layout.activity_drawer);
// setting up action bar
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
mList = (ListView) findViewById(R.id.listf);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar()
.getThemedContext(), android.R.layout.simple_list_item_1,
listCells1);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer,
R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle("title");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("title");
invalidateOptionsMenu();
}
};
mDrawer.setDrawerListener(mDrawerToggle);
mList.setAdapter(adapter);
mList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int pos, long id) {
mDrawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.frame, Fragment.instantiate(
DrawerActivity.this, fragments1[pos]));
ft.commit();
}
});
mDrawer.closeDrawer(mList);
mDrawerToggle.syncState();
}
});
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frame,
Fragment.instantiate(DrawerActivity.this, fragments1[0]));
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceStates) {
super.onPostCreate(savedInstanceStates);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
/*if (item.getItemId() == android.R.id.home) {
// Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
}*/
if (item.getItemId() == R.id.something) {
Toast.makeText(this, "Something something", Toast.LENGTH_LONG)
.show();
}
return super.onOptionsItemSelected(item);
}
}
有什么想法吗?令人惊讶的是,即使经过长时间的谷歌搜索会议,我也没有发现其他人遇到同样的问题。
答案 0 :(得分:1)
您已拨打DrawerLayout.setDrawerListener
两次,但希望原始版本保持同步。看起来您希望在Fragment
关闭后交换每个DrawerLayout
。以下是ActionBarDrawerToggle
中设置的原始onCreate
的示例。
设置Fragment
变量
private Fragment mFragment;
onCreate
@Override
public void onDrawerClosed(View view) {
...
if (mFragment != null) {
final FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.frame, mFragment).commit();
}
}
OnItemClickListener.onItemClick
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
mFragment = yourNewFragmentInstance;
mDrawer.closeDrawer(mList);
}