安卓工作室如何使用一个回收视图进行多个视图



我有一个search fragment,它允许用户搜索用户和帖子。这是XML:

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_bar"
android:background="@drawable/white_rounded"
android:hint="Search for users or recipes ..."
android:drawableEnd="@drawable/ic_search"
android:maxLines="1"
android:inputType="textFilter"
android:layout_margin="10dp"
android:singleLine="true"
android:lines="1"
android:layout_marginStart="25dp"
android:layout_marginLeft="10dp"/>
<!-- android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" -->
</androidx.appcompat.widget.Toolbar>
<com.taimoorsikander.myapplication.Widgets.B_RecycleView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_below="@+id/bar" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_marginBottom="@dimen/_50sdp"
android:layout_below="@+id/bar" />

在我的SearchFragment中,我实现了这样的搜索:

public class SearchFragment extends Fragment {
private B_RecycleView recyclerView;
private SearchAdapter searchAdapter;
private List<User> mUsers;
// for posts
EditText search_bar;
private RecyclerView recyclerView2;
DatabaseReference DataRef;
private List<Post> mPosts;
PostSearchAdapter postSearchAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
DataRef = FirebaseDatabase.getInstance().getReference().child("posts");
recyclerView2 = view.findViewById(R.id.recycle_view2);
search_bar = view.findViewById(R.id.search_bar);
recyclerView2.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView2.setHasFixedSize(true);
mPosts = new ArrayList<>();
postSearchAdapter = new PostSearchAdapter(getContext(), mPosts);
recyclerView2.setAdapter(postSearchAdapter);
readPosts();
search_bar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().isEmpty()) {
mPosts.clear();
recyclerView2.removeAllViews();
//   recyclerView2.hideIfEmpty(recyclerView2);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().isEmpty()) {
searchPosts(s.toString().toLowerCase());
}else
{
mPosts.clear();
recyclerView2.removeAllViews();
}
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().isEmpty()) {
mPosts.clear();
recyclerView2.removeAllViews();
}
}
});
recyclerView = view.findViewById(R.id.recycle_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
search_bar = view.findViewById(R.id.search_bar);
mUsers = new ArrayList<>();
searchAdapter = new SearchAdapter(getContext(), mUsers);
recyclerView.setAdapter(searchAdapter);
readUsers();
search_bar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().isEmpty()) {
mUsers.clear();
recyclerView.removeAllViews();
recyclerView.hideIfEmpty(recyclerView);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().isEmpty()) {
searchUsers(s.toString().toLowerCase());
}else
{
mUsers.clear();
recyclerView.removeAllViews();
}
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().isEmpty()) {
mUsers.clear();
recyclerView.removeAllViews();
}
}
});

我使用了两个适配器,因为我在搜索中使用了两种不同的东西(帖子和用户(。它现在正在工作,但问题是有两个单独的CCD_ 3看起来并不整洁。有办法把它们合并吗?我希望他们在一个观点。有可能吗?

有一些替代方案,但如果您不想重新发明轮子,请尝试使用以下方法:

有不同的项目或布局(视图(:

tl;dr如果你想建立一个布局,如Playstore主页或AirBnb酒店列表

Android有ConcatAdapter请参阅此处

此外,为了更好地了解多视图适配器,请参阅此处推荐的文章

tl;dr:适配器委托的目的类似于ConcatAdapter,具有复合(或多个(RecyclerView(在1 RecyclerView中具有不同布局的适配器(

灵感来自:http://hannesdorfmann.com/android/adapter-delegates/

由于您已经创建了两个适配器,因此可以在单个RecyclerView上有条件地调用它们;类似于如果搜索查询是针对posts-call-posts-adapter,如果搜索查询针对用户,则调用users-adapter。

最新更新