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

 


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;@Datapublic 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)

发表评论