2016-09-28 471 views
0

我是新来的android。我有一个有5个片段的活动,我用addTobackStack,这样当点击返回按钮时,我可以移动到前面的片段。它完成了这项工作,但我想调用在按下后退按钮时我已经替换了碎片的函数。 有什么帮助吗?导航片段

这是代码。

Dashboard.java

package com.example.sheikhspc.design; 

import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.app.Fragment; 
import android.content.Intent; 
import android.os.Build; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class dashboard extends AppCompatActivity { 

ImageView home1, subs1, noti1,settings1,date1; 
Fragment frag = null; 
TextView home, subs,noti,set,date; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_dashboard); 
    home1 = (ImageView) findViewById(R.id.home1); 
    subs1 = (ImageView) findViewById(R.id.subs1); 
    noti1 = (ImageView) findViewById(R.id.noti1); 
    settings1 = (ImageView) findViewById(R.id.settings1); 
    date1 = (ImageView) findViewById(R.id.date1); 
    frag = new dashboard1(); 
    home = (TextView)findViewById(R.id.hometv); 
    subs = (TextView)findViewById(R.id.substv); 
    noti = (TextView)findViewById(R.id.notitv); 
    set = (TextView)findViewById(R.id.settingstv); 
    date = (TextView)findViewById(R.id.datestv); 

    FragmentManager fm = getFragmentManager(); 
    fm.beginTransaction().add(R.id.container1,frag).commit(); 

    Toolbar mytoolbar = (Toolbar)findViewById(R.id.mytoolbar); 

    setSupportActionBar(mytoolbar); 
    getSupportActionBar().setTitle("Home"); 
    home.setTextColor(0xFF000000); 









} 


public void dbfragment(View view) 
{ 
    if(frag != null) 
    { 


      home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon_black) ome1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon_black)); 
home.setTextColor(0xFF000000); 
      subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_ic on)); 
      noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon)); 
     settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon)); 


     date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon)); 
     date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.d))  
     frag = new dashboard1(); 
     getSupportActionBar().setTitle("Home"); 
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

     fragmentTransaction.replace(R.id.container1,frag); 

     fragmentTransaction.commit(); 




    } 
} 
public void dates(View view) 
{ 
    if(frag != null) 
    { 
     home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon)); 
     subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon)); 
     noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon)); 
     settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon)); 
     date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon_black)); 
     date.setTextColor(0xFF000000); 
     getSupportActionBar().setTitle("Available Dates"); 
     frag = new AvailableDates(); 
     FragmentManager fm = getFragmentManager(); 

     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

     fragmentTransaction.replace(R.id.container1,frag); 
     fragmentTransaction.addToBackStack(null); 
     fragmentTransaction.commit(); 
    } 
} 

public void subscription(View view) 
{ 
    if (frag != null) 
    { 
     home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon)); 
     subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon_black)); 
     noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon)); 
     settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon)); 
     date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon)); 
     subs.setTextColor(0xFF000000); 
     getSupportActionBar().setTitle("Subscriptions"); 
     frag = new Subscription(); 
     FragmentManager fm = getFragmentManager(); 

     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

     fragmentTransaction.replace(R.id.container1,frag); 
     fragmentTransaction.addToBackStack(null); 
     fragmentTransaction.commit(); 
    } 
} 
public void noti(View view) 
{ 
    getSupportActionBar().setTitle("Notifications"); 
    if (frag != null) 
    { 
     home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon)); 
     subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon)); 
     noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon_black)); 
     settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon)); 
     noti.setTextColor(0xFF000000); 
     date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon)); 
     frag = new Notification(); 
     FragmentManager fm = getFragmentManager(); 

     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

     fragmentTransaction.replace(R.id.container1,frag); 
     fragmentTransaction.addToBackStack(null); 
     fragmentTransaction.commit(); 
    } 
} 
public void settings(View view) 
{ 
    getSupportActionBar().setTitle("Settings"); 
    if (frag != null) 
    { 
     set.setTextColor(0xFF000000); 
     home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon)); 
     subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon)); 
     noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon)); 
     settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_ion_black)); 
     date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon)); 
     frag = new Settings(); 
     FragmentManager fm = getFragmentManager(); 

     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

     fragmentTransaction.replace(R.id.container1,frag); 
     fragmentTransaction.addToBackStack(null); 

     fragmentTransaction.commit(); 
    } 
} 
public void signout(View view) 
{ 
     Intent intent = new Intent(this, Login.class); 
     startActivity(intent); 

} 

@Override 
public void onBackPressed() { 

    if (getFragmentManager().getBackStackEntryCount() > 0){ 
     getFragmentManager().popBackStack(); 
    } else { 
     super.onBackPressed(); 
    } 


} 
} 

Activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_dashboard" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 


android:background="@color/white" 
tools:context="com.example.sheikhspc.design.dashboard"> 

<include layout="@layout/toolbar" 
    android:id="@+id/mytoolbar"/> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/background" 
    android:layout_below="@id/mytoolbar"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/headrl" 
     android:paddingTop="5dp" 
     android:paddingRight="20dp" 
     android:paddingLeft="20dp" 
     android:paddingBottom="5dp" 
     android:background="@color/white" 
     tools:ignore="NotSibling"> 


