我正在尝试将我的所有代码从活动转换为片段,以便使用导航抽屉并最终使用一些滑动标签。
目前我有一个半工作导航抽屉,可以打开并显示一个列表。 BUt我是片段的新手并且很困惑因为它似乎每次关闭导航抽屉时都重新加载我的第一个片段并且不选择任何东西。
MainDrawer.java:
package com.beerportfolio.beerportfoliopro;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Created by Mike on 1/3/14.
*/
public class MainDraw extends FragmentActivity {
final String[] data ={"Statistics","two","three"};
final String[] fragments ={
"com.beerportfolio.beerportfoliopro.StatisticsPage",
"com.beerportfolio.beerportfoliopro.FragmentTwo",
"com.beerportfolio.beerportfoliopro.FragmentThree"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
//todo: load statistics fragment
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
@Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, Fragment.instantiate(MainDraw.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
}
});
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main,Fragment.instantiate(MainDraw.this, fragments[0]));
tx.commit();
}
}
StatisticsPage.java:
package com.beerportfolio.beerportfoliopro;
import android.app.ActionBar;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Created by Mike on 1/3/14.
*/
public class StatisticsPage extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
String url = "http://www.beerportfolio.com/app_getStatistics.php?";
String userURLComp = "u=" + userID;
url = url + userURLComp ;
Log.d("basicStats", url);
new getBasicStats(getActivity()).execute(url);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.statistics_pagelayout, container, false);
}
}
答案 0 :(得分:0)
问题是你的DrawerListener是在OnClickListener中设置的,不应该是:
@Override public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener(
new DrawerLayout.SimpleDrawerListener(){
@Override public void onDrawerClosed(View drawerView){...}
}
);
}
pos
整数的范围是这样的,每次抽屉关闭时,onDrawerClose都认为pos
的值没有改变。由于没有条件检查是否单击了某个项目,因此每次抽屉关闭时都会重新创建相同的片段(除非您单击其他项目)。
单击项目时将成员布尔值设置为true,并将成员int设置为其位置。使用布尔值来区分项目单击和抽屉关闭:
private boolean mNavItemClicked = false;
private int mNavPosition = 0;
...
@Override protected void onCreate(Bundle savedInstanceState) {
...
navList.setOnItemClickListener(new ListView.OnItemClickListener {
@Override public void onItemClick(AdapterView parent, View view, int position, long id) {
mNavItemClicked = true;
mNavPosition = position;
drawerLayout.closeDrawer(navList);
}
});
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(...) {
public void onDrawerClosed(View view) {
if (mNavItemClicked) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.main, Fragment.instantiate(MainDraw.this
, fragments[mNavPosition])
.commit();
}
mNavItemClicked = false;
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
如果您决定使用ActionBarDrawerToggle,请按照其设计指南调用活动回调方法onConfigurationChanged
和onOptionsItemSelected
。