如何在recyclerview中使用编辑文本进行搜索?

问题描述 投票:0回答:1

我正在为我的申请寻求帮助。我已经构建了一个应用程序,并且包含了聊天功能。在聊天片段中,我还有2个其他片段,一个用于当前打开的聊天,另一个用于可以向所有用户发送消息。我想将搜索栏合并到每个片段中,以允许用户找到要发送消息的特定用户。我是

用户片段XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@color/gold"
    tools:context=".ChatFragments.UsersFragment">

    <EditText
        android:id="@+id/etSearch"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:ems="20"
        android:hint="@string/search"
        android:layout_weight="0.5"
        android:paddingLeft="15dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_below="@+id/etSearch"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

用户片段java类

package com.alicearmstrong.coffeysloyaltyprojectv1.ChatFragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import com.alicearmstrong.coffeysloyaltyprojectv1.Adapter.CustomerAdapter;
import com.alicearmstrong.coffeysloyaltyprojectv1.R;
import com.alicearmstrong.coffeysloyaltyprojectv1.database.Customers;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class UsersFragment extends Fragment {

    private RecyclerView recyclerView;
    private CustomerAdapter customerAdapter;
    private List<Customers> customersList;
    DatabaseReference databaseReference;

    EditText etSearch;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        View view = inflater.inflate( R.layout.fragment_users , container, false);
        etSearch = view.findViewById(R.id.etSearch);
        recyclerView = view.findViewById( R.id.recycler_view );
        recyclerView.setHasFixedSize( true );
        recyclerView.setLayoutManager( new LinearLayoutManager( getContext() ) );

        customersList = new ArrayList<>(  );

        readCustomers();
        return view;
    }

    // Method for reading customers
    private void readCustomers()
    {
        final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        databaseReference = FirebaseDatabase.getInstance().getReference().child("Customers");

        databaseReference.addValueEventListener( new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                customersList.clear();

                for (DataSnapshot snapshot : dataSnapshot.getChildren())
                {
                    Customers customers = snapshot.getValue(Customers.class);

                    if (!customers.getId().equals(firebaseUser.getUid()))
                    {
                        customersList.add( customers );
                    }
                }

                customerAdapter = new CustomerAdapter( getContext(), customersList );
                recyclerView.setAdapter( customerAdapter );

            }

            @Override
            public void onCancelled( @NonNull DatabaseError databaseError) {

            }
        } );
    }
}

我读过addTextChangeListener方法,只是不确定如何实现该方法。任何帮助将不胜感激。

android search android-recyclerview
1个回答
0
投票

为什么存在searchView时为什么使用textView。检查下面的这段代码,但让我看看您的客户Java类,那么我可以更好地帮助您。在第一个设置的搜索视图中,然后按ID查找它,然后....

© www.soinside.com 2019 - 2024. All rights reserved.