Replace ListView with RecyclerView

This commit is contained in:
Alexander Sytnyk 2017-08-08 12:49:47 +03:00
parent 5ded35937e
commit 3dcc410fd7
3 changed files with 54 additions and 44 deletions

View file

@ -90,8 +90,8 @@
<include layout="@layout/card_bottom_divider"/>
<ListView
android:id="@+id/measure_points_list_view"
<android.support.v7.widget.RecyclerView
android:id="@+id/measure_points_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

View file

@ -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<WptPt> {
class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementToolAdapter.Holder> {
private final MapActivity mapActivity;
private final List<WptPt> points;
private RemovePointListener listener;
private MapActivity mapActivity;
private List<WptPt> points;
private RemovePointListener removePointListener;
private ItemClickListener itemClickListener;
MeasurementToolAdapter(@NonNull Context context, @LayoutRes int resource, List<WptPt> points) {
super(context, resource, points);
this.mapActivity = (MapActivity) context;
MeasurementToolAdapter(MapActivity mapActivity, List<WptPt> 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<WptPt> {
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<WptPt> {
interface RemovePointListener {
void onPointRemove();
}
}
interface ItemClickListener {
void onItemClick(View view);
}
}

View file

@ -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);
}
});