底部导航视图在片段上。

huangapple go评论103阅读模式
英文:

BottomNavigationView on fragment

问题

我使用了底部导航视图(Bottom Navigation View),在一个片段(Fragment)上它的工作不正常,问题在于没有过渡效果,只显示了第一个选定的项目。

navigationView.setSelectedItemId(R.id.message_menu_Friend);

我的代码:

public class MessageFragment extends Fragment {

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

        BottomNavigationView navigationView = rootView.findViewById(R.id.top_navigation_bar);
        navigationView.setOnNavigationItemSelectedListener(topNav);
        navigationView.setSelectedItemId(R.id.message_menu_Friend);
        getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M, new FriendFragment()).commit();

        return rootView;
    }

    private BottomNavigationView.OnNavigationItemSelectedListener topNav = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedItem = null;
            switch (item.getItemId()) {
                case R.id.message_menu_message:
                    selectedItem = new MessagesFragment();
                    break;
                case R.id.message_menu_Friend:
                    selectedItem = new FriendFragment();
                    break;
            }
            assert selectedItem != null;
            getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M, selectedItem).commit();
            return true;
        }
    };
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MessageFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_marginTop="112dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/hostFragment_M">

        </FrameLayout>

        <EditText
            android:id="@+id/editTextTextPersonSearch"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="@color/White"
            android:backgroundTint="@color/White"
            android:drawableEnd="@drawable/ic_baseline_search_24"
            android:drawablePadding="8dp"
            android:ems="10"
            android:hint="@string/search"
            android:inputType="textPersonName"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/top_navigation_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/White"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonSearch"
            app:menu="@menu/message_menu" />
      </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

当我点击底部导航时,如何使片段显示出来?

英文:

i use Bottom Navigation View and on a fragment its not work fine the problem is there is no transition its only display the first selected

navigationView.setSelectedItemId(R.id.message_menu_Friend);

my code

public class MessageFragment extends Fragment {

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

    BottomNavigationView navigationView = rootView.findViewById(R.id.top_navigation_bar);
    navigationView.setOnNavigationItemSelectedListener(topNav);
    navigationView.setSelectedItemId(R.id.message_menu_Friend);
    getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M,new FriendFragment()).commit();
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_message, container, false);
}


private BottomNavigationView.OnNavigationItemSelectedListener topNav = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedItem = null;
        switch (item.getItemId()) {
            case R.id.message_menu_message:
                selectedItem = new MessagesFragment();
                break;
            case R.id.message_menu_Friend:
                selectedItem = new FriendFragment();
                break;

        }
        assert selectedItem != null;
        getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M,selectedItem).commit();
        return  true ;
    }
};}

Xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
tools:context=&quot;.MessageFragment&quot;&gt;



&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;


&lt;FrameLayout android:layout_marginTop=&quot;112dp&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:id=&quot;@+id/hostFragment_M&quot;&gt;

&lt;/FrameLayout&gt;

&lt;EditText
    android:id=&quot;@+id/editTextTextPersonSearch&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;56dp&quot;
    android:background=&quot;@color/White&quot;
    android:backgroundTint=&quot;@color/White&quot;
    android:drawableEnd=&quot;@drawable/ic_baseline_search_24&quot;
    android:drawablePadding=&quot;8dp&quot;
    android:ems=&quot;10&quot;
    android:hint=&quot;@string/search&quot;
    android:inputType=&quot;textPersonName&quot;
    android:paddingStart=&quot;15dp&quot;
    android:paddingEnd=&quot;15dp&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;com.google.android.material.bottomnavigation.BottomNavigationView
    android:id=&quot;@+id/top_navigation_bar&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:background=&quot;@color/White&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/editTextTextPersonSearch&quot;
    app:menu=&quot;@menu/message_menu&quot;
    /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/FrameLayout&gt;

how can i make it display the fragment when i click on the bottom navigation

答案1

得分: 1

删除这行代码:navigationView.setSelectedItemId(R.id.message_menu_Friend);,并且将getChildFragmentManager()改为getSupportFragmentManager()。另外,在onCreate方法中,只需返回rootView,而不是重新进行布局填充。

英文:

Remove the line navigationView.setSelectedItemId(R.id.message_menu_Friend);, and also change getChildFragmentManager() to getSupportFragmentManager(). Also, in the onCreate method, just return rootView instead of inflating again.

huangapple
  • 本文由 发表于 2020年8月28日 22:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63636031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定