简单实现历史记录流式布局展示(RecyclerView+FlexboxLayoutManager),无需自定义

总体思想:将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());
            }
        }));
    }
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值