在android中使用viewpager的带有多个片段的SearchView

Posted

技术标签:

【中文标题】在android中使用viewpager的带有多个片段的SearchView【英文标题】:SearchView with multiple fragments using viewpager in android 【发布时间】:2015-12-22 04:45:07 【问题描述】:

我想用 viewPager 中的多个片段来实现 SearchView。所有片段都包含列表,我想过滤这些列表并创建一个新的 ListView,它根据它所属的片段对结果进行分类。

但我的 searchView 无法处理一个片段。

这是我的主要活动:

package com.codeon.directory;

import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import com.codeon.directory.fragments.FiveFragment;
import com.codeon.directory.fragments.FourFragment;
import com.codeon.directory.fragments.OneFragment;
import com.codeon.directory.fragments.SixFragment;
import com.codeon.directory.fragments.ThreeFragment;
import com.codeon.directory.fragments.TwoFragment;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Nikhil Jain on 21-Sep-15.
 */
public class TabEffect extends AppCompatActivity 

    private TabLayout tabLayout;
    private ViewPager viewPager;
    public String pic[] = "1","2","3","1","2","3","1","2","3","1","2","3","1","2","3";
    public String state[] = "A","B","C";
    public String city[] = "P","Q","R";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tablayout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                onBackPressed();
            
        );

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.tab, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        return true;
    

    private void setupViewPager(ViewPager viewPager) 
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        Bundle bundle = new Bundle();
        bundle.putStringArray("pic", pic);
        bundle.putStringArray("district", city);
        bundle.putStringArray("state", state);
        adapter.addFragment(new OneFragment(), "ONE FRAGMENT", bundle);
        adapter.addFragment(new TwoFragment(), "TWO FRAGMENT", bundle);
        adapter.addFragment(new ThreeFragment(), "THREE FRAGMENT", bundle);
        adapter.addFragment(new FourFragment(), "FOUR FRAGMENT", bundle);
        adapter.addFragment(new FiveFragment(), "FIVE FRAGMENT", bundle);
        adapter.addFragment(new SixFragment(), "SIX FRAGMENT", bundle);
        viewPager.setAdapter(adapter);
    

    class ViewPagerAdapter extends FragmentPagerAdapter 
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) 
            super(manager);
        

        @Override
        public Fragment getItem(int position) 
            return mFragmentList.get(position);
        

        @Override
        public int getCount() 
            return mFragmentList.size();
        

        public void addFragment(Fragment fragment, String title, Bundle args) 
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
            fragment.setArguments(args);
        

        @Override
        public CharSequence getPageTitle(int position) 
            return mFragmentTitleList.get(position);
        
    

这是我的第一个片段:

package com.codeon.directory.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.codeon.directory.R;
import com.codeon.directory.RecyclerAdapter;
import com.codeon.directory.customadapters.Listitems_new;

import java.util.ArrayList;

/**
 * Created by Nikhil Jain on 21-Sep-15.
 */
public class OneFragment extends Fragment 

    ListView list;
    private ArrayAdapter<String> listAdapter ;

    public OneFragment() 
        // Required empty public constructor
    

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        RecyclerView rootView = (RecyclerView) inflater.inflate(R.layout.fragment_one, container, false);
        // Inflate the layout for this fragment
        Bundle extras = getArguments();
        String pic[] = extras.getStringArray("pic");
        String state[] = extras.getStringArray("state");
        Log.e("VALUE",pic[0]+pic[1]+pic[2]);

        rootView.setLayoutManager(new LinearLayoutManager(rootView.getContext()));
        //listAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, pic);
        rootView.setAdapter(new RecyclerAdapter(pic));
        return rootView;
    

我的搜索活动:

package com.codeon.directory;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
  * Created by Nikhil Jain on 21-Sep-15.
*/
public class SearchActivity extends Activity 
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) 
        String query = intent.getStringExtra(SearchManager.QUERY);
        doMySearch(query);
        Log.e("QUERY", query);
    


private void doMySearch(String query) 



这是我的 AndroidManifest.xml:

<activity
        android:name=".TabEffect"
        android:label="@string/tablayout"
        android:theme="@style/AppTheme" >
    </activity>
    <activity android:name=".SearchActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@layout/searchable"/>
    </activity>

这是我的可搜索布局:

<?xml version="1.0" encoding="utf-8"?>
<searchable
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"
    android:layout_
    android:layout_/>

简单来说,我想像在 Whatsapp 中一样实现 searchView。

【问题讨论】:

你找到解决办法了吗? 你弄明白了吗? 找到解决方案了吗? 问题的结构很好。有什么解决方案吗?! 你找到解决办法了吗? 【参考方案1】:

@Chintan 是对的。我们需要使用 ViewModel。 关注this。

    创建 SearchViewModel:

'''

private MutableLiveData<String> query= new MutableLiveData<String>();

public void setQuery(String queryData)

    query.setValue(queryData);


public LiveData<String> getQuery() 
    return query;

'''

    搜索父片段

'''

public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) 
    inflater.inflate(R.menu.search_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);

    final MenuItem myMenuItem = menu.findItem(R.id.search_icon);

    searchViewModel = new ViewModelProvider(requireActivity()).get(SearchViewModel.class);

    searchView = (SearchView) myMenuItem.getActionView();
    searchView.setQueryHint("Search");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
        @Override
        public boolean onQueryTextSubmit(String query) 
            return false;
        

        @Override
        public boolean onQueryTextChange(String newText) 
            searchViewModel.setQuery(newText);
            return false;
        
    );

'''

    对于每个子片段(选项卡)

