在片段中的RecylerView上的SearchView

时间:2018-02-06 19:52:28

标签: android android-fragments android-recyclerview searchview

我一直在SearchView工作,我的失败是悲惨的。当我在上面输入内容时,没有任何反应,它也不会过滤列表。我在下面提供了我的片段和我的适配器。请帮忙!谢谢!

其他问题:在搜索视图中输入内容后,如果点击后退按钮,我希望键盘隐藏并返回片段的标题。我该怎么做?

片段

public class RecentNewsFragment extends Fragment implements ConnectivityReceiver.ConnectivityReceiverListener, SearchView.OnQueryTextListener {

    public RecentNewsFragment() {

    }

    final String TAG = "MainPageActivity";
    ArrayList<News> newsList;
    public RecyclerView rvNewsArticle;
    public NewsArticleAdapter adapter;
    public SwipeRefreshLayout swipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_recent_news, container, false);

        checkConnection();

        rvNewsArticle = (RecyclerView) view.findViewById(R.id.rvNewsArticle);
        rvNewsArticle.setHasFixedSize(true);
        final LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rvNewsArticle.setLayoutManager(manager);

        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefresh);
        swipeRefreshLayout.setColorSchemeResources(R.color.primaryColor, R.color.primaryTextColor, R.color.secondaryColor);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadNews();
                    }
                }, 1000);
            }
        });

        return view;
    }

    private void loadNews() {
        String ip = IPAddress.ipaddress;
        String url = "http://" + ip + "/theflare/app/news.php";
        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response);

                        newsList = new JsonConverter<News>()
                                .toArrayList(response, News.class);
                        adapter = new NewsArticleAdapter(getActivity(), newsList);
                        rvNewsArticle.setAdapter(adapter);

                        swipeRefreshLayout.setRefreshing(false);

                        FileCacher<ArrayList<News>> newsCacher = new FileCacher<>(getActivity(), "theflarenewscache.txt");
                        try {
                            newsCacher.appendOrWriteCache(newsList);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (error != null) {
                            Snackbar snackbar = Snackbar
                                    .make(getActivity().findViewById(android.R.id.content), "Something went wrong.", Snackbar.LENGTH_INDEFINITE)
                                    .setAction("Retry", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            loadNews();
                                        }
                                    });
                            View sbView = snackbar.getView();
                            sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.maroon));
                            snackbar.show();
                        }
                    }
                }
        );

        MySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
    }

    private void loadCachedNews() {
        FileCacher<ArrayList<News>> newsCacher = new FileCacher<>(getActivity(), "theflarenewscache.txt");
            if(newsCacher.hasCache()){
                try {
                    ArrayList<News> cachedNewsList = newsCacher.readCache();
                    final NewsArticleAdapter adapter = new NewsArticleAdapter(getActivity(), cachedNewsList);
                    //rvNewsArticle.setAdapter(adapter);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassCastException e){
                    e.printStackTrace();
                }
            }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.main_page, menu);
        // Associate searchable configuration with the SearchView
        final MenuItem searchItem = menu.findItem(R.id.action_search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(this);

        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
                    @Override
                    public boolean onMenuItemActionCollapse(MenuItem item) {
                    // Do something when collapsed
                        adapter.setFilter(newsList);
                        return true; // Return true to collapse action view
                    }

                    @Override
                    public boolean onMenuItemActionExpand(MenuItem item) {
                        // Do something when expanded
                        return true; // Return true to expand action view
                    }
                });

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        if (adapter != null) {

            query = String.valueOf(query.equals(""));
        }
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        newText = newText.toLowerCase();
        final ArrayList<News> filteredNewsList = new ArrayList<>();
        for (News model : newsList) {
            final String title = model.getTitle().toLowerCase();
            final String author = model.getAuthor().toLowerCase();
            final String category = model.getCategory().toLowerCase();
            final String content = model.getContent().toLowerCase();
            if ((title.contains(newText)) || (author.contains(newText)) || (category.contains(newText)) || (content.contains(newText))) {
                filteredNewsList.add(model);
            }
        }
        adapter.setFilter(filteredNewsList);
        return true;
    }

    // Method to manually check connection status
    private void checkConnection() {
        boolean isConnected = isConnected();
        snackInternet(isConnected);
    }

    private void snackInternet(boolean isConnected) {
      if (isConnected) {
          Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content), "Connected to the Internet!", Snackbar.LENGTH_SHORT);
          View sbView = snackbar.getView();
          sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.primaryDarkColor));
          snackbar.show();
          loadNews();
      }
      else {
          Snackbar snackbar = Snackbar
                  .make(getActivity().findViewById(android.R.id.content), "You seem to be offline.", Snackbar.LENGTH_INDEFINITE)
                  .setAction("Retry", new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          loadNews();
                      }
                  });
          View sbView = snackbar.getView();
          sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.maroon));
          snackbar.show();
          loadCachedNews();
      }
    }

    @Override
    public void onResume() {
        super.onResume();

        // register connection status listener
        TheFlare.getInstance().setConnectivityListener(this);
    }

    /**
     * Callback will be triggered when there is change in
     * network connection
     */
    @Override
    public void onNetworkConnectionChanged(boolean isConnected) {
        snackInternet(isConnected);
    }

}

