android-fragments 我如何在Android中的另一个屏幕上显示我的过滤数据?

oo7oh9g9  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(106)

我正在开发一个应用程序,在我的主页片段中,我有一个搜索视图,在该视图下有一个回收视图,我在其中显示整个数据,但当用户使用搜索视图搜索任何数据时,我现在想将数据显示到另一个片段,我可以做到吗??请指导我
图像交换

用于搜索家庭片段的代码

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (searchView != null) {
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                pd.setTitle("Searching");
                pd.show();

                Runnable progressRunnable = new Runnable() {

                    @Override
                    public void run() {
                        search(query);
                        pd.cancel();
                    }
                };

                Handler pdCanceller = new Handler();
                pdCanceller.postDelayed(progressRunnable, 3000);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                if (newText.isEmpty())
                {
                    getData();
                }

                return false;
            }
        });
    }

    getDataCompleteData();
    
}


//SEARCH METHOD
private void search(String s) {

     mydatalist = new ArrayList<>();

    for (RecyclerviewModel object : datalist) {
        if (object.getSearching().contains(s.toLowerCase().trim())) {
            mydatalist.add(object);
        }

        RecyclerviewAdapter adapter = new RecyclerviewAdapter(mydatalist, getContext(), HomeFragment.this);
        recyclerView.setAdapter(adapter);

    }

}

这是我在主页片段上搜索的代码,但此代码在同一片段上显示筛选(搜索数据),我希望当用户在SEARCHVIEW中输入查询时,搜索数据应显示在新片段上。

kx5bkwkv

kx5bkwkv1#

From your requirement i can not able to understand your actual approach to show the data. As i understand this approach to do the same don't know this will fulfil your requirement which you're looking for.
You can transfer data between fragments using bundle. After user search any value you will get the filtered list from the main data list, now you can pass this filtered list using the code below to put when you get the filtered list from your search function in your case you have mydatalist in your search function. Make sure to create your data class of the list Serializable.

Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("list", mydatalist); 
    
    Fragment2 fragment2 = new Fragment2();
    fragment2.setArguments(bundle);
    
    getFragmentManager()
          .beginTransaction()
          .replace(<Your container id>, fragment2)
          .commit();

Now in your second fragment get the list like the code below and set as per your requirement.

Bundle bundle = this.getArguments();
    
    if(bundle != null){
         // handle your code here.
         <Your List Type> = listbundle.getParcelableArrayList("list");
    }

相关问题