总体思想:将FlexboxLayoutManager作为RecyclerView的一种manager,使用FlexboxLayoutManager,无需自定义view
效果图
一、导包
目前最新版本
implementation 'com.google.android:flexbox:2.0.1'
二、在布局文件加RecyclerView
<LinearLayout
android:id="@+id/history_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/search_k"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="历史搜索"
android:textColor="#1F1E1E" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_history"
android:layout_marginStart="-15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
三、添加子项的布局
新建search_history.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp">
<TextView
android:id="@+id/search_h_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="2dp"
android:paddingBottom="3dp"
android:textColor="#1E1E1E"
android:textSize="17sp"
android:background="@drawable/searchyes"/>
</FrameLayout>
四、适配器
新建SearchHistoryAdapter
public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.SearchHistoryHolder>{
private Context mcontext;
private SearchHistoryAdapter.onItemClickListener mlistener;
private List<HistoryRecord> mlist;
public SearchHistoryAdapter(Context context, List<HistoryRecord> list, SearchHistoryAdapter.onItemClickListener listener){
this.mcontext=context;
this.mlistener=listener;
this.mlist=list;
}
@NonNull
@Override
public SearchHistoryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SearchHistoryAdapter.SearchHistoryHolder(LayoutInflater.from(mcontext).inflate(R.layout.search_history,parent,false));
}
@Override
public void onBindViewHolder(@NonNull SearchHistoryHolder holder, int position) {
if(mlist!=null&&mlist.size()>0){
holder.textView.setText(mlist.get(position).getHistory_record());
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mlistener.onClick(position);
}
});
}
@Override
public int getItemCount() {
return mlist.size();
}
public class SearchHistoryHolder extends RecyclerView.ViewHolder{
TextView textView;
public SearchHistoryHolder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.search_h_d);
}
}
public interface onItemClickListener{
void onClick(int pos);
}
}
五、初始化FlexboxLayoutManager
private FlexboxLayoutManager manager;
//历史记录的LayoutManager
manager = new FlexboxLayoutManager(this);
//设置主轴排列方式
manager.setFlexDirection(FlexDirection.ROW);
//设置是否换行
manager.setFlexWrap(FlexWrap.WRAP);
manager.setAlignItems(AlignItems.STRETCH);
六、设置适配器
private RecyclerView searchHistory;
//填充历史搜索
private void history(){
List<HistoryRecord> historyRecordList = LitePal.findAll(HistoryRecord.class);
searchHistory.setLayoutManager(manager);
searchHistory.setAdapter(new SearchHistoryAdapter(this, historyRecordList, new SearchHistoryAdapter.onItemClickListener() {
@Override
public void onClick(int pos) {
HistoryRecord h = historyRecordList.get(pos);
search(h.getHistory_record());
}
}));
}