Change landscape layout

This commit is contained in:
Alexander Sytnyk 2017-11-21 14:48:47 +02:00
parent 2aa354dab2
commit 8b523ea9c2
3 changed files with 208 additions and 108 deletions

View file

@ -7,9 +7,12 @@
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<include layout="@layout/list_item_divider"/>
<include
android:id="@+id/top_divider"
layout="@layout/list_item_divider"/>
<FrameLayout
android:id="@+id/background_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/bg_color">

View file

@ -117,11 +117,12 @@ public class NotesFragment extends OsmAndListFragment {
@Override
public void onResume() {
super.onResume();
boolean portrait = AndroidUiHelper.isOrientationPortrait(getActivity());
List<Object> items = createItemsList();
ListView listView = getListView();
listView.setDivider(null);
listView.setEmptyView(emptyView);
if (items.size() > 0 && footerView == null) {
if (items.size() > 0 && footerView == null && portrait) {
footerView = getActivity().getLayoutInflater().inflate(R.layout.list_shadow_footer, null, false);
listView.addFooterView(footerView);
listView.setHeaderDividersEnabled(false);
@ -131,6 +132,7 @@ public class NotesFragment extends OsmAndListFragment {
listAdapter.setSelectionMode(selectionMode);
listAdapter.setSelected(selected);
listAdapter.setListener(createAdapterListener());
listAdapter.setPortrait(portrait);
listView.setAdapter(listAdapter);
}
@ -294,7 +296,7 @@ public class NotesFragment extends OsmAndListFragment {
private void selectAll(int type) {
if (type == NotesAdapter.TYPE_DATE_HEADER) {
for (int i = 0; i < listAdapter.getCount(); i++) {
for (int i = 0; i < listAdapter.getItemsCount(); i++) {
Object item = listAdapter.getItem(i);
if (item instanceof Recording) {
selected.add((Recording) item);

View file

@ -2,13 +2,16 @@ package net.osmand.plus.audionotes.adapters;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.plus.OsmandApplication;
@ -30,10 +33,13 @@ public class NotesAdapter extends ArrayAdapter<Object> {
private OsmandApplication app;
private NotesAdapterListener listener;
private List<Object> items;
private boolean selectionMode;
private Set<Recording> selected;
private boolean portrait;
public void setListener(NotesAdapterListener listener) {
this.listener = listener;
}
@ -46,127 +52,88 @@ public class NotesAdapter extends ArrayAdapter<Object> {
this.selected = selected;
}
public void setPortrait(boolean portrait) {
this.portrait = portrait;
}
public NotesAdapter(OsmandApplication app, List<Object> items) {
super(app, R.layout.note, items);
this.app = app;
this.items = items;
}
@NonNull
@Override
public View getView(final int position, View row, @NonNull ViewGroup parent) {
final int type = getItemViewType(position);
boolean header = type == TYPE_DATE_HEADER
|| type == TYPE_AUDIO_HEADER
|| type == TYPE_PHOTO_HEADER
|| type == TYPE_VIDEO_HEADER;
if (portrait) {
final int type = getItemViewType(position);
boolean header = type == TYPE_DATE_HEADER
|| type == TYPE_AUDIO_HEADER
|| type == TYPE_PHOTO_HEADER
|| type == TYPE_VIDEO_HEADER;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (header) {
row = inflater.inflate(R.layout.list_item_header, parent, false);
HeaderViewHolder hHolder = new HeaderViewHolder(row);
row.setTag(hHolder);
} else {
row = inflater.inflate(R.layout.note_list_item, parent, false);
ItemViewHolder iHolder = new ItemViewHolder(row);
row.setTag(iHolder);
}
}
if (row == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (header) {
row = inflater.inflate(R.layout.list_item_header, parent, false);
HeaderViewHolder hHolder = new HeaderViewHolder(row);
row.setTag(hHolder);
setupHeader(type, (HeaderViewHolder) row.getTag());
} else {
row = inflater.inflate(R.layout.note_list_item, parent, false);
ItemViewHolder iHolder = new ItemViewHolder(row);
row.setTag(iHolder);
final Object item = getItem(position);
if (item instanceof Recording) {
setupItem(position, (Recording) item, (ItemViewHolder) row.getTag());
}
}
}
if (header) {
final HeaderViewHolder holder = (HeaderViewHolder) row.getTag();
holder.checkBox.setVisibility(selectionMode ? View.VISIBLE : View.GONE);
holder.headerRow.setEnabled(selectionMode);
if (selectionMode) {
holder.checkBox.setChecked(isSelectAllChecked(type));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onHeaderClick(type, holder.checkBox.isChecked());
}
}
});
holder.headerRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.checkBox.performClick();
}
});
} else {
holder.view.setOnClickListener(null);
}
int titleId;
if (type == TYPE_DATE_HEADER) {
titleId = R.string.notes_by_date;
} else if (type == TYPE_AUDIO_HEADER) {
titleId = R.string.shared_string_audio;
} else if (type == TYPE_PHOTO_HEADER) {
titleId = R.string.shared_string_photo;
} else {
titleId = R.string.shared_string_video;
}
holder.title.setText(titleId);
return row;
} else {
final Object item = getItem(position);
if (item instanceof Recording) {
final Recording recording = (Recording) item;
final ItemViewHolder holder = (ItemViewHolder) row.getTag();
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
boolean lastCard = getHeadersCount() == position + 1;
int margin = app.getResources().getDimensionPixelSize(R.dimen.content_padding);
int sideMargin = app.getResources().getDisplayMetrics().widthPixels / 10;
if (recording == NotesFragment.SHARE_LOCATION_FILE) {
holder.title.setText(R.string.av_locations);
holder.description.setText(R.string.av_locations_descr);
FrameLayout fl = new FrameLayout(getContext());
LinearLayout ll = new LinearLayout(getContext());
fl.addView(ll);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundResource(app.getSettings().isLightContent() ? R.drawable.bg_card_light : R.drawable.bg_card_dark);
((FrameLayout.LayoutParams) ll.getLayoutParams()).setMargins(sideMargin, margin, sideMargin, lastCard ? margin : 0);
int headerInd = getHeaderIndex(position);
HeaderViewHolder headerVH = new HeaderViewHolder(inflater.inflate(R.layout.list_item_header, parent, false));
setupHeader((int) items.get(headerInd), headerVH);
ll.addView(headerVH.view);
for (int i = headerInd + 1; i < items.size(); i++) {
Object item = items.get(i);
if (item instanceof Recording) {
ItemViewHolder itemVH = new ItemViewHolder(inflater.inflate(R.layout.note_list_item, parent, false));
setupItem(i, (Recording) item, itemVH);
ll.addView(itemVH.view);
} else {
holder.title.setText(recording.getName(app, true));
holder.description.setText(recording.getNewSmallDescription(app));
int iconRes = recording.isAudio() ? R.drawable.ic_type_audio
: (recording.isVideo() ? R.drawable.ic_type_video : R.drawable.ic_type_img);
int colorRes = app.getSettings().isLightContent() ? R.color.icon_color : R.color.ctx_menu_info_text_dark;
holder.icon.setImageDrawable(app.getIconsCache().getIcon(iconRes, colorRes));
break;
}
holder.bottomDivider.setVisibility(hideBottomDivider(position) ? View.GONE : View.VISIBLE);
holder.icon.setVisibility(selectionMode ? View.GONE : View.VISIBLE);
holder.checkBox.setVisibility(selectionMode ? View.VISIBLE : View.GONE);
holder.options.setVisibility(selectionMode ? View.GONE : View.VISIBLE);
if (selectionMode) {
holder.checkBox.setChecked(selected.contains(recording));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onCheckBoxClick(recording, holder.checkBox.isChecked());
}
}
});
} else {
holder.options.setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_overflow_menu_white));
holder.options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onOptionsClick(recording);
}
}
});
}
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectionMode) {
holder.checkBox.performClick();
} else {
if (listener != null) {
listener.onItemClick(recording);
}
}
}
});
}
}
return row;
return fl;
}
}
@Override
public int getCount() {
if (portrait) {
return super.getCount();
}
return getHeadersCount();
}
@Override
@ -183,13 +150,137 @@ public class NotesAdapter extends ArrayAdapter<Object> {
return TYPE_COUNT;
}
private void setupHeader(final int type, final HeaderViewHolder holder) {
setupBackground(holder.backgroundView);
holder.topDivider.setVisibility(portrait ? View.VISIBLE : View.GONE);
holder.checkBox.setVisibility(selectionMode ? View.VISIBLE : View.GONE);
holder.headerRow.setEnabled(selectionMode);
if (selectionMode) {
holder.checkBox.setChecked(isSelectAllChecked(type));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onHeaderClick(type, holder.checkBox.isChecked());
}
}
});
holder.headerRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.checkBox.performClick();
}
});
} else {
holder.view.setOnClickListener(null);
}
holder.title.setText(getHeaderTitleRes(type));
}
private int getHeaderTitleRes(int type) {
if (type == TYPE_DATE_HEADER) {
return R.string.notes_by_date;
} else if (type == TYPE_AUDIO_HEADER) {
return R.string.shared_string_audio;
} else if (type == TYPE_PHOTO_HEADER) {
return R.string.shared_string_photo;
}
return R.string.shared_string_video;
}
private void setupItem(int position, final Recording recording, final ItemViewHolder holder) {
setupBackground(holder.view);
if (recording == NotesFragment.SHARE_LOCATION_FILE) {
holder.title.setText(R.string.av_locations);
holder.description.setText(R.string.av_locations_descr);
} else {
holder.title.setText(recording.getName(app, true));
holder.description.setText(recording.getNewSmallDescription(app));
int iconRes = recording.isAudio() ? R.drawable.ic_type_audio
: (recording.isVideo() ? R.drawable.ic_type_video : R.drawable.ic_type_img);
int colorRes = app.getSettings().isLightContent() ? R.color.icon_color : R.color.ctx_menu_info_text_dark;
holder.icon.setImageDrawable(app.getIconsCache().getIcon(iconRes, colorRes));
}
holder.bottomDivider.setVisibility(hideBottomDivider(position) ? View.GONE : View.VISIBLE);
holder.icon.setVisibility(selectionMode ? View.GONE : View.VISIBLE);
holder.checkBox.setVisibility(selectionMode ? View.VISIBLE : View.GONE);
holder.options.setVisibility(selectionMode ? View.GONE : View.VISIBLE);
if (selectionMode) {
holder.checkBox.setChecked(selected.contains(recording));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onCheckBoxClick(recording, holder.checkBox.isChecked());
}
}
});
} else {
holder.options.setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_overflow_menu_white));
holder.options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onOptionsClick(recording);
}
}
});
}
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectionMode) {
holder.checkBox.performClick();
} else {
if (listener != null) {
listener.onItemClick(recording);
}
}
}
});
}
private void setupBackground(View view) {
if (!portrait) {
view.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_transparent));
}
}
public int getItemsCount() {
return items.size();
}
private int getHeadersCount() {
int res = 0;
for (Object item : items) {
if (item instanceof Integer) {
res++;
}
}
return res;
}
private int getHeaderIndex(int position) {
int count = 0;
for (int i = 0; i < items.size(); i++) {
if (items.get(i) instanceof Integer) {
if (count == position) {
return i;
}
count++;
}
}
return -1;
}
private boolean hideBottomDivider(int pos) {
return pos == getCount() - 1 || !(getItem(pos + 1) instanceof Recording);
return pos == items.size() - 1 || !(getItem(pos + 1) instanceof Recording);
}
private boolean isSelectAllChecked(int type) {
for (int i = 0; i < getCount(); i++) {
Object item = getItem(i);
for (Object item : items) {
if (item instanceof Recording) {
if (type != TYPE_DATE_HEADER && !isAppropriate((Recording) item, type)) {
continue;
@ -214,12 +305,16 @@ public class NotesAdapter extends ArrayAdapter<Object> {
private class HeaderViewHolder {
final View view;
final View topDivider;
final View backgroundView;
final View headerRow;
final CheckBox checkBox;
final TextView title;
HeaderViewHolder(View view) {
this.view = view;
topDivider = view.findViewById(R.id.top_divider);
backgroundView = view.findViewById(R.id.background_view);
headerRow = view.findViewById(R.id.header_row);
checkBox = (CheckBox) view.findViewById(R.id.check_box);
title = (TextView) view.findViewById(R.id.title_text_view);