导航片段

时间:2016-09-28 03:32:55

标签: android android-fragments

我是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>

1 个答案:

答案 0 :(得分:1)

您可以实现FragmentManager.OnBackStackChangedListener并在那里调用您的方法,具体取决于当前在后栈中的片段。据我了解,目前这些方法是通过按钮点击等触摸交互来调用的。你无法在后台改变上做到这一点,需要找出另一种方法。

您需要在活动中实施OnBackStackChangedListener。最好在onCreate中的dashboard.java中执行以下操作:

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

然后覆盖onBackStackChanged中的dashboard.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.java重构为Dashboard.java

如果您需要更多帮助,请告诉我。