<!--   <Button 
      android:text="" 
      android:background="@drawable/subscription_icon_black" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/notificationbtn" 
      android:onClick="subscription" 
      android:layout_marginLeft="10dp" 
      android:layout_marginStart="10dp" 
      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/adbtn" 
      android:layout_toEndOf="@+id/adbtn" 
      android:layout_marginTop="7dp" />--> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/home_icon_black" 
      android:onClick="dbfragment" 
      android:id="@+id/home1" 

      /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Home" 
      android:layout_below="@+id/home1" 
      android:id="@+id/hometv" 

      android:textSize="10dp" 
      android:layout_marginTop="2dp"/> 


     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/date_icon" 
      android:onClick="dates" 
      android:id="@+id/date1" 
      android:layout_marginLeft="30dp" 

      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/home1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Dates" 
      android:layout_below="@+id/date1" 
      android:textSize="10dp" 
      android:id="@+id/datestv" 
      android:layout_marginLeft="30dp" 
      android:layout_toRightOf="@+id/home1" 
      android:layout_marginTop="0dp"/> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/noti_icon" 
      android:onClick="noti" 
      android:id="@+id/noti1" 
      android:layout_marginLeft="35dp" 

      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/date1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Notifications" 
      android:layout_below="@+id/noti1" 
      android:textSize="10dp" 
      android:id="@+id/notitv" 
      android:layout_marginLeft="15dp" 
      android:layout_toRightOf="@+id/date1" 
      android:layout_marginTop="0dp"/> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/subscription_icon" 
      android:onClick="subscription" 
      android:id="@+id/subs1" 
      android:layout_marginLeft="35dp" 

      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/noti1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Subscription" 
      android:layout_below="@+id/noti1" 
      android:textSize="10dp" 
      android:layout_marginLeft="25dp" 
      android:id="@+id/substv" 
      android:layout_toRightOf="@+id/noti1" 
      android:layout_marginTop="0dp"/> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/setting_icon" 
      android:onClick="settings" 
      android:id="@+id/settings1" 
      android:layout_marginLeft="35dp" 

      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/subs1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Settings" 
      android:layout_below="@+id/noti1" 
      android:textSize="10dp" 
      android:id="@+id/settingstv" 
      android:layout_marginLeft="30dp" 
      android:layout_toRightOf="@+id/subs1" 
      android:layout_marginTop="0dp"/> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/logout_icon" 
      android:onClick="signout" 
      android:id="@+id/signout1" 
      android:layout_marginLeft="35dp" 

      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/settings1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Logout" 
      android:layout_below="@+id/noti1" 
      android:textSize="10dp" 
      android:layout_marginLeft="30dp" 
      android:layout_toRightOf="@+id/settings1" 
      android:layout_marginTop="0dp"/> 


     <!--<Button 
      android:text="" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/adbtn" 
      android:onClick="dates" 

      android:layout_marginTop="7dp" 
      android:layout_marginLeft="10dp" 
      android:background="@drawable/date_icon_black" 
      android:layout_toRightOf="@+id/dbbtn" /> 

     <Button 
      android:text="" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/dbbtn" 
      android:layout_marginTop="7dp" 

      android:background="@drawable/home_icon_black" 

      android:onClick="dbfragment" 
      android:layout_marginLeft="10dp" 

      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" />--> 

    </RelativeLayout> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/headrl" 
     > 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/Black" 
      android:id="@+id/container1"> 


     </RelativeLayout> 

    </ScrollView> 


</RelativeLayout> 

+0

您可以实现[FragmentManager.OnBackStackChangedListener](https://developer.android.com/reference/android/app/FragmentManager.OnBackStackChangedListener.html)并根据当前位于后端堆栈中的哪个片段调用您的方法。据我了解,目前这些方法通过触摸交互调用,如按钮点击。你不能在堆栈变化上做到这一点,需要找出一种替代方法。让我知道你是否需要更多的帮助。 –

+0

在哪里实现这个FragmentManager.OnBackStackChangedListener以及如何检查哪个片段位于后端堆栈中。 –

+0

确实这些方法被称为点击图像视图 –

回答

1

您可以实现FragmentManager.OnBackStackChangedListener并调用你的方法有,这取决于该片段是目前在后面堆栈。据我了解,目前这些方法通过触摸交互调用,如按钮点击。你不能在堆栈变化上做到这一点,需要找出一种替代方法。您需要在活动中执行OnBackStackChangedListener。更好地做好了在onCreatedashboard.java如下:

fragmentManager = getFragmentManager(); 
fragmentManager.addOnBackStackChangedListener(this); 

然后覆盖onBackStackChangeddashboard.java这样的:

@Override 
public void onBackStackChanged() { 
    //get the current fragment being displayed 
    Fragment fragment = fragmentManager.findFragmentById(R.id.container1); 
    if (fragment instanceof AvailableDates) { 
     //do your stuff 
    } else if (fragment instanceof Subscription) { 
     //do other stuff 
    } //and so on for each fragment 
} 

可以使不同的实现方式为您onClick方法,如这样的:

public void dates(View view) 
{ 
    if(frag != null) 
    { 
     openAvailableDates(); 
    } 
} 

然后执行openAvailableDates因此:

void openAvailableDates() { 
    home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon)); 
    subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon)); 
    noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon)); 
    settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon)); 
    date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon_black)); 
    date.setTextColor(0xFF000000); 
    getSupportActionBar().setTitle("Available Dates"); 
    frag = new AvailableDates(); 
    FragmentManager fm = getFragmentManager(); 

    FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

    fragmentTransaction.replace(R.id.container1,frag); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
} 

等等为您的其他方法,然后从onBackStackChanged调用这些新方法。

还有一个提示。命名以小写字母开头的课程并不好。最好重构dashboard.javaDashboard.java

让我知道你是否需要更多帮助。

+0

谢谢Ishita。我在上面。还有一个问题 如何在if条件中调用方法,因为每个方法都有一个View参数。 –

+0

我已经搜索过但没有收到任何信息 –

+0

这就是我说我需要找出另一种方式时的意思。如果你根本不需要在方法中使用视图,我想可以传递一个'null'。但我不知道这是否可取。无论如何,请尝试一次并看看。 –