gpt4 book ai didi

java - 当适配器处于同一 Activity 中时,在回收器 View 中添加搜索 View

转载 作者:行者123 更新时间:2023-12-01 16:39:00 24 4
gpt4 key购买 nike

我的回收器 View 中需要一个搜索 View 。回收器 View 应该是不可见的,一旦我们搜索,结果应该是可见的。但我无法做到这一点,请帮助我。我正在 Android 7 中工作并使用 firebase 作为服务器。我将粘贴该 Activity 。 (ChatsFragment 和联系人)。我已经搜索过这个问题了。但他们将适配器用作单独的类。但在我的代码中,适配器处于同一 Activity 中。我是安卓新手。所以我不知道。我正在使用互联网上提供的代码构建一个聊天应用程序。提前致谢。

 public class ChatsFragment extends Fragment
{
private View PrivateChatsView;
private RecyclerView chatsList;

private DatabaseReference ChatsRef, UsersRef;
private FirebaseAuth mAuth;
private String currentUserID="";


public ChatsFragment() {

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
PrivateChatsView = inflater.inflate(R.layout.fragment_chats, container, false);


mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
ChatsRef = FirebaseDatabase.getInstance().getReference().child("Contacts").child(currentUserID);
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");


chatsList = (RecyclerView) PrivateChatsView.findViewById(R.id.chats_list);
chatsList.setLayoutManager(new LinearLayoutManager(getContext()));


return PrivateChatsView;
}


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


FirebaseRecyclerOptions<Contacts> options =
new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(ChatsRef, Contacts.class)
.build();


FirebaseRecyclerAdapter<Contacts, ChatsViewHolder> adapter =
new FirebaseRecyclerAdapter<Contacts, ChatsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final ChatsViewHolder holder, int position, @NonNull Contacts model)
{
final String usersIDs = getRef(position).getKey();
final String[] retImage = {"default_image"};

UsersRef.child(usersIDs).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if (dataSnapshot.exists())
{
if (dataSnapshot.hasChild("image"))
{
retImage[0] = dataSnapshot.child("image").getValue().toString();
Picasso.get().load(retImage[0]).into(holder.profileImage);
}

final String retName = dataSnapshot.child("name").getValue().toString();
final String retStatus = dataSnapshot.child("status").getValue().toString();

holder.userName.setText(retName);


if (dataSnapshot.child("userState").hasChild("state"))
{
String state = dataSnapshot.child("userState").child("state").getValue().toString();
String date = dataSnapshot.child("userState").child("date").getValue().toString();
String time = dataSnapshot.child("userState").child("time").getValue().toString();

if (state.equals("online"))
{
holder.userStatus.setText("online");
}
else if (state.equals("offline"))
{
holder.userStatus.setText("Last Seen: " + date + " " + time);
}
}
else
{
holder.userStatus.setText("offline");
}

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Intent chatIntent = new Intent(getContext(), ChatActivity.class);
chatIntent.putExtra("visit_user_id", usersIDs);
chatIntent.putExtra("visit_user_name", retName);
chatIntent.putExtra("visit_image", retImage[0]);
startActivity(chatIntent);
}
});
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

@NonNull
@Override
public ChatsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.users_display_layout, viewGroup, false);
return new ChatsViewHolder(view);
}
};

chatsList.setAdapter(adapter);
adapter.startListening();
}




public static class ChatsViewHolder extends RecyclerView.ViewHolder
{
CircleImageView profileImage;
TextView userStatus, userName;


public ChatsViewHolder(@NonNull View itemView)
{
super(itemView);

profileImage = itemView.findViewById(R.id.users_profile_image);
userStatus = itemView.findViewById(R.id.user_status);
userName = itemView.findViewById(R.id.user_profile_name);
}
}
}```








This is Contacts.java


```public class Contacts {
public String name, status, image;

public Contacts()
{

}

public Contacts(String name, String status, String image) {
this.name = name;
this.status = status;
this.image = image;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}
}```





![enter image description here](/image/XhKSc.jpg)![enter image description here](/image/DqgUi.jpg)![enter image description here](/image/nUaNK.jpg)![enter image description here](/image/o1nnQ.jpg)![enter image description here](/image/5Qfed.jpg)

最佳答案

您可以将 editText 与文本更改监听器一起使用`

    etSearchWord.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {

}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//here you can hide the recycleView
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

if (s.toString().isNotEmpty()) {
rvAutoCompResult.visible()
autoCompleteItems(s.toString())
} else
rvAutoCompResult.gone()

}
})

`在您的适配器中,您可以使用 Filterable 来过滤数据

关于java - 当适配器处于同一 Activity 中时,在回收器 View 中添加搜索 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61902090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com