Я использую recyclerView в своем приложении. У меня есть глобальный список объектов (маршрут). В моем календаре, когда я нажимаю на определенный день, создается другой список с Маршрутом, выполненным в тот день.
Дело в том, что теперь я хочу удалить Маршрут календаря.
Я не знаю почему, но он не занимает правильную позицию ..
Вот мой класс адаптера
package ch.hearc.hikoo.ui.preview;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import ch.hearc.hikoo.R;
import ch.hearc.hikoo.route.Route;
import ch.hearc.hikoo.route.Surfacing;
import ch.hearc.hikoo.tools.DialogTools;
import ch.hearc.hikoo.ui.preview.PreviewFragment.OnListFragmentInteractionListener;
import de.hdodenhof.circleimageview.CircleImageView;
public class PreviewRecyclerViewAdapter extends RecyclerView.Adapter<PreviewRecyclerViewAdapter.ViewHolder>{
//Callback
private final OnListFragmentInteractionListener mListener;
//Input
private final List<Route> mRoutes;
private final Context mContext;
private final Activity mActivity;
public PreviewRecyclerViewAdapter(List<Route> items, OnListFragmentInteractionListener listener, Context context, Activity activity) {
mRoutes = items;
mListener = listener;
mContext = context;
mActivity = activity;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_preview, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Route route = mRoutes.get(position);
//Init view data
holder.mItem = route;
holder.mName.setText(route.getName());
holder.mDistance.setText(String.format("%.1f km", route.getLength() / 1000));
holder.mTime.setText(Route.convertMinuteToHour(route.getDuration()));
holder.mSurfacingNatural.setText(String.format("%.0f%% %s", route.getNaturalSurfacing(), Surfacing.NATURAL.getName(mContext)));
holder.mSurfacingHard.setText(String.format("%.0f%% %s", route.getHardSurfacing(), Surfacing.HARD.getName(mContext)));
holder.mLevel.setText(route.getLevel().getName(mContext));
holder.mElevationGain.setText(String.format("+ %s m", route.getElevationGain()));
holder.mElevationLoss.setText(String.format("- %s m", route.getElevationLoss()));
//Color of the circle image
int color = PreviewFragment.createColorFromPosition(position);
holder.mCircleImage.setFillColor(color);
holder.mCircleImage.setBorderColor(color);
//On item click
holder.mView.setOnClickListener(v -> {
if (null != mListener) {
mListener.onPreviewItemClicked(position);
if (holder.isSelected) {
holder.mView.setBackgroundColor(Color.TRANSPARENT);
holder.isSelected = false;
} else {
holder.mView.setBackgroundColor(ContextCompat.getColor(mContext, (R.color.accent_light)));
holder.isSelected = true;
}
}
});
//On item long click
holder.mView.setOnLongClickListener(v -> {
DialogTools.displayRouteInfoDialog(mActivity, route);
return true;
});
holder.mView.setOnClickListener(v -> {
if (holder.isSelected) {
holder.mView.setBackgroundColor(ContextCompat.getColor(mContext, (R.color.accent_light)));
mRoutes.remove(route);
notifyDataSetChanged();
holder.isSelected = false;
} else {
holder.mView.setBackgroundColor(Color.TRANSPARENT);
holder.isSelected = true;
}
});
//Select first item by default
if (position == 0) {
holder.mView.setBackgroundColor(ContextCompat.getColor(mContext, (R.color.accent_light)));
holder.isSelected = true;
}
}
@Override
public int getItemCount() {
return mRoutes.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mName;
public final TextView mDistance;
public final TextView mSurfacingNatural;
public final TextView mSurfacingHard;
public final TextView mTime;
public final TextView mLevel;
public final TextView mElevationGain;
public final TextView mElevationLoss;
public CircleImageView mCircleImage;
public Route mItem;
public boolean isSelected;
public ViewHolder(View view) {
super(view);
mView = view;
mName = (TextView) view.findViewById(R.id.text_route_name);
mDistance = (TextView) view.findViewById(R.id.text_route_distance);
mSurfacingNatural = (TextView) view.findViewById(R.id.text_route_surfacing_naturel);
mSurfacingHard = (TextView) view.findViewById(R.id.text_route_surfacing_hard);
mTime = (TextView) view.findViewById(R.id.text_route_time);
mLevel = (TextView) view.findViewById(R.id.text_route_level);
mElevationGain = (TextView) view.findViewById(R.id.text_route_gain);
mElevationLoss = (TextView) view.findViewById(R.id.text_route_loss);
mCircleImage = (CircleImageView) view.findViewById(R.id.circle_image_route);
isSelected = false;
}
public String getName()
{
return (String) this.mName.getText();
}
@Override
public String toString() {
return super.toString() + " '" + mName.getText() + "'";
}
}
}
, как я создаю экземпляр адаптера в моем фрагменте:
recyclerView = (RecyclerView) view.findViewById(R.id.list_preview);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
mAdapter = new PreviewRecyclerViewAdapter(listRouteDayClicked, mCallback, getContext(), getActivity());
recyclerView.setAdapter(mAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
Я могу получить доступ к позиции, но как передать ее моему методу, который находится в моем фрагменте, а не в моем классе адаптера? Метод вызывается после нажатия на кнопку моего фрагмента