New file |
| | |
| | | package com.hdl.photovoltaic.ui.adapter; |
| | | |
| | | import android.content.Context; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.widget.TextView; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.ui.bean.HouseIdBean; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 搜索电站适配器 |
| | | */ |
| | | public class SearchHouseAdapter extends RecyclerView.Adapter<SearchHouseAdapter.MyViewHolder> { |
| | | List<HouseIdBean> mList; |
| | | |
| | | Context mContext; |
| | | |
| | | OnClickListener mOnclickListener; |
| | | |
| | | public SearchHouseAdapter(Context context) { |
| | | |
| | | this.mContext = context; |
| | | } |
| | | |
| | | @NonNull |
| | | @Override |
| | | public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| | | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_house, parent, false); |
| | | return new MyViewHolder(view); |
| | | } |
| | | |
| | | @Override |
| | | public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { |
| | | HouseIdBean houseIdBean = this.mList.get(position); |
| | | |
| | | holder.power_station_name_tv.setText(houseIdBean.getHomeName()); |
| | | holder.power_station_location_tv.setText(houseIdBean.getHomeAddress()); |
| | | holder.itemView.setTag(position); |
| | | holder.itemView.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | try { |
| | | if (mOnclickListener != null) { |
| | | mOnclickListener.onClick((int) holder.itemView.getTag(), houseIdBean); |
| | | } |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | | return this.mList == null ? 0 : this.mList.size(); |
| | | } |
| | | |
| | | public void setOnclickListener(OnClickListener onClickListener) { |
| | | this.mOnclickListener = onClickListener; |
| | | } |
| | | |
| | | public void setList(List<HouseIdBean> newData) { |
| | | if (this.mList == null) { |
| | | this.mList = new ArrayList<>(); |
| | | } else { |
| | | this.mList.clear(); |
| | | } |
| | | |
| | | this.mList.addAll(newData); |
| | | notifyDataSetChanged(); |
| | | } |
| | | |
| | | public interface OnClickListener { |
| | | void onClick(int position, HouseIdBean houseIdBean); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 一行布局容器 |
| | | */ |
| | | static class MyViewHolder extends RecyclerView.ViewHolder { |
| | | |
| | | |
| | | public TextView power_station_name_tv; |
| | | public TextView power_station_location_tv; |
| | | |
| | | |
| | | public MyViewHolder(@NonNull View itemView) { |
| | | super(itemView); |
| | | power_station_name_tv = itemView.findViewById(R.id.message_name_tv); |
| | | power_station_location_tv = itemView.findViewById(R.id.power_station_location_tv); |
| | | } |
| | | } |
| | | } |