android 热门搜索标签,Android网易云历史搜索和热门标签

Android网易云历史搜索和热门标签

最近开发了一个网易云音乐播放器,有这么一个需求,需要展示搜索建议,历史搜索记录

项目地址: https://github.com/shellhub/NetEaseMusic

538176c35c74

search_entry.png

从效果图可以看到,标签如果太长无法容纳会自动换行,虽然我们可以自己实现自定义View,但是人生苦短没必要重复造轮子,这里推荐谷歌推出的库flexbox-layout

添加依赖

implementation 'com.google.android:flexbox:1.1.0'

代码实现

首先这个布局的话我们可以使用RecyclerView去实现,这里需要使用到RecyclerView的多布局,但是为了简单起见我们只看其中的RecyclerView就可以了。

首先我们在你的Activity或者Fragment中初始化RecyclerView,然后设置RecyclerView的布局管理器,然后就可以了,简单吧,就一行代码

rvHots.setLayoutManager(new FlexboxLayoutManager(getContext()));

完整代码如下

public class HistoryFragment extends Fragment {

private String TAG = TagUtils.getTag(this.getClass());

@BindView(R.id.iv_remove_history)

ImageView ivRemoveHistory;

@BindView(R.id.rvHots)

RecyclerView rvHots;

@BindView(R.id.rvHistory)

RecyclerView rvHistory;

HotSearchAdapter mHotSearchAdapter;

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_search_hot, container, false);

ButterKnife.bind(this, view);

setup();

return view;

}

@Override

public void onStart() {

super.onStart();

if (!EventBus.getDefault().isRegistered(this)) {

EventBus.getDefault().register(this);

}

}

private void setup() {

rvHots.setAdapter(mHotSearchAdapter = new HotSearchAdapter());

rvHots.setLayoutManager(new FlexboxLayoutManager(getContext()));

}

@Subscribe(threadMode = ThreadMode.MAIN)

public void onHotsReady(HotResponse hotResponse) {

mHotSearchAdapter.setHots(hotResponse.getResult().getHots());

mHotSearchAdapter.notifyDataSetChanged();

}

}

package shellhub.github.neteasemusic.adapter;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import java.util.List;

import androidx.annotation.NonNull;

import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;

import butterknife.ButterKnife;

import lombok.Data;

import shellhub.github.neteasemusic.R;

import shellhub.github.neteasemusic.model.entities.HistoryEvent;

import shellhub.github.neteasemusic.util.TagUtils;

@Data

public class HistoryAdapter extends RecyclerView.Adapter {

private String TAG = TagUtils.getTag(this.getClass());

private List histories = new ArrayList<>();

@NonNull

@Override

public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hot_item, parent, false);

ButterKnife.bind(this, view);

return new HistoryViewHolder(view);

}

@Override

public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

if (holder instanceof HistoryViewHolder) {

((HistoryViewHolder) holder).bind(position);

}

}

@Override

public int getItemCount() {

LogUtils.d(TAG, histories.size());

return histories.size();

}

public class HistoryViewHolder extends RecyclerView.ViewHolder {

@BindView(R.id.tv_hot)

TextView tvHistory;

public HistoryViewHolder(@NonNull View itemView) {

super(itemView);

ButterKnife.bind(this, itemView);

}

public void bind(int position) {

tvHistory.setText(histories.get(position));

itemView.setOnClickListener((view) -> {

EventBus.getDefault().post(new HistoryEvent(histories.get(position)));

});

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值