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.DeviceBean; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | public class SearchDeviceAdapter extends RecyclerView.Adapter<SearchDeviceAdapter.MyViewHolder> { |
| | | List<DeviceBean> mList; |
| | | |
| | | Context mContext; |
| | | |
| | | DeviceInfoAdapter.OnClickListener mOnclickListener; |
| | | |
| | | public SearchDeviceAdapter(Context context) { |
| | | |
| | | this.mContext = context; |
| | | } |
| | | |
| | | @NonNull |
| | | @Override |
| | | public SearchDeviceAdapter.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 SearchDeviceAdapter.MyViewHolder holder, int position) { |
| | | DeviceBean deviceBean = this.mList.get(position); |
| | | |
| | | holder.power_station_name_tv.setText(deviceBean.getHomeName()); |
| | | holder.power_station_location_tv.setText(deviceBean.getAddress()); |
| | | 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(), deviceBean); |
| | | } |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | | return this.mList == null ? 0 : this.mList.size(); |
| | | } |
| | | |
| | | public void setOnclickListener(DeviceInfoAdapter.OnClickListener onClickListener) { |
| | | this.mOnclickListener = onClickListener; |
| | | } |
| | | |
| | | public void setList(List<DeviceBean> 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, DeviceBean deviceBean); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 一行布局容器 |
| | | */ |
| | | 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); |
| | | } |
| | | } |
| | | } |