Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
Add MarqueeReplyView
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Dec 5, 2016
1 parent 98f187f commit 6a9d070
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 81 deletions.
85 changes: 5 additions & 80 deletions app/src/main/java/com/hippo/nimingban/ui/ListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
Expand All @@ -52,10 +51,8 @@
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.hippo.app.CheckBoxDialogBuilder;
import com.hippo.drawerlayout.DrawerLayout;
Expand Down Expand Up @@ -90,6 +87,7 @@
import com.hippo.nimingban.widget.FontTextView;
import com.hippo.nimingban.widget.LeftDrawer;
import com.hippo.nimingban.widget.LoadImageView;
import com.hippo.nimingban.widget.MarqueeReplyView;
import com.hippo.nimingban.widget.RightDrawer;
import com.hippo.ripple.Ripple;
import com.hippo.text.Html;
Expand All @@ -107,7 +105,6 @@
import com.hippo.yorozuya.Messenger;
import com.hippo.yorozuya.ObjectUtils;
import com.hippo.yorozuya.ResourcesUtils;
import com.hippo.yorozuya.SimpleHandler;

import java.io.IOException;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -394,7 +391,6 @@ private void resumeHolders() {
if (holder != null) {
// Only resume attached view holder
if (holder.itemView.getParent() != null) {
holder.resumeReplies();
holder.thumb.start();
}
} else {
Expand All @@ -414,7 +410,6 @@ private void pauseHolders() {
while (iterator.hasNext()) {
ListHolder holder = iterator.next().get();
if (holder != null) {
holder.pauseReplies();
holder.thumb.stop();
} else {
iterator.remove();
Expand Down Expand Up @@ -460,7 +455,6 @@ protected void onDestroy() {
for (WeakReference<ListHolder> ref : mListHolderList) {
ListHolder holder = ref.get();
if (holder != null) {
holder.clearReplies();
holder.thumb.unload();
}
}
Expand Down Expand Up @@ -1019,10 +1013,7 @@ public boolean onItemClick(EasyRecyclerView parent, View view, int position, lon
}
}

private class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
ViewSwitcher.ViewFactory, Runnable {

private Handler mHandler;
private class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView leftText;
public TextView centerText;
Expand All @@ -1031,31 +1022,22 @@ private class ListHolder extends RecyclerView.ViewHolder implements View.OnClick
public TextView bottomText;
public LoadImageView thumb;
public View bottom;
public TextSwitcher reply;

private int mShowIndex;
private Reply[] mReplies;

private boolean mRunning = false;
public MarqueeReplyView reply;

public ListHolder(View itemView) {
super(itemView);

mHandler = SimpleHandler.getInstance();

leftText = (TextView) itemView.findViewById(R.id.left_text);
centerText = (TextView) itemView.findViewById(R.id.center_text);
rightText = (TextView) itemView.findViewById(R.id.right_text);
content = (FontTextView) itemView.findViewById(R.id.content);
bottomText = (TextView) itemView.findViewById(R.id.bottom_text);
thumb = (LoadImageView) itemView.findViewById(R.id.thumb);
reply = (TextSwitcher) itemView.findViewById(R.id.reply);
reply = (MarqueeReplyView) itemView.findViewById(R.id.reply);
bottom = itemView.findViewById(R.id.bottom);

thumb.setOnClickListener(this);

reply.setFactory(this);

Drawable drawable = DrawableManager.getDrawable(ListActivity.this, R.drawable.v_comment_multiple_outline_x16);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
bottomText.setCompoundDrawables(drawable, null, null, null);
Expand All @@ -1080,71 +1062,16 @@ public void onClick(View v) {
}
}

public long getReplyInterval() {
return MathUtils.random(3000, 5001);
}

public boolean setReplies(Reply[] replies) {
mHandler.removeCallbacks(this);
mRunning = false;

if (replies == null || replies.length == 0) {
mReplies = null;
reply.setVisibility(View.INVISIBLE);
return false;
} else {
mReplies = replies;
mShowIndex = MathUtils.random(replies.length);
reply.setVisibility(View.VISIBLE);
reply.setCurrentText(replies[mShowIndex].getNMBDisplayContent());

if (replies.length > 1 && !mRunning) {
mRunning = true;
mHandler.postDelayed(this, getReplyInterval());
}

reply.setReplies(replies);
return true;
}
}

public void resumeReplies() {
if (mReplies != null && mReplies.length > 1 && !mRunning) {
mRunning = true;
mHandler.postDelayed(this, getReplyInterval());
}
}

public void pauseReplies() {
if (mReplies != null && mReplies.length > 1) {
mRunning = false;
mHandler.removeCallbacks(this);
}
}

public void clearReplies() {
mRunning = false;
mHandler.removeCallbacks(this);
mReplies = null;
}

@Override
public View makeView() {
return getLayoutInflater().inflate(R.layout.item_list_reply, reply, false);
}

@Override
public void run() {
if (mReplies == null) {
return;
}

mShowIndex = (mShowIndex + 1) % mReplies.length;
reply.setText(mReplies[mShowIndex].getNMBDisplayContent());

if (mRunning) {
mHandler.postDelayed(this, getReplyInterval());
}
}
}

private class PostAdapter extends RecyclerView.Adapter<ListHolder> {
Expand Down Expand Up @@ -1234,13 +1161,11 @@ public int getItemCount() {

@Override
public void onViewAttachedToWindow(ListHolder holder) {
holder.resumeReplies();
holder.thumb.start();
}

@Override
public void onViewDetachedFromWindow(ListHolder holder) {
holder.pauseReplies();
holder.thumb.stop();
}
}
Expand Down
118 changes: 118 additions & 0 deletions app/src/main/java/com/hippo/nimingban/widget/MarqueeReplyView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2016 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hippo.nimingban.widget;

/*
* Created by Hippo on 12/5/2016.
*/

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.ViewSwitcher;

import com.hippo.nimingban.R;
import com.hippo.nimingban.client.data.Reply;
import com.hippo.yorozuya.MathUtils;

public class MarqueeReplyView extends TextSwitcher {

private Reply[] mReplies;
private int mIndex;
private boolean mCanUpdate;
private boolean mShouldUpdate;

private final Runnable mInvalidateTask = new Runnable() {
@Override
public void run() {
mShouldUpdate = true;
invalidate();
}
};

private final ViewSwitcher.ViewFactory mReplyViewFactory = new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return LayoutInflater.from(getContext()).inflate(
R.layout.item_list_reply, MarqueeReplyView.this, false);
}
};

public MarqueeReplyView(Context context) {
super(context);
init();
}

public MarqueeReplyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
setWillNotDraw(false);
setFactory(mReplyViewFactory);
}

private boolean canMarquee() {
return mReplies != null && mReplies.length > 0;
}

private long getMarqueeInterval() {
return MathUtils.random(3000, 5001);
}

private void nextMarquee() {
stopMarquee();
if (canMarquee()) {
mCanUpdate = true;
postDelayed(mInvalidateTask, getMarqueeInterval());
}
}

private void stopMarquee() {
mCanUpdate = false;
removeCallbacks(mInvalidateTask);
}

public void setReplies(Reply[] replies) {
mReplies = replies;
mIndex = 0;
mCanUpdate = false;
mShouldUpdate = false;
if (replies != null && replies.length > 0) {
setCurrentText(replies[0].getNMBDisplayContent());
} else {
setCurrentText(null);
}
nextMarquee();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (mCanUpdate && mShouldUpdate && canMarquee()) {
mIndex = (mIndex + 1) % mReplies.length;
setText(mReplies[mIndex].getNMBDisplayContent());
nextMarquee();
}
mShouldUpdate = false;
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_list_include.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
android:layout_marginTop="10dp"
android:orientation="horizontal">

<TextSwitcher
<com.hippo.nimingban.widget.MarqueeReplyView
android:id="@+id/reply"
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 6a9d070

Please sign in to comment.