我开始为Android编写应用程序。我的左上方Animation
图标的NavigationDrawer
有问题。当应用程序第一次启动时,它可以正常工作但是当我从NavgationDrawer
列表中选择一个项目时,它的图标保持在“打开”状态,即使`NavigationDrawer按原样关闭。
这是我的MainActivity
:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.app.ActionBar;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
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.content.res.*;
public class MainActivity extends FragmentActivity {
ActionBarDrawerToggle icon;
final String[] listContent ={"Accueil","Fiche technique","Pilotes","Ecuries"};
final String[] fragments ={
"com.mycompany.f1holo.MainPageFragment",
"com.mycompany.f1holo.FirstFragment",
"com.mycompany.f1holo.SecondFragment",
"com.mycompany.f1holo.ThirdFragment"};
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> ad = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, listContent);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView list = (ListView) findViewById(R.id.drawer);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this,
drawer,
R.drawable.navdrawer,
R.string.open,
R.string.close);
drawer.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
list.setAdapter(ad);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
@Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.mains, Fragment.instantiate(MainActivity.this, fragments[position]));
transition.commit();
}
});
drawer.closeDrawer(list);
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId())
{
case R.id.mainMenuAbout:
Toast.makeText(this, "F1 Holo", Toast.LENGTH_SHORT).show();
return true;
case R.id.mainMenuQuitter:
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
getMenuInflater().inflate(R.menu.main_about, menu);
return true;
}
}
这是我的FirstFragment
:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
public static Fragment newInstance(Context context) {
FirstFragment frag = new FirstFragment();
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.first_fragment_layout, null);
return root;
}
}
答案 0 :(得分:1)
我找到了解决方案,很明显我可以自己开始注意不要注意......
问题在于:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.mains, Fragment.instantiate(MainActivity.this, fragments[position]));
transition.commit();
}
});
drawer.closeDrawer(list);
}
});
在此OnClickListener
中,您要设置新的SimpleDrawerListener
,因此会覆盖此行:
drawer.setDrawerListener(actionBarDrawerToggle);
这会将ActionBarDrawerToogle
与DrawerLayout
断开连接,从而停止播放动画......这就是您所需要的:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.mains, Fragment.instantiate(MainActivity.this, fragments[position]));
transition.commit();
drawer.closeDrawer(list);
}
});
你为什么要这样做呢?它只会导致用户选择项目和实际更改内容之间的延迟。如果是因为性能问题 - 可能是NavigationDrawer
的关闭动画没有正确播放 - 那么做这样的事情可能是合适的,但无论如何,如果你决定实现这样做,那就这样做:
首先创建一个名为drawerItemSelection
的全局变量:
private String drawerItemSelection = null;
然后像这样实施ItemClickListener
:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
drawerItemSelection = fragments[position];
drawer.closeDrawer(list);
}
});
最后在您的onCreate()
方法中实现ActionBarDrawerToogle
,如下所示:
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, R.drawable.navdrawer, R.string.open, R.string.close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if(drawerItemSelection != null) {
FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
transition.replace(R.id.mains, Fragment.instantiate(MainActivity.this, drawerItemSelection));
transition.commit();
drawerItemSelection = null;
}
}
};
如果您有任何其他问题,请随时提出!