SearchView 与 tabLayout 相同搜索的两个结果

问题描述

我有一个活动,其中有一个搜索视图,我可以在其中搜索在底层回收视图中显示的电影。

activity_search.xml

<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/big_stone"
tools:context=".activities.SearchActivity">


<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/toolbar"
        app:layout_scrollFlags="scroll|enteralways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">

        <androidx.appcompat.widget.SearchView
            android:id="@+id/search_view"
            style="@style/Widget.AppCompat.SearchView"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:iconifiedByDefault="false"
            app:queryBackground="@null"
            app:queryHint="@string/msg_searchView_hint_message"
            app:searchIcon="@null" />

    </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:layout_behavior="com.google.android.material.AppBarLayout$ScrollingViewBehavior"
    app:layout_constraintTop_toBottomOf="@id/appBarLayout" />

<LinearLayout
    android:id="@+id/empty_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:orientation="vertical"
    android:visibility="visible"
    app:layout_constraintTop_toBottomOf="@id/appBarLayout">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_search_empty_result_message" />

    <TextView
        android:id="@+id/empty_message_text"
        style="@style/Text_Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/msg_search_txv_empty"
        android:textColor="@color/silver" />

    <TextView
        style="@style/Text_normal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/msg_txv_empty_message_subtitle"
        android:textColor="@color/silver" />

</LinearLayout>

SearchActivity.java

public class SearchActivity extends AppCompatActivity implements OnMovieListener {

//Widgets
private RecyclerView recyclerView;
private LinearLayout empty_message;
private TextView empty_message_text;
private MovieSearchRecyclerView movieRecyclerViewAdapter;


private MovieListviewmodel movieListviewmodel;
private Networkchangelistener networkchangelistener = new Networkchangelistener();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search2);

    //Toolbar
    initToolbar();

    initWidgets();

 
    //SearchView
       SetupSearchView();

        movieListviewmodel = new viewmodelProvider(this).get(MovieListviewmodel.class);

        ConfigureRecyclerView();

       ObserveAnyChange();
}


private void initWidgets() {
    recyclerView = findViewById(R.id.recyclerView);
    empty_message = findViewById(R.id.empty_message);
    empty_message_text = findViewById(R.id.empty_message_text);
}

@Override
protected void onStart() {
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(networkchangelistener,filter);

    super.onStart();
}

@Override
protected void onStop() {
    unregisterReceiver(networkchangelistener);
    super.onStop();
}

private void initToolbar() {
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setdisplayHomeAsUpEnabled(true);
    }
}

//Observing any data change
private void ObserveAnyChange() {

    movieListviewmodel.getMovies().observe(this,new Observer<List<MovieModel>>() {
        @Override
        public void onChanged(List<MovieModel> movieModels) {
            //Observing for any data change
            if (movieModels != null) {
                empty_message.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
                movieRecyclerViewAdapter.setmMovies(movieModels);

            } else {
                empty_message.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }

        }
    });

}

//5 Initializing recyclerView & adding data to it
private void ConfigureRecyclerView() {
    movieRecyclerViewAdapter = new MovieSearchRecyclerView(this);

    recyclerView.setAdapter(movieRecyclerViewAdapter);
    recyclerView.setLayoutManager(new linearlayoutmanager(this,linearlayoutmanager.VERTICAL,false));

    //RecyclerView Pagination
    //Loading next page of api response
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView,int newState) {
            if (!recyclerView.canScrollVertically(1)) {
                //Here we need to display the next search result
                movieListviewmodel.searchNextPage();
            }
        }
    });
}

//Get data from searchview & query the api to get the results (Movies)
private void SetupSearchView() {
    final SearchView searchView = findViewById(R.id.search_view);

    searchView.setonQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            movieListviewmodel.searchMovieApi(
                    query,1
            );
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            if (TextUtils.isEmpty(newText)) {
               /* empty_message.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);*/
            } else {
                movieListviewmodel.searchMovieApi(
                        newText,1
                );

            }
            return false;
        }
    });

}

我正在尝试在同一活动中实现演员搜索,但使用带有两个标签的 tabLayout,一个用于电影,一个用于演员,然后是 tabLayout 中的两个片段。下面是新设计。

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/toolbar"
        app:layout_scrollFlags="scroll|enteralways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">

        <androidx.appcompat.widget.SearchView
            android:id="@+id/search_view"
            style="@style/Widget.AppCompat.SearchView"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:iconifiedByDefault="false"
            app:queryBackground="@null"
            app:queryHint="@string/msg_searchView_hint_message"
            app:searchIcon="@null" />

    </androidx.appcompat.widget.Toolbar>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        app:tabMode="auto"
        android:layout_height="wrap_content"
        app:tabSelectedTextColor="@color/white"
        app:tabTextColor="@color/silver"
        app:tabIndicatorColor="@color/vermilion"
        android:background="@color/cloud_burst"
        />

</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>

fragment_search_movie搜索结果的电影所在的片段)内,有一个回收器视图。

问题 如何将搜索结果发送到 fragment_search_movie,以在回收者视图中显示结果?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)