java - 如何在 Fragment 中使用另一个 XML 文件中的 FloatingActionButton

标签 java android xml android-fragments

我想在 Fragment 类中使用 FloatingActionButton

home.xml 用于class Home extends AppCompatActivity

    <?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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.azadgh.app.myapplicationtab.Home">


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

这是 TabCost:

    public class TabCost extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tabcost, container, false);

        FloatingActionButton fab = (FloatingActionButton) rootView.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();
            }
        });
        return rootView;
    }
}

如何在 TabCost 中使用 fab

我有三个类似的选项卡 TabCost ,我想当我在任何选项卡中单击 fab 时显示不同的 Snackbar 。 我该如何修复它?

最佳答案

嗯,你可以在Fragment中使用:

FloatingActionButton fab =(FloatingActionButton)getActivity().findViewById(R.id.fab)

正如您所提到的,您使用 ViewPager 来处理 fragment 。您可以将 OnPageChangeListener() 添加到 ViewPager 并重写其方法:onPageScrollStateChangedonPageScrolledonPageSelected。 因此,当您选择任何页面(在您的情况下为 fragment )时,它将被调用,并且您可以在其中为 Snackbar 设置不同的内容。

@Override
public onPageSelected(int position) {
    String contentForSnackbar = "default string";
    switch(position){
    case 0:  
      contentForSnackbar = "first fragment";
      break;
    case 1:  
      contentForSnackbar = "second fragment";
      break;
    case 2:
      contentForSnackbar = "third fragment";
      break;
    }

    fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, contentForSnackbar, Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
}

关于java - 如何在 Fragment 中使用另一个 XML 文件中的 FloatingActionButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46012805/

相关文章:

java 8 你能将方法传递给过滤器吗

android - 我的应用程序在键盘下方显示空白

c# - 使用 System.Xml.Serialization.XmlSerializer 反序列化有效 xml 文件时存在差异

c# - 使用 XElement 处理无效的 XML 字符

Java JLabel - 添加唯一标识符

java - ArrayList的对象消失

java - JDK安装独特错误

java - 某些操作的刷新 Activity

Android Studio 未检测到 Microsoft Android 模拟器

android - 膨胀成 "this"?