【问题标题】:Delete FloatingActionButton from Settings Page Only仅从设置页面中删除 FloatingActionButton
【发布时间】:2016-12-15 01:28:04
【问题描述】:

所以我最近在我的应用中实现了一个导航抽屉。除了导航抽屉,我还实现了菜单项来访问设置等选项。但是,我想在所有导航抽屉页面上保留这个浮动操作按钮,但我不想将它放在从菜单访问的设置页面上。这是我拥有的当前代码。

为导航栏创建。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav_drawer);
    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();
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
        }
    });

    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);
}

那么这是app_bar_nav_drawer.xml中浮动操作按钮本身的代码

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.jamessingleton.chffrapi.NavDrawerActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_nav_drawer" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_menu_share"/>

</android.support.design.widget.CoordinatorLayout>

这是 settings_layout.xml 的代码

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


    <TextView
        android:text="Use Cell Data to retrieve driving data:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="18sp"
        android:textColor="@color/cast_expanded_controller_background_color"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <RadioGroup
        android:layout_width="89dp"
        android:layout_height="69dp"
        android:weightSum="1"
        android:layout_below="@+id/textView">

        <RadioButton
            android:text="Enable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton7"
            android:layout_weight="1" />

        <RadioButton
            android:text="Disable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioButton6"
            android:layout_weight="1"
            android:checked="true" />
    </RadioGroup>


</RelativeLayout>

仅供参考,我只想保留菜单页面,而不是设置页面。如果我可以添加更多有用的代码,请告诉我。

整个导航抽屉活动

    public class NavDrawerActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_drawer);
        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();
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
            }
        });

        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);
    }


    @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.nav_drawer, menu);
        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();
        FragmentManager fragmentSettingsManager = getFragmentManager();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            fragmentSettingsManager.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();
            setTitle(R.string.action_settings);
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        FragmentManager fragmentManager = getFragmentManager();
        if (id == R.id.nav_first_layout) {
            fragmentManager.beginTransaction().replace(R.id.content_frame, new FirstFragment()).commit();
            setTitle(R.string.speed_graph);
        } else if (id == R.id.nav_second_layout) {
            fragmentManager.beginTransaction().replace(R.id.content_frame, new SecondFragment()).commit();
            setTitle(R.string.drive_player);
        } else if (id == R.id.nav_third_layout) {
            fragmentManager.beginTransaction().replace(R.id.content_frame, new ThirdFragment()).commit();
            setTitle(R.string.google_maps);
        } else if (id == R.id.nav_share) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Go Check Out All Driving Data in the Play Store!");
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

【问题讨论】:

  • 请更新导航项选择方法的代码,必须在此处进行更改。
  • @MohammedAtif,我添加了我选择设置按钮的位置。我认为这就是您要求的正确吗?
  • 是的,我将通过更改您的代码来发布答案

标签: android application-settings floating-action-button navigation-drawer


【解决方案1】:

您只能在应该隐藏的那些活动中将“fab.setVisibility(View.GONE)”添加到 OnCreate

希望这有帮助

【讨论】:

  • 非常糟糕的建议。他会浪费时间用你的程序解决空指针异常。
  • 如果他在 LayoutInflater 之后使用它应该可以正常工作
  • 您必须记住的一件事是fab 在主类中是变量。可以从其他类访问或修改它,但绝对不推荐。
  • 但是如果按钮存在于每个 Activity 中,他可以在任何 Activity 中使用相同的FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  • 我建议您再次阅读导航抽屉实现。我们最好使用 Fragments 作为抽屉,并使用单个 FAB 覆盖在由 main Activity 控制的所有 Fragments 上。
【解决方案2】:

这是我找到的最适合我的解决方案

@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();
            FragmentManager fragmentSettingsManager = getFragmentManager();
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                fragmentSettingsManager.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();
                setTitle(R.string.action_settings);
                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setVisibility(View.GONE);
            }
            return super.onOptionsItemSelected(item);
        }

        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();
            FragmentManager fragmentManager = getFragmentManager();
            if (id == R.id.nav_first_layout) {
                fragmentManager.beginTransaction().replace(R.id.content_frame, new FirstFragment()).commit();
                setTitle(R.string.speed_graph);
                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setVisibility(View.VISIBLE);
            } else if (id == R.id.nav_second_layout) {
                fragmentManager.beginTransaction().replace(R.id.content_frame, new SecondFragment()).commit();
                setTitle(R.string.drive_player);
                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setVisibility(View.VISIBLE);
            } else if (id == R.id.nav_third_layout) {
                fragmentManager.beginTransaction().replace(R.id.content_frame, new ThirdFragment()).commit();
                setTitle(R.string.google_maps);
                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setVisibility(View.VISIBLE);
            } else if (id == R.id.nav_share) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "Go Check Out All Driving Data in the Play Store!");
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
            }

【讨论】:

  • 从设置页面返回正常页面时是否有效?
  • @MohammedAtif 是的,因为每个都有View.VISIBLE。我唯一遇到的问题是返回通过登录屏幕时进入的原始页面,但在实施此操作之前这是一个问题。
  • 这就是我所说的。只有当您从抽屉中选择页面时,您的 fab 按钮才会可见。如果您在设置页面中按返回按钮,它将不可见。尝试我提到的解决方案。它将为您提供很多帮助,并优化应用程序以获得更好的性能。在您的解决方案中,您创建了大量不必要的对象,最终也会降低您的应用程序的速度。
  • 嗯,目前后退按钮会将我带到登录屏幕,所以我需要解决这个问题。
  • 在这种情况下,您需要在onBackPressed() 方法中添加更多条件检查
【解决方案3】:

最佳做法是使用接口处理此类情况。 所以先创建一个接口。

FabVisibilityController.java

public interface FabVisiblityController{
   toggleVisibility(boolean visibility);
}

然后在你的主课中

NavigationDrawerActivity.java

public class NavigationDrawerActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, FabVisibilityController{
    //write this code at the end of the class
    @override
    toggleVisibility(boolean visibility){
         if(visibility)
             fab.setVisibility(View.VISIBLE);
         else
             fab.setVisibility(View.GONE);
    }
}

然后在您的设置片段中

public class SettingsFragment extends Fragment{
    Context context;
    FabVisibilityController controller;
    //then in onAttach method
    @override
    onAttach(Context context){
        this.context = context;
        if(context instanceof FabVisibilityController)
              controller = (FabVisibilityController) context;
    }

    //then in onResume Method
    @override
    onResume(){
        super.onResume();
        //this will hide FAB whenever settings page is called
        controller.toggleVisibility(false);
    }
}

在您的其他片段中执行相同操作,但唯一不同的是,应在 onResume() 中调用 controller.toggleVisibility(true) 以使 FAB 可见。

【讨论】:

  • 我会将“其他”放在什么位置?工厂的id?
  • 不,您剩余抽屉物品的 ID。
  • 设置以外的任何东西都应该有fab.setVisibility(View.VISIBLE),否则当您进入其他页面时,您的fab按钮将不会重新出现。
  • 但是我选择的菜单项与选择设置按钮是分开的。此外,它在 fab.setVisibility(View.VISIBLE); 中以红色突出显示 fab;
  • 我已经添加了整个抽屉活动,所以你可以看看我的意思。
猜你喜欢
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多