'''

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) 
    super.onViewCreated(view, savedInstanceState);
    searchViewModel = new ViewModelProvider(requireActivity()).get(SearchViewModel.class);
    searchViewModel.getQuery().observe(getViewLifecycleOwner(), new Observer<String>() 
        @Override
        public void onChanged(String s) 
            if (s!=null)
            adapter.getFilter().filter(s);
        
    );

'''

单个查询将适用于所有选项卡。) 我研究了很长时间,但是这个解决方案对我有用。

【讨论】:

【参考方案2】:

我得到了我的片段列表,其中一个 Searchview 使用以下代码工作.. 将代码放在你想要的每个片段中

 @Override
public void setUserVisibleHint(boolean isVisibleToUser) 
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser)
    
       search_bar.setOnQueryTextListener(this);
        if(listAdapter!=null)
            listAdapter.getFilter().filter(search_bar.getQuery());
    

截图:

【讨论】:

【参考方案3】:

以下提示可能对您有所帮助

public class AnyActivity extends AppCompatActivity private ListenFromActivity activityListener1;

然后用单独的文件定义接口

public interface ListenFromActivity void doSearchInFragment(String SearchKey);

现在在你的片段中

    public class Connections extends Fragment implements ListenFromActivity 
     
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    
      ((AnyActivity)Objects.requireNonNull(getActivity())).setActivityTab1Listener(Connections.this);

    
    @Override
    public void doSearchInFragment(String SearchKey) 
    //pass to any network or local call.
    

【讨论】:

【参考方案4】:

如果您想搜索所有片段的查询,必须使用 ViewmodelLivedata。 在 Livedata 中设置您的查询,然后在您的 Livedata 中观察您的所有片段。

viewPager 端的这段代码

val searchLiveData = MutableLiveData<String>()
searchLiveData.postValue("query")

所有片段中的这段代码

mainViewModel.searchLiveData.observe(viewLifecycleOwner, Observer  query ->
     //Add your code
)

//1
val liveData: MutableLiveData<Any>()
//2
liveData.value = "Hello"
//3
liveData.observe(this, Observer  
  // Update UI  
)

【讨论】:

【参考方案5】:

很好的答案Любовь,真的帮助我。ViewModel 非常适合片段。 所以如果你想从 MainActivity 触发搜索。 您需要创建: 1.ViewModel

public class SearchViewModel extends ViewModel 
public   MutableLiveData<String> query = new MutableLiveData<String>();
public void setQuery(String queryData) 

    query.setValue(queryData);

public LiveData<String> getQuery() 

    return query;

2.在要触发和执行搜索的活动中创建它的实例。

searchViewModel = new ViewModelProvider(this).get(SearchViewModel.class);
SearchView searchView = (SearchView) bottomAppBar.getMenu().findItem(R.id.search).getActionView();
                    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
                        @Override
                        public boolean onQueryTextSubmit(String query) 
                            Log.i("onQueryTextSubmit", query);
                            return false;
                        

                        @Override
                        public boolean onQueryTextChange(String newText) 
                            searchViewModel.setQuery(newText);
                            Log.i("onQueryTextChange", newText);
                            return false;
                        
                    );

3.在片段中创建 ViewModel 的实例以观察它在 mainActivity 中的变化(这样你就可以将数据从 Activity 传递到片段)

searchViewModel = new ViewModelProvider(getActivity()).get(SearchViewModel.class);
    searchViewModel.getQuery().observe(getViewLifecycleOwner(), new Observer<String>() 
        @Override
        public void onChanged(String s) 
            if (s != null) 
                Log.i("onChanged",s);
                adapter.getFilter().filter(s);
                Log.i("filter",s);
            
        
    );

4.如果它的字符串数组,getFilter 现在如何使用它,则在您的适配器中覆盖并实现自定义 getFilter(仅当您为其提供自定义对象时)。

 @Override
public Filter getFilter() 
    if (filter == null) 
        filter = new SearchFilter();
    
    return filter;

私有类 SearchFilter 扩展了 Filter

    @Override
    protected FilterResults performFiltering(CharSequence s) 

        FilterResults result = new FilterResults();
        if(s != null && s.toString().length() > 0)
        
            s = s.toString().toLowerCase();
            ArrayList<Word> filteredItems = new ArrayList<Word>();

            for(int i = 0, l = searchList.size(); i < l; i++)
            
                if(searchList.get(i).getmDefaultTranslation().toLowerCase().contains(s))
                
                    Word x = new Word(searchList.get(i).getmDefaultTranslation(),searchList.get(i).getmMiwokTranslation(),
                            searchList.get(i).getmImageResourceId(),searchList.get(i).getmAudioResourceId());
                    filteredItems.add(x);
                   
                
            
            result.count = filteredItems.size();
            result.values = filteredItems;
        else 
            result.count = searchList.size();
            result.values = searchList;
        

        return result;
    

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence s,
                                  FilterResults results) 

        numbersWords = (ArrayList<Word>)results.values;
        Log.i("new search", String.valueOf(results));
        notifyDataSetChanged();
    

对不起我的技术语言!!! 这是我的仓库的链接,您可以在其中检查具有多个片段的搜索的完整实现(使用 ViewModel) https://github.com/artnkfv/Multiscreen_App/tree/main_v1.1

【讨论】:

以上是关于在android中使用viewpager的带有多个片段的SearchView的主要内容,如果未能解决你的问题,请参考以下文章

带有底点的 Android ViewPager

如果我们必须在android中使用viewpager创建多个页面,那么我们是不是必须创建多个片段?还有其他选择吗?

带有ListView的Android ViewPager在调用notifyDatasetchanged时无法记住最后一个位置

带有viewpager2 android的导航图

listview 没有在带有 viewpager 选项卡的片段中刷新

Android ViewPage使用