0

I have a main activity with a bottomnav bar, include a fragment name library, and inside library fragment I have a tablayout holds another 2 fragment like this. So the problem is, the SearchView inside the tablayout fragment item cannot be typed in. I can paste a text into it by using copy paste but the normal typing can't. Why is that and how to fix it?

(The code of main activity)


public class temp_page extends AppCompatActivity {
    BottomNavigationView bottomNav;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temp_page);           

        bottomNav = findViewById(R.id.bottomNav);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment, new home_page()).commit();
        bottomNav.setOnItemSelectedListener(new BottomNavigationView.OnItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment temp = null;
                switch(item.getItemId()){
                    case R.id.home:
                        temp = new home_page();
                        break;
                    case R.id.library:
                        temp = new library_page();
                        break;
                    case R.id.playlist:
                        temp = new playlist();
                        break;
                }getSupportFragmentManager().beginTransaction().replace(R.id.fragment, temp).commit();
                return true;
            }
        });
    }
}
(the java code from library page)
 @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tabLayout = view.findViewById(R.id.tab_layout);
        pager2 = view.findViewById(R.id.viewpager_library);

        FragmentManager fm = getActivity().getSupportFragmentManager();
        fragmentAdapter = new FragmentAdapter(fm, getLifecycle());
        pager2.setAdapter(fragmentAdapter);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                pager2.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        getActivity().getSupportFragmentManager().findFragmentById(R.id.viewpager_library);
        pager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tabLayout.selectTab(tabLayout.getTabAt(position));
            }
        });
    }

(the xml code from library page)

<?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=".library_page">
    <LinearLayout
        android:background="#3B3B3B"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        tools:context=".LibraryPage">
        <com.google.android.material.tabs.TabLayout
            android:background="#3B3B3B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tab_layout"
            app:tabIndicatorFullWidth="true"
            app:tabIndicatorGravity="bottom"
            app:tabTextColor="@color/white">
            <com.google.android.material.tabs.TabItem
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:text="BÀI HÁT"/>
            <com.google.android.material.tabs.TabItem
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="ALBUM"/>
        </com.google.android.material.tabs.TabLayout>
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewpager_library"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</FrameLayout>

(FragmentAdapter page)

public class FragmentAdapter extends FragmentStateAdapter {
    public FragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position){
            case 1:
                return new album_list();
        }
        return  new song_list();
    }

    @Override
    public int getItemCount() {
        return 3;
    }
}

The java code and xml code inside seachview page is just bare minimum to make a searchview, so I'm 100% sure they aren't the problem. (searchview 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=".song_list">
    <androidx.appcompat.widget.SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Nhập tên bài hát cần tìm" />
    <LinearLayout
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_marginTop="20dp"
            android:id="@+id/recyclerview_library_songs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</FrameLayout>
David Tran
  • 29
  • 4

1 Answers1

0

So I have figured it out, the reason why searchview don't work is because the linear layout which it was contained in has descendantFocusbility="blockDescendant", delete it and everything is fine.

David Tran
  • 29
  • 4