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

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

原文地址

最近开发了一个网易云音乐播放器,有这么一个需求,需要展示搜索建议,历史搜索记录
项目地址: https://github.com/shellhub/NetEaseMusic

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()));

完整代码如下

HistoryFragment.java

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();
    }
}

HistoryAdapter.java

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)));
            });
        }
    }

}

你可能感兴趣的:(Android网易云历史搜索和热门标签)