如果您使用过Google Plus Android应用,则会注意到不同评论之间不断轮换的评论Feed。
下面是它的截图:
有没有人知道如何实施类似的方法?我不是要求任何代码,而是想要了解如何实现它的基本思路。
请使用Google Plus Android应用并查看帖子评论的行为。你会明白我在问什么。
答案 0 :(得分:1)
我怀疑这是一个与此类似的自定义View
,请记得从xml手动调整宽度和高度:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
public class RotatingView extends View {
public static class RotatingData {
String data;
public RotatingData(String data) {
super();
this.data = data;
}
}
public interface OnItemRotatedListener {
public void onItemRotated(RotatingData data);
}
private List<RotatingData> mList;
private Paint mPaint;
private Handler mHandler;
private int mCurrentSelection;
private OnItemRotatedListener mItemRotatedListener;
private Random mRandom;
public RotatingView(Context context) {
this(context, null);
}
public RotatingView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RotatingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mHandler = new Handler();
mRandom = new Random();
List<RotatingData> list = new ArrayList<RotatingView.RotatingData>();
list.add(new RotatingData("0th item selected"));
list.add(new RotatingData("1st item selected"));
list.add(new RotatingData("2nd item selected"));
setRotatingData(list);
setSelection(0);
}
private void setSelection(int i) {
mCurrentSelection = i;
invokeListener(mList.get(i));
invalidate();
}
public void setRotatingData(List<RotatingData> list) {
mList = list;
invalidate();
}
public OnItemRotatedListener getItemRotatedListener() {
return mItemRotatedListener;
}
public void setItemRotatedListener(OnItemRotatedListener itemRotatedListener) {
mItemRotatedListener = itemRotatedListener;
invokeListener(mList.get(mCurrentSelection));
}
private void invokeListener(RotatingData data) {
if (mItemRotatedListener != null) {
mItemRotatedListener.onItemRotated(data);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final List<RotatingData> list = mList;
final int count = list.size();
final float w = getMeasuredWidth() / (float) count;
final int h = getMeasuredHeight();
int left = 0;
final Paint paint = mPaint;
for (int i = 0; i < list.size(); i++) {
if (i == mCurrentSelection) {
paint.setColor(Color.DKGRAY);
} else {
paint.setColor(Color.LTGRAY);
}
canvas.drawRect(left, 0, left + w, h, paint);
left += w;
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
startRotating();
}
private void startRotating() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mCurrentSelection++;
mCurrentSelection %= mList.size();
setSelection(mCurrentSelection);
mHandler.postDelayed(this, mRandom.nextInt(1000) + 1000);
}
}, mRandom.nextInt(1000) + 1000);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
stopRotating();
}
private void stopRotating() {
mHandler.removeCallbacksAndMessages(null);
}
}