这很奇怪,我花了最后5个小时,没有任何效果。问题是这样,当我从活动a转到活动b,然后按回按钮并返回活动a时,导航抽屉不打开。 这是代码:
public abstract class DrawerActivity extends ActionBarActivity {
static LinearLayout fullLayout;
static FrameLayout actContent;
static DrawerLayout mDrawerLayout;
static ListView mDrawerList;
static ActionBarDrawerToggle mDrawerToggle;
static LinearLayout mDrawer;
List<HashMap<String, String>> mList;
SimpleAdapter mAdapter;
final private String COUNTRY = "country";
final private String FLAG = "flag";
final private String COUNT = "count";
@Override
public void setContentView(final int layoutResID) {
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null); // Your base layout here
actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);
getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame
super.setContentView(fullLayout);
makelist();
}
mTitle = (String) getTitle();
// Getting a reference to the drawer listview
mDrawerList = (ListView) findViewById(R.id.drawer_list);
// Getting a reference to the sidebar drawer ( Title + ListView )
mDrawer = (LinearLayout) findViewById(R.id.drawer);
// Each row in the list stores country name, count and flag
mList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < mCountries.length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put(COUNTRY, mCountries[i]);
hm.put(FLAG, Integer.toString(mFlags[i]));
mList.add(hm);
}
String[] from = { FLAG, COUNTRY };
int[] to = { R.id.flag, R.id.country };
mAdapter = new SimpleAdapter(this, mList, R.layout.drawer_layout, from,to);
// Getting reference to DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_navigation_drawer, R.string.drawer_open,R.string.drawer_close) {
/** Called when drawer is closed */
public void onDrawerClosed(View view) {
highlightSelectedCountry();
supportInvalidateOptionsMenu();
}
/** Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
supportInvalidateOptionsMenu();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
Log.v("this","close");
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
// Setting event listener for the drawer
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setAdapter(mAdapter);
}
public static boolean open() {
if (mDrawerLayout.isDrawerOpen(mDrawer)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
return true;
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
return true;
}
}
这是MainActivity类:
public class MainActivity extends DrawerActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//suppose this is the imageview on the actionbar
ImageView img=(ImageView)dialogg.findViewById(R.id.img);
dateup.img(new OnClickListener() {
@Override
public void onClick(View v) {
DrawerActivity.open();
}
});
}
我想注意的一件事是,mainActivity扩展了DrawerActivity类。
你能帮我解决这个问题吗?
谢谢你
答案 0 :(得分:0)
这个
尝试使用Abstract类中的方法open()并使用“mDrawerLayout”,此时我必须为null
public void onClick(View v) {
DrawerActivity.open();
}
也许你用
public void onClick(View v) {
this.open();
}
然后您将使用当前活动的开放方法
您必须在Activity
中实现onClickListener错误消息可以很好地获取有关错误的更多信息