在家里打开片段 activity
Opening a fragment(s) in home activity
我一直在通过 Stack Overflow 进行搜索,但似乎找不到对我遇到的这个问题有用的信息。
下面我附上了我拥有的主页 Activity 的代码,以及它的 XML 文件。我似乎无法理解问题出在哪里,我不知道我做错了什么。我想显示第三个 class 的内容(例如,我的每个片段都有一个不同的片段 class,基本上我希望能够显示同一片段持有者中的所有片段主页 activity.. 在这种情况下,我试图打开片段 Bookmarks (men.bookmarks)..
大家有什么建议吗?
我没有包含该片段的代码,因为那时会有很多 classes,但如果您需要它,请告诉我 :) ..
XML file for my HomeActivity:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start" >
<include
layout="@layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer" />
<fragment
android:name="com.nooriandroid.n007.mybookmarks_v1.Fragment_Bookmarks"
android:id="@+id/fragBookmarks"
android:layout_weight="2"
android:layout_width="25dp"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
package com.nooriandroid.n007.mybookmarks_v1;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Activity_Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//--------------Variables---------------------//
private String date_pattern = "hh:mm dd-MM-yyyy";
private Fragment fragment_bookmarks;
private Fragment_Favorite fragment_favorite;
private Fragment_Category fragment_category;
private Fragment_SharedWith_me fragment_sharedWith_me;
private Fragment_Myshared fragment_myshared;
private Activity_Home homeActivity;
private Fragment_Item_Container fragment_item_container;
private User user;
private boolean frag_favorite_visible = false, frag_bookmark_visible = false, frag_category_visible = false,
frag_myshared_visible = false, frag_sharedwithme__visible = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//---------------Customized------------------//
setTitle("Home");
ActionBar ab = getSupportActionBar();
ab.setLogo(R.mipmap.ic_logolight);
ab.setDisplayUseLogoEnabled(true);
ab.setDisplayShowHomeEnabled(true);
fragment_favorite = new Fragment_Favorite();
fragment_bookmarks = new Fragment_Bookmarks();
fragment_category = new Fragment_Category();
fragment_myshared = new Fragment_Myshared();
fragment_sharedWith_me = new Fragment_SharedWith_me();
fragment_item_container = new Fragment_Item_Container();
homeActivity = this;
FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment_favorite);
fragmentTransaction.add(R.id.fragment_container, fragment_bookmarks);
fragmentTransaction.add(R.id.fragment_container, fragment_category);
fragmentTransaction.add(R.id.fragment_container, fragment_myshared);
fragmentTransaction.add(R.id.fragment_container, fragment_sharedWith_me);
frag_favorite_visible = true;
fragmentTransaction.hide(fragment_bookmarks);
fragmentTransaction.hide(fragment_category);
fragmentTransaction.hide(fragment_myshared);
fragmentTransaction.hide(fragment_sharedWith_me);
fragmentTransaction.commit();
goHomeOnclicked();
user = new User();
user = (User) getIntent().getSerializableExtra("currentuser");
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
rundUserSettings();
SimpleDateFormat format = new SimpleDateFormat(date_pattern);
String current_date = format.format(new Date());
TextView nav_textview_Date = (TextView) findViewById(R.id.textView_Date);
nav_textview_Date.setText(current_date);
TextView nameLast_textView = (TextView) findViewById(R.id.textView_UsernameLastname);
nameLast_textView.setText("" + user.getName() + " " + user.getLast_name());
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.men_newItem) {
Intent intent_NewItem = new Intent("com.nooriandroid.n007.mybookmarks_v1.Activity_NewItem");
startActivity(intent_NewItem);
} else if (id == R.id.men_bookmarks) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
fragmentTransaction.show(fragment_bookmarks);
frag_bookmark_visible = true;
this.setTitle("Bookmarks"); ///Ombytnings Zirt
fragmentTransaction.commit(); ///
} else if (id == R.id.men_category) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
frag_category_visible= true;
fragmentTransaction.show(fragment_category);
fragmentTransaction.commit();
this.setTitle("Categories ");
Toast.makeText(Activity_Home.this,"Not implemented yet.!",Toast.LENGTH_SHORT).show();
}
else if (id == R.id.men_share) {
Toast.makeText(Activity_Home.this,"Not implemented yet.!",Toast.LENGTH_SHORT).show();
} else if (id == R.id.men_sharedwithme) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
frag_sharedwithme__visible = true;
fragmentTransaction.show(fragment_sharedWith_me);
fragmentTransaction.commit();
this.setTitle("Shared with me");
Toast.makeText(Activity_Home.this,"Not implemented yet.!",Toast.LENGTH_SHORT).show();
} else if (id == R.id.men_myshared) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
frag_myshared_visible = true;
fragmentTransaction.show(fragment_myshared);
fragmentTransaction.commit();
this.setTitle("My shared");
Toast.makeText(Activity_Home.this,"Not implemented yet.!",Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void checkFragmentsStatus(FragmentTransaction fragmentTransaction){
if (frag_favorite_visible){
frag_favorite_visible = false;
fragmentTransaction.hide(fragment_favorite);
}else if (frag_bookmark_visible){
frag_bookmark_visible = false;
fragmentTransaction.hide(fragment_bookmarks);
}else if (frag_category_visible){
frag_category_visible = false;
fragmentTransaction.hide(fragment_category);
}else if (frag_myshared_visible){
frag_myshared_visible = false;
fragmentTransaction.hide(fragment_myshared);
}else if (frag_sharedwithme__visible){
frag_sharedwithme__visible = false;
fragmentTransaction.hide(fragment_sharedWith_me);
}
}
private void goHomeOnclicked(){
findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!frag_favorite_visible){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
frag_favorite_visible = true;
fragmentTransaction.show(fragment_favorite);
fragmentTransaction.commit();
homeActivity.setTitle("Home");
}
}
});
}
private boolean rundUserSettings(){
if (this.user!=null){
try {
File file = new File(user.getProfile_pic());
Uri uri = Uri.fromFile(file);
ImageView view = (ImageView) findViewById(R.id.imageView_ProfilePic);
view.setImageURI(uri);
}catch (Exception e){
Toast.makeText(Activity_Home.this,"Unable to load profile picture.!",Toast.LENGTH_SHORT).show();
}
return true;
} else {
return false;
}
}
}
DrawerLayout
应该只有两个直接children.If你想在同一个activity中替换不同的片段,在app_bar_homelayout.Then中创建一个FrameLayout你的Activity,这样做是为了替换片段。
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_layout_id,new yourFragment()).commit();
您用于在同一 activity 上添加不同片段的代码确实一团糟。您可以使用更好的方法。我在下面添加示例代码:
在Activity的布局中,添加这行代码
<FrameLayout
android:id="@+id/fragBookmarks"
android:layout_weight="2"
android:layout_width="25dp"
android:layout_height="match_parent" />
而不是:
<fragment
android:name="com.nooriandroid.n007.mybookmarks_v1.Fragment_Bookmarks"
android:id="@+id/fragBookmarks"
android:layout_weight="2"
android:layout_width="25dp"
android:layout_height="match_parent" />
现在把这个方法写在你的首页Activity添加不同的片段
public void addFragment(Fragment frag, String tag) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragBookmarks, frag, tag);
transaction.addToBackStack(getFragmentManager().getBackStackEntryCount() == 0 ? "FirstFragment" : null).commit();
}
}
此方法将帮助您随时随地向此 activity 添加任何片段。您不需要从头开始添加所有片段。就像如果现在我在 HomeFragment 上,我想在从主页单击按钮时打开 BookmarkFragment,我会写类似的东西;
btnBookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((HomeActivity) getActivity()).addFragment(new BookmarkFragment(), BookmarkFragment.class.getSimpleName()); // This will add the new fragment Bookmark fragment to the activity while adding it to backstack
}
});
现在,如果您想从 BookmarkFragment 返回到 HomeFragment,请在您的主页中添加此方法Activity
public void popFragment() {
if (getFragmentManager() == null)
return;
getFragmentManager().popBackStack();
}
并在 BookmarkFragment 中调用此函数,例如,
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((HomeActivity) getActivity()).popFragment(); // This will remove the last added fragment Bookmark fragment
}
});
我一直在通过 Stack Overflow 进行搜索,但似乎找不到对我遇到的这个问题有用的信息。 下面我附上了我拥有的主页 Activity 的代码,以及它的 XML 文件。我似乎无法理解问题出在哪里,我不知道我做错了什么。我想显示第三个 class 的内容(例如,我的每个片段都有一个不同的片段 class,基本上我希望能够显示同一片段持有者中的所有片段主页 activity.. 在这种情况下,我试图打开片段 Bookmarks (men.bookmarks)..
大家有什么建议吗?
我没有包含该片段的代码,因为那时会有很多 classes,但如果您需要它,请告诉我 :) ..
XML file for my HomeActivity:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start" >
<include
layout="@layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer" />
<fragment
android:name="com.nooriandroid.n007.mybookmarks_v1.Fragment_Bookmarks"
android:id="@+id/fragBookmarks"
android:layout_weight="2"
android:layout_width="25dp"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
package com.nooriandroid.n007.mybookmarks_v1;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Activity_Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//--------------Variables---------------------//
private String date_pattern = "hh:mm dd-MM-yyyy";
private Fragment fragment_bookmarks;
private Fragment_Favorite fragment_favorite;
private Fragment_Category fragment_category;
private Fragment_SharedWith_me fragment_sharedWith_me;
private Fragment_Myshared fragment_myshared;
private Activity_Home homeActivity;
private Fragment_Item_Container fragment_item_container;
private User user;
private boolean frag_favorite_visible = false, frag_bookmark_visible = false, frag_category_visible = false,
frag_myshared_visible = false, frag_sharedwithme__visible = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//---------------Customized------------------//
setTitle("Home");
ActionBar ab = getSupportActionBar();
ab.setLogo(R.mipmap.ic_logolight);
ab.setDisplayUseLogoEnabled(true);
ab.setDisplayShowHomeEnabled(true);
fragment_favorite = new Fragment_Favorite();
fragment_bookmarks = new Fragment_Bookmarks();
fragment_category = new Fragment_Category();
fragment_myshared = new Fragment_Myshared();
fragment_sharedWith_me = new Fragment_SharedWith_me();
fragment_item_container = new Fragment_Item_Container();
homeActivity = this;
FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment_favorite);
fragmentTransaction.add(R.id.fragment_container, fragment_bookmarks);
fragmentTransaction.add(R.id.fragment_container, fragment_category);
fragmentTransaction.add(R.id.fragment_container, fragment_myshared);
fragmentTransaction.add(R.id.fragment_container, fragment_sharedWith_me);
frag_favorite_visible = true;
fragmentTransaction.hide(fragment_bookmarks);
fragmentTransaction.hide(fragment_category);
fragmentTransaction.hide(fragment_myshared);
fragmentTransaction.hide(fragment_sharedWith_me);
fragmentTransaction.commit();
goHomeOnclicked();
user = new User();
user = (User) getIntent().getSerializableExtra("currentuser");
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
rundUserSettings();
SimpleDateFormat format = new SimpleDateFormat(date_pattern);
String current_date = format.format(new Date());
TextView nav_textview_Date = (TextView) findViewById(R.id.textView_Date);
nav_textview_Date.setText(current_date);
TextView nameLast_textView = (TextView) findViewById(R.id.textView_UsernameLastname);
nameLast_textView.setText("" + user.getName() + " " + user.getLast_name());
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.men_newItem) {
Intent intent_NewItem = new Intent("com.nooriandroid.n007.mybookmarks_v1.Activity_NewItem");
startActivity(intent_NewItem);
} else if (id == R.id.men_bookmarks) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
fragmentTransaction.show(fragment_bookmarks);
frag_bookmark_visible = true;
this.setTitle("Bookmarks"); ///Ombytnings Zirt
fragmentTransaction.commit(); ///
} else if (id == R.id.men_category) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
frag_category_visible= true;
fragmentTransaction.show(fragment_category);
fragmentTransaction.commit();
this.setTitle("Categories ");
Toast.makeText(Activity_Home.this,"Not implemented yet.!",Toast.LENGTH_SHORT).show();
}
else if (id == R.id.men_share) {
Toast.makeText(Activity_Home.this,"Not implemented yet.!",Toast.LENGTH_SHORT).show();
} else if (id == R.id.men_sharedwithme) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
frag_sharedwithme__visible = true;
fragmentTransaction.show(fragment_sharedWith_me);
fragmentTransaction.commit();
this.setTitle("Shared with me");
Toast.makeText(Activity_Home.this,"Not implemented yet.!",Toast.LENGTH_SHORT).show();
} else if (id == R.id.men_myshared) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
frag_myshared_visible = true;
fragmentTransaction.show(fragment_myshared);
fragmentTransaction.commit();
this.setTitle("My shared");
Toast.makeText(Activity_Home.this,"Not implemented yet.!",Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void checkFragmentsStatus(FragmentTransaction fragmentTransaction){
if (frag_favorite_visible){
frag_favorite_visible = false;
fragmentTransaction.hide(fragment_favorite);
}else if (frag_bookmark_visible){
frag_bookmark_visible = false;
fragmentTransaction.hide(fragment_bookmarks);
}else if (frag_category_visible){
frag_category_visible = false;
fragmentTransaction.hide(fragment_category);
}else if (frag_myshared_visible){
frag_myshared_visible = false;
fragmentTransaction.hide(fragment_myshared);
}else if (frag_sharedwithme__visible){
frag_sharedwithme__visible = false;
fragmentTransaction.hide(fragment_sharedWith_me);
}
}
private void goHomeOnclicked(){
findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!frag_favorite_visible){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
checkFragmentsStatus(fragmentTransaction);
frag_favorite_visible = true;
fragmentTransaction.show(fragment_favorite);
fragmentTransaction.commit();
homeActivity.setTitle("Home");
}
}
});
}
private boolean rundUserSettings(){
if (this.user!=null){
try {
File file = new File(user.getProfile_pic());
Uri uri = Uri.fromFile(file);
ImageView view = (ImageView) findViewById(R.id.imageView_ProfilePic);
view.setImageURI(uri);
}catch (Exception e){
Toast.makeText(Activity_Home.this,"Unable to load profile picture.!",Toast.LENGTH_SHORT).show();
}
return true;
} else {
return false;
}
}
}
DrawerLayout
应该只有两个直接children.If你想在同一个activity中替换不同的片段,在app_bar_homelayout.Then中创建一个FrameLayout你的Activity,这样做是为了替换片段。
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_layout_id,new yourFragment()).commit();
您用于在同一 activity 上添加不同片段的代码确实一团糟。您可以使用更好的方法。我在下面添加示例代码:
在Activity的布局中,添加这行代码
<FrameLayout
android:id="@+id/fragBookmarks"
android:layout_weight="2"
android:layout_width="25dp"
android:layout_height="match_parent" />
而不是:
<fragment
android:name="com.nooriandroid.n007.mybookmarks_v1.Fragment_Bookmarks"
android:id="@+id/fragBookmarks"
android:layout_weight="2"
android:layout_width="25dp"
android:layout_height="match_parent" />
现在把这个方法写在你的首页Activity添加不同的片段
public void addFragment(Fragment frag, String tag) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragBookmarks, frag, tag);
transaction.addToBackStack(getFragmentManager().getBackStackEntryCount() == 0 ? "FirstFragment" : null).commit();
}
}
此方法将帮助您随时随地向此 activity 添加任何片段。您不需要从头开始添加所有片段。就像如果现在我在 HomeFragment 上,我想在从主页单击按钮时打开 BookmarkFragment,我会写类似的东西;
btnBookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((HomeActivity) getActivity()).addFragment(new BookmarkFragment(), BookmarkFragment.class.getSimpleName()); // This will add the new fragment Bookmark fragment to the activity while adding it to backstack
}
});
现在,如果您想从 BookmarkFragment 返回到 HomeFragment,请在您的主页中添加此方法Activity
public void popFragment() {
if (getFragmentManager() == null)
return;
getFragmentManager().popBackStack();
}
并在 BookmarkFragment 中调用此函数,例如,
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((HomeActivity) getActivity()).popFragment(); // This will remove the last added fragment Bookmark fragment
}
});