我有一个应用程序,它按类别列出回收视图中的书籍。类别列表是动态的;每个用户将根据其配置文件看到不同的类别。图书列表也在(水平)回收视图中,也是动态的,取决于用户可以访问哪些书籍。
就像这样:
Category 1
<List of books>
Category 2
<List of books>如何为检索到的所有书籍添加带有自定义适配器的SearchView?
这是我的主要活动。
BrowseBooksActivity它有两个功能:
BrowseBooksAdapter
class BrowseBooksAdapter (val context: Context, val bookListByGroup : HashMap<String, List<ResourcesList>>?): RecyclerView.Adapter<CustomViewHolder>() {
override fun getItemCount(): Int {
return bookListByGroup!!.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.browsebooks_layout, parent, false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val keyList = ArrayList(bookListByGroup!!.keys)
val valueList = ArrayList(bookListByGroup!!.values)
holder?.v?.book_category.text = keyList[position]
var booksByCategory : List<ResourcesList>? = valueList[position]
holder?.v.inner_recyclerview.adapter = BooksByCategoryAdapter(booksByCategory)
holder?.v.inner_recyclerview.layoutManager = CustomLinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
}
}
class CustomViewHolder(val v: View): RecyclerView.ViewHolder(v) {
}BooksByCategoryAdapter
class BooksByCategoryAdapter (val booksByCategory: List<ResourcesList>?): RecyclerView.Adapter<CustomVH>() {
override fun getItemCount(): Int {
return booksByCategory!!.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomVH {
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.bookcontainer_layout, parent, false)
return CustomVH(cellForRow)
}
override fun onBindViewHolder(holder: CustomVH, position: Int) {
holder.booksByCategory = booksByCategory!![position]
Picasso.get().load(booksByCategory!![position].coverURI).into(holder?.v?.inner_bookcover)
}
}
class CustomVH(val v: View, var booksByCategory : ResourcesList? = null): RecyclerView.ViewHolder(v) {
init {
v.setOnClickListener {
val intent = Intent (v.context, ReadBooksActivity::class.java)
intent.putExtra("ID", booksByCategory!!.id)
v.context.startActivity(intent)
}
}
}发布于 2018-04-26 05:55:28
您的问题还不清楚,但是如果您想显示所有数据,那么如果您想使用自定义适配器按类别进行搜索,则可以使用此方法。
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.learn2crack.recyclerviewsearch.MainActivity"> <item android:id="@+id/search" android:icon="@drawable/ic_search" android:title="Search" app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView"/> </menu>@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.search_menu, menu); MenuItem search = menu.findItem(R.id.search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(search); //Calling function to search the text you entered in your custom adapter search(searchView); }这里,Old_Adapter是你发送到回收视图来显示内容的适配器。您必须使用与搜索的字符串匹配的数据创建新的适配器对象,并使用此新适配器调用recyclerview。
private void search(SearchView searchView) {
searchView.setQueryHint("SOME_CATEGORY"); //Mention what you want to search as hint
searchView.setInputType(InputType.TYPE_CLASS_TEXT);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
//Your searched text will appear as newText
@Override
public boolean onQueryTextChange(String newText) {
//Create new object of your custom adapter
List<AnyAdapter> searchcategory = new ArrayList<>();
//Old_Adapter is your adapter with which you sent data to your Recyclerview
for (int x = 0; x < Old_Adapter.size(); x++) {
//Fetch data from your Old_Adapter
final AnyAdapter newSearch = Old_Adapter.get(x);
String cat = newSearch.getCategory();
//compare the data with searched text add newSearch adapter to new Adapter created earlier
if (cat.contains(newText)) {
searchcategory.add(newSearch);
}
}
if (newText.isEmpty()) { //If searched text is empty then pass Old_Adapter to your recyclerview again
callRecyclerview(Old_Adapter);
} else{ //Else call recyclerview with new Adapter
callRecyclerview(searchcategory);
}
return true;
}
});
}https://stackoverflow.com/questions/50034479
复制相似问题