RecyclerView添加Header

1

// 万能adapter
    implementation 'com.zhy:base-rvadapter:3.0.3'
    implementation 'com.zhy:base-adapter:3.0.3'

2

GridLayoutManager gridLayoutManager = new GridLayoutManager(ServiceCustomActivity.this, 2);
                    hotServiceRv.setLayoutManager(gridLayoutManager);


                    myAdapter = new HotServiceAdapter(data, ServiceCustomActivity.this);
//                    hotServiceRv.setAdapter(myAdapter);
                    myAdapter.setRecyclerItemClickListener(new HotServiceAdapter.OnRecyclerItemClickListener() {
                        @Override
                        public void onRecyclerItemClick(int position) {
                            int serviceClassId = data.get(position).getServiceClassId();
                            int serviceId = data.get(position).getServiceId();
                            Intent intent = new Intent(ServiceCustomActivity.this, ServiceDetailWebActivity.class);
                            intent.putExtra("weburl", Constant.service + "serviceClassId=" + serviceClassId + "&serviceId=" + serviceId);
                            startActivity(intent);

                        }
                    });


                    HeaderAndFooterWrapper mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(myAdapter);

                    TextView t1 = new TextView(ServiceCustomActivity.this);
                    t1.setText("热门服务");
                    t1.setTextColor(R.color.black);
                    t1.setTextSize(16);
                    t1.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    View headView = getLayoutInflater().inflate(R.layout.header_view, null);
                    mHeaderAndFooterWrapper.addHeaderView(headView);
                    mHeaderAndFooterWrapper.addHeaderView(t1);

                    hotServiceRv.setAdapter(mHeaderAndFooterWrapper);
                    mHeaderAndFooterWrapper.notifyDataSetChanged();

3




    

        

        

            

4

package com.ruhai.yibei.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ruhai.yibei.R;
import com.ruhai.yibei.bean.HotServiceBean;

import java.util.List;

public class HotServiceAdapter extends RecyclerView.Adapter {

    private List data;
    private Context context;

    public HotServiceAdapter(List data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.hot_service_item, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tv.setText(data.get(position).getServiceName());
        if (data.get(position).getPicUrl().contains("http")) {
            Glide.with(context).load(data.get(position).getPicUrl()).into(holder.hotServiceImgv);
        } else {
            Glide.with(context).load("https:" + data.get(position).getPicUrl()).into(holder.hotServiceImgv);
        }

    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;
        private ImageView hotServiceImgv;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.hot_service_tv);
            hotServiceImgv = (ImageView) itemView.findViewById(R.id.hot_service_imgv);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    private OnRecyclerItemClickListener mOnItemClickListener;

    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public interface OnRecyclerItemClickListener {
        void onRecyclerItemClick(int position);
    }
}


你可能感兴趣的:(万能adapter,HeaderView)