|
|
|
|
@ -20,73 +20,97 @@ import com.monke.monkeybook.utils.DensityUtil;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
// ContentSwitchView类继承自FrameLayout,并实现了BookContentView.SetDataListener接口
|
|
|
|
|
public class ContentSwitchView extends FrameLayout implements BookContentView.SetDataListener {
|
|
|
|
|
// 获取屏幕宽度
|
|
|
|
|
private final int screenWidth = DensityUtil.getWindowWidth(getContext());
|
|
|
|
|
// 动画持续时间
|
|
|
|
|
private final long animDuration = 300;
|
|
|
|
|
// 表示没有上一页和下一页的状态常量
|
|
|
|
|
public final static int NONE = -1;
|
|
|
|
|
// 表示既有上一页又有下一页的状态常量
|
|
|
|
|
public final static int PREANDNEXT = 0;
|
|
|
|
|
// 表示只有上一页的状态常量
|
|
|
|
|
public final static int ONLYPRE = 1;
|
|
|
|
|
// 表示只有下一页的状态常量
|
|
|
|
|
public final static int ONLYNEXT = 2;
|
|
|
|
|
private int state = NONE; //0是有上一页 也有下一页 ; 2是只有下一页 ;1是只有上一页;-1是没有上一页 也没有下一页;
|
|
|
|
|
|
|
|
|
|
// 当前视图的状态,初始化为NONE
|
|
|
|
|
private int state = NONE;
|
|
|
|
|
// 横向滚动的偏移量
|
|
|
|
|
private int scrollX;
|
|
|
|
|
// 表示是否正在移动的标志
|
|
|
|
|
private Boolean isMoving = false;
|
|
|
|
|
|
|
|
|
|
// 当前显示的页面视图
|
|
|
|
|
private BookContentView durPageView;
|
|
|
|
|
// 存储所有页面视图的列表
|
|
|
|
|
private List<BookContentView> viewContents;
|
|
|
|
|
|
|
|
|
|
// 定义一个接口,用于在书籍阅读初始化成功时回调
|
|
|
|
|
public interface OnBookReadInitListener {
|
|
|
|
|
public void success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 书籍阅读初始化监听器
|
|
|
|
|
private OnBookReadInitListener bookReadInitListener;
|
|
|
|
|
|
|
|
|
|
// 构造函数,用于在代码中创建视图时初始化
|
|
|
|
|
public ContentSwitchView(Context context) {
|
|
|
|
|
super(context);
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造函数,用于在XML布局中创建视图时初始化,并接收属性集
|
|
|
|
|
public ContentSwitchView(Context context, AttributeSet attrs) {
|
|
|
|
|
super(context, attrs);
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造函数,用于在XML布局中创建视图时初始化,并接收属性集和默认样式属性
|
|
|
|
|
public ContentSwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造函数,用于在XML布局中创建视图时初始化,并接收属性集、默认样式属性和特定版本的样式资源
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
|
public ContentSwitchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
|
|
|
|
super(context, attrs, defStyleAttr, defStyleRes);
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 读取书籍控制实例
|
|
|
|
|
private ReadBookControl readBookControl;
|
|
|
|
|
|
|
|
|
|
// 初始化方法
|
|
|
|
|
private void init() {
|
|
|
|
|
readBookControl = ReadBookControl.getInstance();
|
|
|
|
|
|
|
|
|
|
// 将30dp转换为像素值,作为横向滚动的偏移量
|
|
|
|
|
scrollX = DensityUtil.dp2px(getContext(), 30f);
|
|
|
|
|
// 创建当前页面视图
|
|
|
|
|
durPageView = new BookContentView(getContext());
|
|
|
|
|
// 设置当前页面视图的书籍控制实例
|
|
|
|
|
durPageView.setReadBookControl(readBookControl);
|
|
|
|
|
|
|
|
|
|
viewContents = new ArrayList<>();
|
|
|
|
|
viewContents.add(durPageView);
|
|
|
|
|
|
|
|
|
|
// 将当前页面视图添加到布局中
|
|
|
|
|
addView(durPageView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 用于开始书籍阅读的初始化,并设置初始化监听器
|
|
|
|
|
public void bookReadInit(OnBookReadInitListener bookReadInitListener) {
|
|
|
|
|
this.bookReadInitListener = bookReadInitListener;
|
|
|
|
|
durPageView.getTvContent().getViewTreeObserver().addOnGlobalLayoutListener(layoutInitListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 开始加载数据
|
|
|
|
|
public void startLoading() {
|
|
|
|
|
int height = durPageView.getTvContent().getHeight();
|
|
|
|
|
if (height > 0) {
|
|
|
|
|
if (loadDataListener != null && durHeight != height) {
|
|
|
|
|
if (loadDataListener!= null && durHeight!= height) {
|
|
|
|
|
durHeight = height;
|
|
|
|
|
loadDataListener.initData(durPageView.getLineCount(height));
|
|
|
|
|
}
|
|
|
|
|
@ -94,18 +118,22 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
durPageView.getTvContent().getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 测量视图的大小
|
|
|
|
|
@Override
|
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 触摸事件的起始X坐标
|
|
|
|
|
private float startX = -1;
|
|
|
|
|
|
|
|
|
|
// 处理触摸事件
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
|
int action = event.getAction();
|
|
|
|
|
if (!isMoving) {
|
|
|
|
|
int durWidth = screenWidth > 1400 ? 10 : 0; //当分辨率过大时,添加横向滑动冗余值
|
|
|
|
|
// 当屏幕宽度大于1400时,设置横向滑动冗余值为10,否则为0
|
|
|
|
|
int durWidth = screenWidth > 1400? 10 : 0;
|
|
|
|
|
switch (action) {
|
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
|
startX = event.getX();
|
|
|
|
|
@ -115,7 +143,7 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
if (startX == -1)
|
|
|
|
|
startX = event.getX();
|
|
|
|
|
|
|
|
|
|
//处理分辨率过大,移动冗余值,当横向滑动值超过冗余值则开始滑动
|
|
|
|
|
// 处理分辨率过大时的横向滑动冗余值
|
|
|
|
|
int durX = (int) (event.getX() - startX);
|
|
|
|
|
if(durX>durWidth){
|
|
|
|
|
durX = durX - durWidth;
|
|
|
|
|
@ -138,62 +166,62 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
tempX = 0;
|
|
|
|
|
else if (tempX < -getWidth())
|
|
|
|
|
tempX = -getWidth();
|
|
|
|
|
int tempIndex = (state == PREANDNEXT ? 1 : 0);
|
|
|
|
|
int tempIndex = (state == PREANDNEXT? 1 : 0);
|
|
|
|
|
viewContents.get(tempIndex).layout(tempX, viewContents.get(tempIndex).getTop(), tempX + getWidth(), viewContents.get(tempIndex).getBottom());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case MotionEvent.ACTION_CANCEL: //小米8长按传送门会引导手势进入action_cancel
|
|
|
|
|
case MotionEvent.ACTION_CANCEL: // 小米8长按传送门会引导手势进入action_cancel
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
if (startX == -1)
|
|
|
|
|
startX = event.getX();
|
|
|
|
|
if (event.getX() - startX > durWidth) {
|
|
|
|
|
if (state == PREANDNEXT || state == ONLYPRE) {
|
|
|
|
|
//注意冗余值
|
|
|
|
|
// 注意冗余值,判断是否向前翻页成功
|
|
|
|
|
if (event.getX() - startX + durWidth> scrollX) {
|
|
|
|
|
//向前翻页成功
|
|
|
|
|
// 向前翻页成功
|
|
|
|
|
initMoveSuccessAnim(viewContents.get(0), 0);
|
|
|
|
|
} else {
|
|
|
|
|
initMoveFailAnim(viewContents.get(0), -getWidth());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//没有上一页
|
|
|
|
|
// 没有上一页
|
|
|
|
|
noPre();
|
|
|
|
|
}
|
|
|
|
|
} else if (event.getX() - startX < -durWidth) {
|
|
|
|
|
if (state == PREANDNEXT || state == ONLYNEXT) {
|
|
|
|
|
int tempIndex = (state == PREANDNEXT ? 1 : 0);
|
|
|
|
|
//注意冗余值
|
|
|
|
|
int tempIndex = (state == PREANDNEXT? 1 : 0);
|
|
|
|
|
// 注意冗余值,判断是否向后翻页成功
|
|
|
|
|
if (startX - event.getX() - durWidth > scrollX) {
|
|
|
|
|
//向后翻页成功
|
|
|
|
|
// 向后翻页成功
|
|
|
|
|
initMoveSuccessAnim(viewContents.get(tempIndex), -getWidth());
|
|
|
|
|
} else {
|
|
|
|
|
initMoveFailAnim(viewContents.get(tempIndex), 0);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//没有下一页
|
|
|
|
|
// 没有下一页
|
|
|
|
|
noNext();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//点击事件
|
|
|
|
|
// 点击事件
|
|
|
|
|
if (readBookControl.getCanClickTurn() && event.getX() <= getWidth() / 3) {
|
|
|
|
|
//点击向前翻页
|
|
|
|
|
// 点击向前翻页
|
|
|
|
|
if (state == PREANDNEXT || state == ONLYPRE) {
|
|
|
|
|
initMoveSuccessAnim(viewContents.get(0), 0);
|
|
|
|
|
} else {
|
|
|
|
|
noPre();
|
|
|
|
|
}
|
|
|
|
|
} else if (readBookControl.getCanClickTurn() && event.getX() >= getWidth() / 3 * 2) {
|
|
|
|
|
//点击向后翻页
|
|
|
|
|
// 点击向后翻页
|
|
|
|
|
if (state == PREANDNEXT || state == ONLYNEXT) {
|
|
|
|
|
int tempIndex = (state == PREANDNEXT ? 1 : 0);
|
|
|
|
|
int tempIndex = (state == PREANDNEXT? 1 : 0);
|
|
|
|
|
initMoveSuccessAnim(viewContents.get(tempIndex), -getWidth());
|
|
|
|
|
} else {
|
|
|
|
|
noNext();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//点击中间部位
|
|
|
|
|
if (loadDataListener != null)
|
|
|
|
|
// 点击中间部位
|
|
|
|
|
if (loadDataListener!= null)
|
|
|
|
|
loadDataListener.showMenu();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -206,6 +234,7 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
return super.onTouchEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 布局子视图
|
|
|
|
|
@Override
|
|
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
|
|
|
if (viewContents.size() > 0) {
|
|
|
|
|
@ -227,14 +256,16 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化翻页成功的动画
|
|
|
|
|
private void initMoveSuccessAnim(final View view, final int orderX) {
|
|
|
|
|
if (null != view) {
|
|
|
|
|
if (null!= view) {
|
|
|
|
|
// 根据视图的移动距离和屏幕宽度计算动画持续时间
|
|
|
|
|
long temp = Math.abs(view.getLeft() - orderX) / (getWidth() / animDuration);
|
|
|
|
|
ValueAnimator tempAnim = ValueAnimator.ofInt(view.getLeft(), orderX).setDuration(temp);
|
|
|
|
|
tempAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
|
|
if (null != view) {
|
|
|
|
|
if (null!= view) {
|
|
|
|
|
int value = (int) animation.getAnimatedValue();
|
|
|
|
|
view.layout(value, view.getTop(), value + getWidth(), view.getBottom());
|
|
|
|
|
}
|
|
|
|
|
@ -250,9 +281,10 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
isMoving = false;
|
|
|
|
|
if (orderX == 0) {
|
|
|
|
|
//翻向前一页
|
|
|
|
|
// 翻向前一页
|
|
|
|
|
durPageView = viewContents.get(0);
|
|
|
|
|
if (state == PREANDNEXT) {
|
|
|
|
|
// 移除最后一页视图
|
|
|
|
|
ContentSwitchView.this.removeView(viewContents.get(viewContents.size() - 1));
|
|
|
|
|
viewContents.remove(viewContents.size() - 1);
|
|
|
|
|
}
|
|
|
|
|
@ -264,11 +296,12 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
else state = PREANDNEXT;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//翻向后一夜
|
|
|
|
|
// 翻向后一页
|
|
|
|
|
if (state == ONLYNEXT) {
|
|
|
|
|
durPageView = viewContents.get(1);
|
|
|
|
|
} else {
|
|
|
|
|
durPageView = viewContents.get(2);
|
|
|
|
|
// 移除第一页视图
|
|
|
|
|
ContentSwitchView.this.removeView(viewContents.get(0));
|
|
|
|
|
viewContents.remove(0);
|
|
|
|
|
}
|
|
|
|
|
@ -280,7 +313,7 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
else state = PREANDNEXT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (loadDataListener != null)
|
|
|
|
|
if (loadDataListener!= null)
|
|
|
|
|
loadDataListener.updateProgress(durPageView.getDurChapterIndex(), durPageView.getDurPageIndex());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -298,14 +331,15 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化翻页失败的动画
|
|
|
|
|
private void initMoveFailAnim(final View view, int orderX) {
|
|
|
|
|
if (null != view) {
|
|
|
|
|
if (null!= view) {
|
|
|
|
|
long temp = Math.abs(view.getLeft() - orderX) / (getWidth() / animDuration);
|
|
|
|
|
ValueAnimator tempAnim = ValueAnimator.ofInt(view.getLeft(), orderX).setDuration(temp);
|
|
|
|
|
tempAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
|
|
if (null != view) {
|
|
|
|
|
if (null!= view) {
|
|
|
|
|
int value = (int) animation.getAnimatedValue();
|
|
|
|
|
view.layout(value, view.getTop(), value + getWidth(), view.getBottom());
|
|
|
|
|
}
|
|
|
|
|
@ -315,27 +349,29 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置初始数据
|
|
|
|
|
public void setInitData(int durChapterIndex, int chapterAll, int durPageIndex) {
|
|
|
|
|
updateOtherPage(durChapterIndex, chapterAll, durPageIndex, -1);
|
|
|
|
|
durPageView.setLoadDataListener(loadDataListener, this);
|
|
|
|
|
durPageView.loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex) : "", durChapterIndex, chapterAll, durPageIndex);
|
|
|
|
|
durPageView.loadData(null!= loadDataListener? loadDataListener.getChapterTitle(durChapterIndex) : "", durChapterIndex, chapterAll, durPageIndex);
|
|
|
|
|
|
|
|
|
|
if (loadDataListener != null)
|
|
|
|
|
if (loadDataListener!= null)
|
|
|
|
|
loadDataListener.updateProgress(durPageView.getDurChapterIndex(), durPageView.getDurPageIndex());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据当前页面的位置和总页数等信息更新其他页面的状态和视图
|
|
|
|
|
private void updateOtherPage(int durChapterIndex, int chapterAll, int durPageIndex, int pageAll) {
|
|
|
|
|
if (chapterAll > 1 || pageAll > 1) {
|
|
|
|
|
if ((durChapterIndex == 0 && pageAll == -1) || (durChapterIndex == 0 && durPageIndex == 0 && pageAll != -1)) {
|
|
|
|
|
//ONLYNEXT
|
|
|
|
|
if ((durChapterIndex == 0 && pageAll == -1) || (durChapterIndex == 0 && durPageIndex == 0 && pageAll!= -1)) {
|
|
|
|
|
// ONLYNEXT,只有下一页的情况
|
|
|
|
|
addNextPage(durChapterIndex, chapterAll, durPageIndex, pageAll);
|
|
|
|
|
if (state == ONLYPRE || state == PREANDNEXT) {
|
|
|
|
|
this.removeView(viewContents.get(0));
|
|
|
|
|
viewContents.remove(0);
|
|
|
|
|
}
|
|
|
|
|
state = ONLYNEXT;
|
|
|
|
|
} else if ((durChapterIndex == chapterAll - 1 && pageAll == -1) || (durChapterIndex == chapterAll - 1 && durPageIndex == pageAll - 1 && pageAll != -1)) {
|
|
|
|
|
//ONLYPRE
|
|
|
|
|
} else if ((durChapterIndex == chapterAll - 1 && pageAll == -1) || (durChapterIndex == chapterAll - 1 && durPageIndex == pageAll - 1 && pageAll!= -1)) {
|
|
|
|
|
// ONLYPRE,只有上一页的情况
|
|
|
|
|
addPrePage(durChapterIndex, chapterAll, durPageIndex, pageAll);
|
|
|
|
|
if (state == ONLYNEXT || state == PREANDNEXT) {
|
|
|
|
|
this.removeView(viewContents.get(2));
|
|
|
|
|
@ -343,13 +379,13 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
}
|
|
|
|
|
state = ONLYPRE;
|
|
|
|
|
} else {
|
|
|
|
|
//PREANDNEXT
|
|
|
|
|
// PREANDNEXT,既有上一页又有下一页的情况
|
|
|
|
|
addNextPage(durChapterIndex, chapterAll, durPageIndex, pageAll);
|
|
|
|
|
addPrePage(durChapterIndex, chapterAll, durPageIndex, pageAll);
|
|
|
|
|
state = PREANDNEXT;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//NONE
|
|
|
|
|
// NONE,没有上一页和下一页的情况
|
|
|
|
|
if (state == ONLYPRE) {
|
|
|
|
|
this.removeView(viewContents.get(0));
|
|
|
|
|
viewContents.remove(0);
|
|
|
|
|
@ -358,180 +394,4 @@ public class ContentSwitchView extends FrameLayout implements BookContentView.Se
|
|
|
|
|
viewContents.remove(1);
|
|
|
|
|
} else if (state == PREANDNEXT) {
|
|
|
|
|
this.removeView(viewContents.get(0));
|
|
|
|
|
this.removeView(viewContents.get(2));
|
|
|
|
|
viewContents.remove(2);
|
|
|
|
|
viewContents.remove(0);
|
|
|
|
|
}
|
|
|
|
|
state = NONE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addNextPage(int durChapterIndex, int chapterAll, int durPageIndex, int pageAll) {
|
|
|
|
|
if (state == ONLYNEXT || state == PREANDNEXT) {
|
|
|
|
|
int temp = (state == ONLYNEXT ? 1 : 2);
|
|
|
|
|
if (pageAll > 0 && durPageIndex >= 0 && durPageIndex < pageAll - 1)
|
|
|
|
|
viewContents.get(temp).loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex) : "", durChapterIndex, chapterAll, durPageIndex + 1);
|
|
|
|
|
else
|
|
|
|
|
viewContents.get(temp).loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex + 1) : "", durChapterIndex + 1, chapterAll, BookContentView.DURPAGEINDEXBEGIN);
|
|
|
|
|
} else if (state == ONLYPRE || state == NONE) {
|
|
|
|
|
BookContentView next = new BookContentView(getContext());
|
|
|
|
|
next.setReadBookControl(readBookControl);
|
|
|
|
|
next.setLoadDataListener(loadDataListener, this);
|
|
|
|
|
if (pageAll > 0 && durPageIndex >= 0 && durPageIndex < pageAll - 1)
|
|
|
|
|
next.loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex) : "", durChapterIndex, chapterAll, durPageIndex + 1);
|
|
|
|
|
else
|
|
|
|
|
next.loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex + 1) : "", durChapterIndex + 1, chapterAll, BookContentView.DURPAGEINDEXBEGIN);
|
|
|
|
|
viewContents.add(next);
|
|
|
|
|
this.addView(next, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addPrePage(int durChapterIndex, int chapterAll, int durPageIndex, int pageAll) {
|
|
|
|
|
if (state == ONLYNEXT || state == NONE) {
|
|
|
|
|
BookContentView pre = new BookContentView(getContext());
|
|
|
|
|
pre.setReadBookControl(readBookControl);
|
|
|
|
|
pre.setLoadDataListener(loadDataListener, this);
|
|
|
|
|
if (pageAll > 0 && durPageIndex >= 0 && durPageIndex > 0)
|
|
|
|
|
pre.loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex) : "", durChapterIndex, chapterAll, durPageIndex - 1);
|
|
|
|
|
else
|
|
|
|
|
pre.loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex - 1) : "", durChapterIndex - 1, chapterAll, BookContentView.DURPAGEINDEXEND);
|
|
|
|
|
viewContents.add(0, pre);
|
|
|
|
|
this.addView(pre);
|
|
|
|
|
} else if (state == ONLYPRE || state == PREANDNEXT) {
|
|
|
|
|
if (pageAll > 0 && durPageIndex >= 0 && durPageIndex > 0)
|
|
|
|
|
viewContents.get(0).loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex) : "", durChapterIndex, chapterAll, durPageIndex - 1);
|
|
|
|
|
else
|
|
|
|
|
viewContents.get(0).loadData(null != loadDataListener ? loadDataListener.getChapterTitle(durChapterIndex - 1) : "", durChapterIndex - 1, chapterAll, BookContentView.DURPAGEINDEXEND);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setDataFinish(BookContentView bookContentView, int durChapterIndex, int chapterAll, int durPageIndex, int pageAll, int fromPageIndex) {
|
|
|
|
|
if (null != getDurContentView() && bookContentView == getDurContentView() && chapterAll > 0 && pageAll > 0) {
|
|
|
|
|
updateOtherPage(durChapterIndex, chapterAll, durPageIndex, pageAll);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface LoadDataListener {
|
|
|
|
|
public void loaddata(BookContentView bookContentView, long tag, int chapterIndex, int pageIndex);
|
|
|
|
|
|
|
|
|
|
public void updateProgress(int chapterIndex, int pageIndex);
|
|
|
|
|
|
|
|
|
|
public String getChapterTitle(int chapterIndex);
|
|
|
|
|
|
|
|
|
|
public void initData(int lineCount);
|
|
|
|
|
|
|
|
|
|
public void showMenu();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LoadDataListener loadDataListener;
|
|
|
|
|
|
|
|
|
|
public LoadDataListener getLoadDataListener() {
|
|
|
|
|
return loadDataListener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setLoadDataListener(LoadDataListener loadDataListener) {
|
|
|
|
|
this.loadDataListener = loadDataListener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BookContentView getDurContentView() {
|
|
|
|
|
return durPageView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void noPre() {
|
|
|
|
|
Toast.makeText(getContext(), "没有上一页", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void noNext() {
|
|
|
|
|
Toast.makeText(getContext(), "没有下一页", Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ViewTreeObserver.OnGlobalLayoutListener layoutInitListener = new ViewTreeObserver.OnGlobalLayoutListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onGlobalLayout() {
|
|
|
|
|
if (bookReadInitListener != null) {
|
|
|
|
|
bookReadInitListener.success();
|
|
|
|
|
}
|
|
|
|
|
durPageView.getTvContent().getViewTreeObserver().removeOnGlobalLayoutListener(layoutInitListener);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
private ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onGlobalLayout() {
|
|
|
|
|
int height = durPageView.getTvContent().getHeight();
|
|
|
|
|
if (height > 0) {
|
|
|
|
|
if (loadDataListener != null && durHeight != height) {
|
|
|
|
|
durHeight = height;
|
|
|
|
|
loadDataListener.initData(durPageView.getLineCount(height));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private int durHeight = 0;
|
|
|
|
|
|
|
|
|
|
public Paint getTextPaint() {
|
|
|
|
|
return durPageView.getTvContent().getPaint();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getContentWidth() {
|
|
|
|
|
return durPageView.getTvContent().getWidth();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void changeBg() {
|
|
|
|
|
for (BookContentView item : viewContents) {
|
|
|
|
|
item.setBg(readBookControl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void changeTextSize() {
|
|
|
|
|
for (BookContentView item : viewContents) {
|
|
|
|
|
item.setTextKind(readBookControl);
|
|
|
|
|
}
|
|
|
|
|
loadDataListener.initData(durPageView.getLineCount(durHeight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
|
if (readBookControl.getCanKeyTurn() && keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
|
|
|
|
|
if (state == PREANDNEXT || state == ONLYNEXT) {
|
|
|
|
|
int tempIndex = (state == PREANDNEXT ? 1 : 0);
|
|
|
|
|
initMoveSuccessAnim(viewContents.get(tempIndex), -getWidth());
|
|
|
|
|
} else {
|
|
|
|
|
noNext();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} else if (readBookControl.getCanKeyTurn() && keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
|
|
|
|
|
if (state == PREANDNEXT || state == ONLYPRE) {
|
|
|
|
|
initMoveSuccessAnim(viewContents.get(0), 0);
|
|
|
|
|
} else {
|
|
|
|
|
noPre();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
|
|
|
|
if (readBookControl.getCanKeyTurn() && keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (readBookControl.getCanKeyTurn() && keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OnBookReadInitListener getBookReadInitListener() {
|
|
|
|
|
return bookReadInitListener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setBookReadInitListener(OnBookReadInitListener bookReadInitListener) {
|
|
|
|
|
this.bookReadInitListener = bookReadInitListener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void loadError() {
|
|
|
|
|
if (durPageView != null) {
|
|
|
|
|
durPageView.loadError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.removeView(viewContents.get(2));
|