/* Copyright (C) 2021-2024 Arjan Schrijver This file is part of Gadgetbridge. Gadgetbridge is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Gadgetbridge is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.devices.qhybrid; import android.content.Context; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; import java.util.Collections; import java.util.List; import nodomain.freeyourgadget.gadgetbridge.R; public class CommuteActionsListAdapter extends RecyclerView.Adapter { private CommuteActionsActivity parentActivity; private List mData; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor public CommuteActionsListAdapter(Context context, List data) { this.mInflater = LayoutInflater.from(context); this.mData = data; this.parentActivity = (CommuteActionsActivity) context; } // inflates the row layout from xml when needed @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.fossil_hr_row_commute_action, parent, false); return new ViewHolder(view); } // binds the data to the TextView in each row @Override public void onBindViewHolder(final ViewHolder holder, int position) { String line = mData.get(position); holder.mTextView.setText(line); holder.mDragHandle.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { parentActivity.startDragging(holder); return true; } }); } // total number of rows @Override public int getItemCount() { return mData.size(); } public void onItemMove(int from, int to) { Collections.swap(mData, from, to); notifyItemMoved(from, to); } // stores and recycles views as they are scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView mTextView; ImageView mDragHandle; ViewHolder(View itemView) { super(itemView); mTextView = itemView.findViewById(R.id.fossil_hr_row_commute_action); mDragHandle = (ImageView) itemView.findViewById(R.id.drag_handle); itemView.setOnClickListener(this); } @Override public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData.get(id); } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // parent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } }