xml
<android.support.v7.widget.RecyclerView
android:id="@+id/user_historyLog_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/list_title"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" />
Adapter.class
public class DroneHistoryAdapter extends RecyclerView.Adapter<DroneHistoryAdapter.ViewHolder> implements View.OnClickListener {
// 数据集合
private List<DroneHistoryData> droneHistoryDataList;
private Context context;
// 监听单击事件
private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener = null;
public DroneHistoryAdapter(Context context, List<DroneHistoryData> datas) {
// 初始化数据
this.context = context;
droneHistoryDataList = datas;
mOpenDatabase = new OpenDatabase(context);
}
public void setOnItemClickListener(OnRecyclerViewItemClickListener listener){
mOnRecyclerViewItemClickListener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_histroy_tlog_item, parent, false);
final TextView ID = (TextView) view.findViewById(R.id.history_list_id);
final TextView date = (TextView) view.findViewById(R.id.history_list_date);
final TextView location = (TextView) view.findViewById(R.id.history_list_location);
final TextView distance = (TextView) view.findViewById(R.id.history_list_distance);
final TextView useTime = (TextView) view.findViewById(R.id.history_list_use_time);
final TextView height = (TextView) view.findViewById(R.id.history_list_height);
view.setOnClickListener(this);
return new ViewHolder(view, ID, date, location, distance, useTime, height);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final DroneHistoryData droneHistoryData = droneHistoryDataList.get(position);
if (droneHistoryData == null)
return;
holder.itemView.setTag(droneHistoryData);
// 设置ID号
holder.ID.setText((position + 1) + "");
// 设置日期
holder.date.setText(droneHistoryData.getFlightDate());
// 设置距离
holder.distance.setText(String.format("%sm", droneHistoryData.getFlightDistance()));
// 设置高度
holder.height.setText(String.format("%sm", droneHistoryData.getFlightHeight()));
}
@Override
public int getItemCount() {
return droneHistoryDataList.size();
}
@Override
public void onClick(View view) {
if(mOnRecyclerViewItemClickListener!=null){
mOnRecyclerViewItemClickListener.onItemClick(view, (DroneHistoryData) view.getTag());
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
final TextView ID;
final TextView date;
final TextView location;
final TextView distance;
final TextView time;
final TextView height;
public ViewHolder(View itemView, TextView ID, TextView date, TextView distance, TextView height,
TextView time, TextView location) {
super(itemView);
this.ID = ID;
this.date = date;
this.location = location;
this.distance = distance;
this.time = time;
this.height = height;
}
}
}
使用代码段
recyclerView = (RecyclerView) view.findViewById(R.id.user_historyLog_list);
recyclerView.setHasFixedSize(true);
// 列表的列数
final int colCount = 1;
final RecyclerView.LayoutManager gridLayoutMgr = new GridLayoutManager(context, colCount);
recyclerView.setLayoutManager(gridLayoutMgr);
监听单击接口
public interface OnRecyclerViewItemClickListener {
void onItemClick(View view, DroneHistoryData data);
}