第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

Android網(wǎng)易云歷史搜索和熱門標(biāo)簽

標(biāo)簽:
Android

webp

search_entry.png

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

代码实现

首先这个布局的话我们可以使用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<RecyclerView.ViewHolder> {    private String TAG = TagUtils.getTag(this.getClass());    private List<String> 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)));
            });
        }
    }

}



作者:shellhub
链接:https://www.jianshu.com/p/538176c35c74


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消