Create view holder

This commit is contained in:
PavelRatushny 2017-11-17 14:13:20 +02:00
parent 6cf247efd8
commit b010d0eb95

View file

@ -6,6 +6,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -56,38 +57,38 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
@NonNull @NonNull
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View v = convertView; View view = convertView;
final OsmPoint child = getItem(position); if (view == null) {
if (v == null) { view = LayoutInflater.from(getContext()).inflate(R.layout.note, parent, false);
v = LayoutInflater.from(getContext()).inflate(R.layout.note, parent, false); OsmEditViewHolder holder = new OsmEditViewHolder(view);
view.setTag(holder);
} }
OsmEditsFragment.getOsmEditView(v, child, app); final OsmPoint child = getItem(position);
v.findViewById(R.id.play).setVisibility(View.GONE); OsmEditsFragment.getOsmEditView(view, child, app);
final CheckBox ch = (CheckBox) v.findViewById(R.id.check_local_index); final OsmEditViewHolder holder = (OsmEditViewHolder) view.getTag();
View options = v.findViewById(R.id.options); holder.playImageButton.setVisibility(View.GONE);
if (selectionMode) { if (selectionMode) {
options.setVisibility(View.GONE); holder.optionsImageButton.setVisibility(View.GONE);
ch.setVisibility(View.VISIBLE); holder.selectCheckBox.setVisibility(View.VISIBLE);
ch.setChecked(selectedOsmEdits.contains(child)); holder.selectCheckBox.setChecked(selectedOsmEdits.contains(child));
v.findViewById(R.id.icon).setVisibility(View.GONE); holder.icon.setVisibility(View.GONE);
ch.setOnClickListener(new View.OnClickListener() { holder.selectCheckBox.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
selectItem(ch, child); selectItem(holder.selectCheckBox, child);
} }
}); });
} else { } else {
v.findViewById(R.id.icon).setVisibility(View.VISIBLE); holder.icon.setVisibility(View.VISIBLE);
options.setVisibility(View.VISIBLE); holder.optionsImageButton.setVisibility(View.VISIBLE);
ch.setVisibility(View.GONE); holder.selectCheckBox.setVisibility(View.GONE);
} }
((ImageView) options).setImageDrawable(app.getIconsCache() holder.optionsImageButton.setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_overflow_menu_white));
.getThemedIcon(R.drawable.ic_overflow_menu_white)); holder.optionsImageButton.setOnClickListener(new View.OnClickListener() {
options.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (listener != null) { if (listener != null) {
@ -95,12 +96,13 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
} }
} }
}); });
v.setOnClickListener(new View.OnClickListener() { holder.mainView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (selectionMode) { if (selectionMode) {
ch.setChecked(!ch.isChecked()); CheckBox checkBox = holder.selectCheckBox;
selectItem(ch, child); checkBox.setChecked(!checkBox.isChecked());
selectItem(checkBox, child);
} else { } else {
if (listener != null) { if (listener != null) {
listener.onItemShowMap(child); listener.onItemShowMap(child);
@ -109,7 +111,7 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
} }
}); });
return v; return view;
} }
private void selectItem(CheckBox checkBox, OsmPoint note) { private void selectItem(CheckBox checkBox, OsmPoint note) {
@ -123,6 +125,22 @@ public class OsmEditsAdapter extends ArrayAdapter<OsmPoint> {
} }
} }
private class OsmEditViewHolder {
View mainView;
ImageView icon;
ImageButton playImageButton;
CheckBox selectCheckBox;
ImageButton optionsImageButton;
OsmEditViewHolder(View view) {
mainView = view;
icon = (ImageView) view.findViewById(R.id.icon);
playImageButton = (ImageButton) view.findViewById(R.id.play);
selectCheckBox = (CheckBox) view.findViewById(R.id.check_local_index);
optionsImageButton = (ImageButton) view.findViewById(R.id.options);
}
}
public interface OsmEditsAdapterListener { public interface OsmEditsAdapterListener {
void onItemSelect(OsmPoint point); void onItemSelect(OsmPoint point);