Skip to content

Commit

Permalink
Merge pull request #31 from HugoGresse/fix/listExpandedBug
Browse files Browse the repository at this point in the history
Improve/fix expanded selection
  • Loading branch information
HugoGresse authored Dec 15, 2016
2 parents 408e793 + 3d1b590 commit f257d37
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public void onDestroy() {
* Toggle the Overlay/UI above the fullscreen content. This should be called by child fragment
*/
protected void toggleOverlayVisibility() {
if (mContentTextView.isShown()) {
// sometime mContentTextView is null, cf http://crashes.to/s/42615391f2d
if (mContentTextView != null && mContentTextView.isShown()) {
Log.d(TAG, "toggleOverlayVisibility to GONE");
mOverlayLinearLayout.setVisibility(View.GONE);
mGradientView.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public BaseAnecdoteViewHolder(View itemView) {
super(itemView);
}

public abstract void setData(int position, Anecdote anecdote);
public abstract void setData(int position, boolean isExpanded, Anecdote anecdote);

/**
* When the viewHolder is detached from the window = it's not visible anymore and will maybe be recycled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public ImageViewHolder(View itemView,
}

@Override
public void setData(int position, Anecdote anecdote) {
super.setData(position, anecdote);
public void setData(int position, boolean isExpanded, Anecdote anecdote) {
super.setData(position, isExpanded, anecdote);
String log = "setData: url:" + anecdote.media + " text:" + anecdote.text;

ViewCompat.setTransitionName(mImageView, String.valueOf(position) + "_image");
Expand Down Expand Up @@ -114,7 +114,7 @@ private void loadImage(){

@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if(mRetried < RETRY_COUNT){
if(mRetried < RETRY_COUNT && mImageUrl != null){
mRetried ++;
Log.d(TAG, "Retry " + mImageUrl + " retried?" + mRetried);
loadImage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ public class MixedBaseViewHolder
extends AnecdoteAdapter.BaseAnecdoteViewHolder
implements View.OnClickListener {

private int mExpandedPosition = -1;
protected final AdapterListener mAdapterListener;
protected final MixedContentAdapter mAdapter;
private final int mTextSize;
private final boolean mRowStriping;
private final int mRowBackground;
private final int mRowStripingBackground;
private final int mTextSize;
private final boolean mRowStriping;
private final int mRowBackground;
private final int mRowStripingBackground;

private View mItemView;

Expand Down Expand Up @@ -64,14 +63,14 @@ public MixedBaseViewHolder(View itemView,
}

@Override
public void setData(int position, Anecdote anecdote) {
public void setData(int position, boolean isExpanded, Anecdote anecdote) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mTextView.setText(Html.fromHtml(anecdote.text, Html.FROM_HTML_MODE_LEGACY));
} else {
try {
//noinspection deprecation
mTextView.setText(Html.fromHtml(anecdote.text));
} catch (Error error){
} catch (Error error) {
mTextView.setText(anecdote.text);
}
}
Expand All @@ -89,7 +88,7 @@ public void setData(int position, Anecdote anecdote) {
return;
}

if ( position == mExpandedPosition) {
if (isExpanded) {
mSeparatorView.setVisibility(View.VISIBLE);
mExpandLayout.setVisibility(View.VISIBLE);
((ViewGroup.MarginLayoutParams) itemView.getLayoutParams()).topMargin = 50;
Expand All @@ -110,22 +109,7 @@ public void setData(int position, Anecdote anecdote) {

@Override
public void onClick(View v) {
if (mExpandedPosition == getAdapterPosition()) {
mExpandedPosition = -1;
mAdapter.notifyItemChanged(getAdapterPosition());
return;
}
// Notify expanded last position
mAdapter.notifyItemChanged(mExpandedPosition);
mExpandedPosition = getAdapterPosition();
// Notify new element
mAdapter.notifyItemChanged(mExpandedPosition);
if (mAdapterListener != null) {
mAdapterListener.onClick(
mAdapter.getItem(getAdapterPosition()),
itemView,
AdapterListener.ACTION_OPEN_IN_BROWSER_PRELOAD);
}
mAdapter.toggleExpanded(getAdapterPosition());
}

@OnClick(R.id.shareButton)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class MixedContentAdapter
private boolean mRowStriping;
private int mRowBackground;
private int mRowStripingBackground;
private int mExpandedPosition = -1;

MixedContentAdapter(@Nullable AdapterListener adapterListener, boolean isSinglePage) {
mAnecdotes = new ArrayList<>();
Expand All @@ -62,7 +63,7 @@ public void setData(final List<Anecdote> quotes) {
quotes)
);

if(!mAnecdotes.isEmpty()){
if (!mAnecdotes.isEmpty()) {
mAnecdotes.clear();
mAnecdotes.addAll(quotes);
diffResult.dispatchUpdatesTo(this);
Expand Down Expand Up @@ -130,7 +131,7 @@ public void onBindViewHolder(BaseAnecdoteViewHolder holder, int position) {
@Override
public void onBindViewHolder(BaseAnecdoteViewHolder holder, int position, List<Object> payloads) {
if (position < mAnecdotes.size()) {
holder.setData(position, mAnecdotes.get(position));
holder.setData(position, mExpandedPosition == position, mAnecdotes.get(position));
}
}

Expand Down Expand Up @@ -173,6 +174,29 @@ public Anecdote getItem(int position) {
return mAnecdotes.get(position);
}

/**
* Set expanded item at the given position, used by viewHolder to prevent the adapter of a new expandedPosition and
* save it here
* @param position the expanded position
*/
public void toggleExpanded(int position){
if (mExpandedPosition == position) {
Log.d(TAG, "onClick close current ");
mExpandedPosition = -1;
notifyItemChanged(position);
return;
}

// Notify expanded last position
if(mExpandedPosition >= 0){
notifyItemChanged(mExpandedPosition);
}
mExpandedPosition = position;
// Notify new element
notifyItemChanged(mExpandedPosition);
mExpandedPosition = position;
}

private class LoadViewHolder extends BaseAnecdoteViewHolder {

LoadViewHolder(View itemView) {
Expand All @@ -182,7 +206,7 @@ private class LoadViewHolder extends BaseAnecdoteViewHolder {
}

@Override
public void setData(int position, Anecdote anecdote) {
public void setData(int position, boolean isExpanded, Anecdote anecdote) {
// This view is static, no need to change it's data
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public VideoViewHolder(View itemView,
}

@Override
public void setData(int position, Anecdote anecdote) {
super.setData(position, anecdote);
public void setData(int position, boolean isExpanded, Anecdote anecdote) {
super.setData(position, isExpanded, anecdote);
if (mPlayerView != null && anecdote.media != null) {
mPlayerView.setVideoUrl(anecdote.media);
}
Expand Down

0 comments on commit f257d37

Please sign in to comment.