在我的项目中包含导航抽屉,导航抽屉中包含很少的项目,我使用片段来表示抽屉内的物品。
这是我的主要活动,我在这里包含了所有片段,用于从抽屉导航到相应的片段视图。
public class MainActivity extends NavigationActionBarLiveo
implements OnItemClickListener {
//naviagtion Drawer libarary
private HelpLiveo mHelpLiveo;
private Toolbar mToolbar;
private SessionManager session;
public void onInt(Bundle savedInstanceState) {
// Bundle bundle = getIntent().getExtras(); // String
username=bundle.getString("name"); // String
email=bundle.getString("email"); // //
this.userName.setText(username); //
this.userEmail.setText(email);
this.userPhoto.setImageResource(R.drawable.ic_rudsonlive);
this.userBackground.setImageResource(R.drawable.ic_user_background_first);
// Creating items navigation
mHelpLiveo = new HelpLiveo();
mHelpLiveo.add(getString(R.string.Home), R.mipmap.home);
mHelpLiveo.add(getString(R.string.MyGorups), R.mipmap.mygroup);
mHelpLiveo.add(getString(R.string.MyCart), R.mipmap.mycart); // mHelpLiveo.addSeparator(); // Item separator
mHelpLiveo.addSubHeader(getString(R.string.categories)); //Item subHeader
mHelpLiveo.add(getString(R.string.Mobile), R.mipmap.mobile);
mHelpLiveo.add(getString(R.string.Electronics), R.mipmap.electronics);
mHelpLiveo.add(getString(R.string.Laptops), R.mipmap.laptop);
//{optional} - Header Customization - method customHeader // View mCustomHeader =
getLayoutInflater().inflate(R.layout.custom_header_user,
this.getListView(), false); // ImageView imageView =
(ImageView) mCustomHeader.findViewById(R.id.imageView);
with(this).startingPosition(0) //Starting position in the list
.addAllHelpItem(mHelpLiveo.getHelp())
//{optional} - List Customization "If you remove these methods and the list will take his white standard
color"
//.selectorCheck(R.drawable.selector_check) //Inform the background of the selected item color
//.colorItemDefault(R.color.nliveo_blue_colorPrimary) //Inform the
standard color name, icon and counter
//.colorItemSelected(R.color.nliveo_purple_colorPrimary) //State the
name of the color, icon and meter when it is selected
//.backgroundList(R.color.nliveo_black_light) //Inform the list of
background color
//.colorLineSeparator(R.color.nliveo_transparent) //Inform the color
of the subheader line
//{optional} - SubHeader Customization
.colorItemSelected(R.color.nliveo_blue_colorPrimary)
.colorNameSubHeader(R.color.nliveo_blue_colorPrimary)
//.colorLineSeparator(R.color.nliveo_blue_colorPrimary)
//.footerSecondItem(R.string.settings, R.mipmap.ic_settings_black_24dp)
//{optional} - Header Customization
//.customHeader(mCustomHeader)
//{optional} - Footer Customization
//.footerNameColor(R.color.nliveo_blue_colorPrimary)
//.footerIconColor(R.color.nliveo_blue_colorPrimary)
//.footerBackground(R.color.nliveo_white)
.setOnClickUser(onClickPhoto)
.setOnPrepareOptionsMenu(onPrepare)
.setOnClickFooterSecond(onClickFooter)
.build();
int position = this.getCurrentPosition();
//this.setElevationToolBar(position != 0 ? 6 : 0);
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
}
@Override
public void onItemClick(int position) {
Fragment mFragment=null;
FragmentManager mFragmentManager = getSupportFragmentManager();
String title = getString(R.string.app_name);
switch (position){
**case 0:
mFragment = new Fragment_Home();
title = getString(R.string.title_home);
break;**
case 1:
mFragment=new Fragment_Mygroup();
title=getString(R.string.title_mygroup);
break;
case 2:
mFragment=new Fragment_Mycart();
title=getString(R.string.title_mycart);
break;
case 4:
mFragment=new Fragment_Mobile();
title=getString(R.string.title_mobile);
break;
case 5:
mFragment=new Fragment_Electronics();
title=getString(R.string.title_electronics);
break;
case 6:
mFragment=new Fragment_Laptops();
title=getString(R.string.title_Laptop);
break;
default:
break;
}
;
if (mFragment != null){
mFragmentManager.beginTransaction().replace(R.id.container,
mFragment).commit();
getSupportActionBar().setTitle(title);
}
//setElevationToolBar(position != 0 ? 6 : 0);
}
从抽屉我可以进入每个片段视图,每个片段包含一个recyclerview,用于列出来自webservice的详细信息,一旦点击该项目,它将重定向到一个名为Viewactvity的行为。 我的View活动的代码是
public class Viewactivity extends AppCompatActivity implements
View.OnClickListener {
TextView phone,price,email;
ImageView product;
Button wishList,orderList;
private ProgressDialog progressDialog;
private static final String TAG = Viewactivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewactivity);
phone=(TextView)findViewById(R.id.product_name);
price=(TextView)findViewById(R.id.product_price);
email=(TextView)findViewById(R.id.textView_email);
product=(ImageView)findViewById(R.id.product_image);
wishList=(Button)findViewById(R.id.button_wishlist);
orderList=(Button)findViewById(R.id.button_order);
// order & wishlist
wishList.setOnClickListener(this);
orderList.setOnClickListener(this);
//setting progressDialog
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
// Fragments home
// Getting the product details
Bundle bundle = getIntent().getExtras();
String url = bundle.getString("Image");
phone.setText(bundle.getString("Product"));
price.setText( "Price: "+bundle.getString("Price"));
// email.setText(bundle.getString("email"));
Picasso.with(getApplicationContext()).load(url).into(product);
}
/**
* function to verify login details
* */
private void Orderplacing( final String product_name) {
// Tag used to cancel the request
String tag_string_req = "req_order";
progressDialog.setMessage("Your order is placing ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_ORDERPLACING, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
Boolean error = jObj.getBoolean("error");
//String order=jObj.getString("order");
System.out.println("Your now at if"+error);
if (!error) {
// Launching home activity
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction=mFragmentManager.beginTransaction();
Fragment_Home fragment_home=new Fragment_Home();
fragmentTransaction.replace(android.R.id.content,fragment_home);
fragmentTransaction.commit();
Intent intent = new Intent(Viewactivity.this,
Fragment_Home.class);
String responseMsg = jObj.getString("response");
Toast.makeText(getApplicationContext(),
responseMsg, Toast.LENGTH_LONG).show();
System.out.println("Your" + responseMsg);
startActivity(intent);
// startActivity(intent);
}
else {
// order error
String responseMsg = jObj.getString("response");
Toast.makeText(Viewactivity.this,
"ooola kirane"+ responseMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Viewactivity.this,
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Post product to orderplacing url
Map<String, String> params = new HashMap<String, String>();
params.put("item", product_name);
return params;
}
};
// Adding request to queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
/*
function to show dialog
*/
private void showDialog() {
if (!progressDialog.isShowing())
progressDialog.show();
}
/*
function to hide dialog
*/
private void hideDialog() {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
从这个活动我使用了一个web服务api,一旦api的响应没有错误,那么我想从这个活动重定向到My Fragment_Home(Fragment)。但是当我试图运行应用程序时它崩溃了.. < / p>
答案 0 :(得分:0)
你可以尝试像这样修改
Fragment mFragment;
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction=mFragmentManager.beginTransaction();
Fragment_Home fragment_home=new Fragment_Home();
fragmentTransaction.replace(android.R.id.content,fragment_home);
fragmentTransaction.commit();
mFragment = new Fragment_Home();