From 3dcc410fd7a1600e429e69e424372e64f36862d4 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 8 Aug 2017 12:49:47 +0300 Subject: [PATCH] Replace ListView with RecyclerView --- .../res/layout/fragment_measurement_tool.xml | 4 +- .../MeasurementToolAdapter.java | 77 +++++++++++-------- .../MeasurementToolFragment.java | 17 ++-- 3 files changed, 54 insertions(+), 44 deletions(-) diff --git a/OsmAnd/res/layout/fragment_measurement_tool.xml b/OsmAnd/res/layout/fragment_measurement_tool.xml index b146c796ca..6e2250ea2e 100644 --- a/OsmAnd/res/layout/fragment_measurement_tool.xml +++ b/OsmAnd/res/layout/fragment_measurement_tool.xml @@ -90,8 +90,8 @@ - diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolAdapter.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolAdapter.java index 5062915c27..57ae8e9b63 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolAdapter.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolAdapter.java @@ -1,13 +1,9 @@ package net.osmand.plus.measurementtool; -import android.content.Context; -import android.support.annotation.LayoutRes; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; @@ -21,39 +17,44 @@ import net.osmand.util.MapUtils; import java.util.List; -class MeasurementToolAdapter extends ArrayAdapter { +class MeasurementToolAdapter extends RecyclerView.Adapter { - private final MapActivity mapActivity; - private final List points; - private RemovePointListener listener; + private MapActivity mapActivity; + private List points; + private RemovePointListener removePointListener; + private ItemClickListener itemClickListener; - MeasurementToolAdapter(@NonNull Context context, @LayoutRes int resource, List points) { - super(context, resource, points); - this.mapActivity = (MapActivity) context; + MeasurementToolAdapter(MapActivity mapActivity, List points) { + this.mapActivity = mapActivity; this.points = points; } void setRemovePointListener(RemovePointListener listener) { - this.listener = listener; + this.removePointListener = listener; + } + + public void setItemClickListener(ItemClickListener itemClickListener) { + this.itemClickListener = itemClickListener; } - @NonNull @Override - public View getView(final int pos, @Nullable View view, @NonNull ViewGroup parent) { - ViewHolder holder; - - if (view == null) { - view = LayoutInflater.from(parent.getContext()).inflate(R.layout.measure_points_list_item, parent, false); - final boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls(); - if (!nightMode) { - view.findViewById(R.id.points_divider).setBackgroundResource(R.drawable.divider); - } - holder = new ViewHolder(view); - view.setTag(holder); - } else { - holder = (ViewHolder) view.getTag(); + public Holder onCreateViewHolder(ViewGroup viewGroup, int i) { + View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.measure_points_list_item, viewGroup, false); + final boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls(); + if (!nightMode) { + view.findViewById(R.id.points_divider).setBackgroundResource(R.drawable.divider); } + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + itemClickListener.onItemClick(view); + } + }); + return new Holder(view); + } + @Override + public void onBindViewHolder(final Holder holder, int pos) { IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache(); holder.icon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_measure_point)); holder.title.setText(mapActivity.getString(R.string.plugin_distance_point) + " - " + (pos + 1)); @@ -71,22 +72,26 @@ class MeasurementToolAdapter extends ArrayAdapter { holder.deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - points.remove(pos); - listener.onPointRemove(); + points.remove(holder.getAdapterPosition()); + removePointListener.onPointRemove(); } }); - - return view; } - private static class ViewHolder { + @Override + public int getItemCount() { + return points.size(); + } + + static class Holder extends RecyclerView.ViewHolder { final ImageView icon; final TextView title; final TextView descr; final ImageButton deleteBtn; - ViewHolder(View view) { + Holder(View view) { + super(view); icon = (ImageView) view.findViewById(R.id.measure_point_icon); title = (TextView) view.findViewById(R.id.measure_point_title); descr = (TextView) view.findViewById(R.id.measure_point_descr); @@ -97,4 +102,8 @@ class MeasurementToolAdapter extends ArrayAdapter { interface RemovePointListener { void onPointRemove(); } -} + + interface ItemClickListener { + void onItemClick(View view); + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index d5d49242aa..9279ba54a2 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -10,6 +10,8 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SwitchCompat; import android.text.Editable; import android.text.TextWatcher; @@ -19,11 +21,9 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; -import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -200,7 +200,7 @@ public class MeasurementToolFragment extends Fragment { mapActivity.showTopToolbar(toolBarController); } - adapter = new MeasurementToolAdapter(getMapActivity(), R.layout.measure_points_list_item, measurementLayer.getMeasurementPoints()); + adapter = new MeasurementToolAdapter(getMapActivity(), measurementLayer.getMeasurementPoints()); adapter.setRemovePointListener(new MeasurementToolAdapter.RemovePointListener() { @Override public void onPointRemove() { @@ -215,12 +215,13 @@ public class MeasurementToolFragment extends Fragment { } } }); - ListView lv = mainView.findViewById(R.id.measure_points_list_view); - lv.setDivider(null); - lv.setAdapter(adapter); - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + final RecyclerView rv = mainView.findViewById(R.id.measure_points_recycler_view); + rv.setLayoutManager(new LinearLayoutManager(getContext())); + rv.setAdapter(adapter); + adapter.setItemClickListener(new MeasurementToolAdapter.ItemClickListener() { @Override - public void onItemClick(AdapterView adapterView, View view, int pos, long l) { + public void onItemClick(View view) { + int pos = rv.indexOfChild(view); measurementLayer.moveMapToPoint(pos); } });