适配器

public class NewsArticleAdapter extends RecyclerView.Adapter<NewsArticleAdapter.NewsViewHolder> {

    private Context context;
    private ArrayList<News> newsList;

    public NewsArticleAdapter(Context context, ArrayList<News> newsList){
        this.context = context;
        this.newsList = newsList;
    }

    @Override
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.from(parent.getContext())
                .inflate(R.layout.news_cardview_layout, parent, false);

        NewsViewHolder newsViewHolder = new NewsViewHolder(view);
        return newsViewHolder;
    }

    @Override
    public void onBindViewHolder(NewsViewHolder holder, int position) {
        final News selectedNews = newsList.get(position);
        holder.tvId.setText(String.valueOf(selectedNews.id));
        holder.tvTitle.setText(selectedNews.title);
        holder.tvPubDate.setText("" + selectedNews.publicationDate);

        String ip = IPAddress.ipaddress;
        String fullUrl = "http://" + ip + "/theflare/admin/img/newsbanner/" + selectedNews.imgBanner;

        Picasso.with(context)
                .load(fullUrl)
                .placeholder(R.drawable.bannerplaceholder)
                .error(android.R.drawable.stat_notify_error)
                .into(holder.ivImage);

        holder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view) {
                Intent intent = new Intent(context, NewsDetailActivity.class);
                intent.putExtra("newsList", selectedNews);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if(newsList != null){
            return newsList.size();
        }
        return 0;
    }

    public void setFilter(ArrayList<News> newsArrayList) {
        newsList.clear();
        newsList.addAll(newsArrayList);
        notifyDataSetChanged();
    }

    //ViewHolder class
    public static class NewsViewHolder extends RecyclerView.ViewHolder{

        public CardView cvNews;
        public TextView tvId;
        public ImageView ivImage;
        public TextView tvTitle;
        public TextView tvPubDate;

        public NewsViewHolder(View itemView) {
            super(itemView);
            cvNews = (CardView)itemView.findViewById(R.id.cvNews);
            tvId = (TextView) itemView.findViewById(R.id.tvId);
            ivImage = (ImageView)itemView.findViewById(R.id.ivImageUrl);
            tvTitle = (TextView)itemView.findViewById(R.id.tvTitle);
            tvPubDate = (TextView)itemView.findViewById(R.id.tvPubDate);

        }
    }
}

1 个答案:

答案 0 :(得分:0)

解决了!不得不插入一些代码,它终于工作了!这就是我所做的:

在我的MainActivity中,我必须将onCreateOptions方法更改为:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }

然后在我的片段的onCreateView方法中:

setHasOptionsMenu(true);

我还必须实现searchview和menuitem的监听器:

public class RecentNewsFragment extends Fragment implements SearchView.OnQueryTextListener, MenuItem.OnActionExpandListener

之后,我实施了这些方法:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.search_menu, menu);
        // Associate searchable configuration with the SearchView
        final MenuItem searchItem = menu.findItem(R.id.action_search);
        MenuItemCompat.setShowAsAction(searchItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(this);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (newText == null || newText.trim().isEmpty()) {
            adapter.setFilter(newsList);
            return false;
        }
        newText = newText.toLowerCase();
        final ArrayList<News> filteredNewsList = new ArrayList<>();
        for (News model : newsList) {
            final String title = model.getTitle().toLowerCase();
            final String author = model.getAuthor().toLowerCase();
            final String category = model.getCategory().toLowerCase();
            final String content = model.getContent().toLowerCase();
            if ((title.contains(newText)) || (author.contains(newText)) || (category.contains(newText)) || (content.contains(newText))) {
                filteredNewsList.add(model);
            }
        }
        adapter.setFilter(filteredNewsList);
        return true;
    }

@Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        adapter.setFilter(newsList);
        return true;
    }

最后,在我的适配器中:

public void setFilter(ArrayList<News> newsArrayList) {
        news.clear();
        news.addAll(newsArrayList);
        notifyDataSetChanged();
    }