【原创】带下划线单选菜单栏-标签Tab

【本文原创,转载请注明出处】

实际效果:

实现思路:

结合 RecyclerView 实现,横向可拖动 Tab 导航栏。

使用方法:

第一步:一些成员变量:

    private RecyclerView mHorizontalSpeciesRecycler;
    private TextView tv[] = new TextView[10];//用于记录选择了哪个种类商品 灰色变红色
    private int i = 0;
    private String index = "name0";//用于标记tv

第二步:创建 RecyclerView :

public void setmHorizontalSpeciesListView() {
    List<GoodsSpecies> data = initHorizontalSpeciesData();
    mHorizontalSpeciesRecycler = (RecyclerView) goodsView.findViewById(R.id.main_goods_page_horizontal_species_list);
    RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity());
    ((LinearLayoutManager) manager).setOrientation(LinearLayoutManager.HORIZONTAL);
    mHorizontalSpeciesRecycler.setLayoutManager(manager);
    mHorizontalSpeciesRecycler.setAdapter(new HorizontalSpeciesListAdapter(data));
}

第三步:为 RecyclerView 配置 Adapter :

class HorizontalSpeciesListAdapter extends RecyclerView.Adapter<HorizontalSpeciesListAdapter.VH>{
    //② 创建ViewHolder
    class VH extends RecyclerView.ViewHolder{
        private final TextView name,selectedBar;
        public VH(View v) {
            super(v);
            v.getLayoutParams().width = getActivity().getWindowManager().getDefaultDisplay().getWidth() / 4 + 30;
            v.getLayoutParams().height = v.getLayoutParams().width / 2 ;
            name = (TextView) v.findViewById(R.id.main_goods_species_name);
            tv[i++] = selectedBar = (TextView) v.findViewById(R.id.main_goods_selected_bar);
        }
    }

    private List<GoodsSpecies> mDatas;
    public HorizontalSpeciesListAdapter(List<GoodsSpecies> data) {
        this.mDatas = data;
    }

    //③ 在Adapter中实现3个方法
    @Override
    public void onBindViewHolder(final HorizontalSpeciesListAdapter.VH holder, int position) {
        holder.name.setText(mDatas.get(position).getSpeciseName());
        if (mDatas.get(position).getSpeciseName().equals(index)){
            holder.selectedBar.setBackgroundColor(Color.RED);
        }else {
            holder.selectedBar.setBackgroundColor(Color.GRAY);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //item 点击事件
                try {
                    for (TextView j:tv){
                        j.setBackgroundColor(Color.GRAY);
                    }
                }catch (Exception e){

                }
                holder.selectedBar.setBackgroundColor(Color.RED);
                index = holder.name.getText().toString();
                Toast.makeText(getActivity(),"click",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public HorizontalSpeciesListAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_goods_horizontal_species_list, parent, false);
        return new HorizontalSpeciesListAdapter.VH(v);
    }
}

第四步:为 RecyclerView 添加测试信息:

private List<GoodsSpecies> initHorizontalSpeciesData(){
    List<GoodsSpecies> mainHorizontalList = new ArrayList<>();
    for (int i = 0 ; i < 10 ; i++){
        mainHorizontalList.add(new GoodsSpecies("name" + i));
    }
    return mainHorizontalList;
}

【因为是在写项目中用到的,所以如果哪里有疑问,请在评论区留言。】

如果本文对您有用欢迎关注我获得更多信息

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值