1.最近公司项目有个需求需要定制系统桌面并且实现无限循环滑动,通过主窗口的layout我们可以得到系统桌面的分页是通过Workspace来实现的;
2.通过代码可以知道Workspace 继承自PagedView,而pagedview的效果和viewpager其实类似,那么久简单了,那么我们通过复制首尾两屏的页面数据然后分别插入首尾两个部分就行,这里应用了这位小朋友的图片来做说明。
3.那么接下来就是首尾两页数据的数据备份了,方式1就是在bind数据的时候记录首尾两页数据的子项了,然后再add到相对于新增的首尾两个celllayout里面去,但是这样的话太麻烦了尤其是还要去更新各种拖动移除后的数据;
4.通过源码中的bindItems 方法可以得知,每个子item项目的tag其实就是bindItems,所以就不用去记录首尾两页数据的数据了,直接通过view获取item,然后在逐个去添加就好了;
5.废话不多说了,直接上代码
package com.android.launcher3;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.launcher3.dragndrop.DragOptions;
import com.android.launcher3.folder.Folder;
import com.android.launcher3.folder.FolderIcon;
import com.android.launcher3.touch.ItemLongClickListener;
/**
* 定义一个可以循环滚动的Workspace
*/
public class CycleWorkspaceextends Workspace {
private static final StringTAG ="CycleWorkspace";
//第一页的ID
public static final int FIRST_SCREEN_ID = -1001;
//最后一页的ID
public static final int LAST_SCREEN_ID =1001;
CellLayoutmEndCellLayout,mStartCellLayout;
public CycleWorkspace(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CycleWorkspace(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs, defStyle);
}
/**
* copy 一份最后的页面存放在首位
*/
public void addFirstCellLayout(){
//Log.e(TAG,"ScreenOrderID ="+mScreenOrder.get(mScreenOrder.size()-1));
CellLayout cellLayout= getScreenWithId(mScreenOrder.get(mScreenOrder.size()-1));
if (cellLayout.getChildCount()>0) {
ViewGroup viewGroup = (ViewGroup) cellLayout.getChildAt(0);
//Log.e(TAG, "addFirstCellLayout viewGroups=" + viewGroup.getChildCount());
if (mStartCellLayout ==null) {
mStartCellLayout = copyInsertNewWorkspaceScreen(0);
mStartCellLayout.setId(FIRST_SCREEN_ID);
}else {
if (getChildAt(0).getId() !=FIRST_SCREEN_ID) {
addView(mStartCellLayout,0);
}
}
mStartCellLayout.removeAllViews();
if (viewGroup.getChildCount() >0) {
//设置endCellLayout的view
for (int i =0; i < viewGroup.getChildCount(); i++) {
ItemInfo item = (ItemInfo) viewGroup.getChildAt(i).getTag();
View view =null;
if (iteminstanceof WorkspaceItemInfo) {
WorkspaceItemInfo info = (WorkspaceItemInfo) item;
view =mLauncher.createShortcut(viewGroup, info);
}else if (iteminstanceof FolderInfo) {
view = FolderIcon.fromXml(R.layout.folder_icon,mLauncher,viewGroup, (FolderInfo) item);
}else if (iteminstanceof LauncherAppWidgetInfo) {
view =mLauncher.inflateAppWidget((LauncherAppWidgetInfo) item);
}
if (view ==null) {
continue;
}
addCopyInScreen(view, item.cellX, item.cellY, item.spanX, item.spanY,mStartCellLayout);
}
}
}
}
/**
* copy 首页一份最后的页面存放在最后面
*/
public void addEndCellLayout(){
CellLayout cellLayout= getScreenWithId(mScreenOrder.get(0));
//Log.e(TAG, "addEndCellLayout cellLayout=" + cellLayout);
if (cellLayout.getChildCount()>0) {
if (mEndCellLayout ==null) {
mEndCellLayout = copyNewFirstWorkspaceScreen(getChildCount());
mEndCellLayout.setId(LAST_SCREEN_ID);
}else {
if (getChildAt(getChildCount()-1).getId() !=LAST_SCREEN_ID) {
addView(mEndCellLayout, getChildCount());
}
}
ViewGroup viewGroup = (ViewGroup) cellLayout.getChildAt(0);
//Log.e(TAG, "addEndCellLayout viewGroups=" + viewGroup.getChildCount());
mEndCellLayout.removeAllViews();
if (viewGroup.getChildCount() >0) {
mEndCellLayout.removeAllViews();
for (int i =0; i < viewGroup.getChildCount(); i++) {
if (i==0){
if (mQsb !=null) {
CellLayout.LayoutParams lp =new CellLayout.LayoutParams(0,0,mEndCellLayout.getCountX(),1);
lp.canReorder =false;
mEndCellLayout.addViewToCellLayout(mQsb,0, R.id.copy_search_container_workspace, lp,true);
}
}else {
ItemInfo item = (ItemInfo) viewGroup.getChildAt(i).getTag();
View view =null;
if (iteminstanceof WorkspaceItemInfo) {
WorkspaceItemInfo info = (WorkspaceItemInfo) item;
view =mLauncher.createShortcut(viewGroup, info);
}else if (iteminstanceof FolderInfo) {
view = FolderIcon.fromXml(R.layout.folder_icon,mLauncher, viewGroup, (FolderInfo) item);
}else if (iteminstanceof LauncherAppWidgetInfo) {
view =mLauncher.inflateAppWidget((LauncherAppWidgetInfo) item);
}
if (view ==null) {
continue;
}
addCopyInScreen(view, item.cellX, item.cellY, item.spanX, item.spanY,mEndCellLayout);
}
}
}
}
}
private void addCopyInScreen(View child,int x,int y,
int spanX,int spanY,CellLayout layout) {
//Log.e(TAG,"instanceof child ="+(child instanceof FolderIcon));
if (childinstanceof FolderIcon) {
((FolderIcon) child).setTextVisible(true);
}
LayoutParams genericLp = child.getLayoutParams();
CellLayout.LayoutParams lp;
if (!(genericLpinstanceof CellLayout.LayoutParams)) {
lp =new CellLayout.LayoutParams(x, y, spanX, spanY);
}else {
lp = (CellLayout.LayoutParams) genericLp;
lp.cellX = x;
lp.cellY = y;
lp.cellHSpan = spanX;
lp.cellVSpan = spanY;
}
if (spanX <0 && spanY <0) {
lp.isLockedToGrid =false;
}
// Get the canonical child id to uniquely represent this view in this screen
ItemInfo info = (ItemInfo) child.getTag();
int childId = info.id;
//Log.e(TAG,"childId ="+childId);
boolean markCellsAsOccupied = !(childinstanceof Folder);
//Log.e(TAG,"markCellsAsOccupied ="+markCellsAsOccupied);
if (!layout.addViewToCellLayout(child, -1, childId, lp, markCellsAsOccupied)) {
// TODO: This branch occurs when the workspace is adding views
// outside of the defined grid
// maybe we should be deleting these items from the LauncherModel?
Log.e(TAG,"Failed to add to item at (" + lp.cellX +"," + lp.cellY +") to CellLayout");
}
child.setHapticFeedbackEnabled(false);
child.setOnLongClickListener(ItemLongClickListener.INSTANCE_WORKSPACE);
if (childinstanceof DropTarget) {
Log.e(TAG,"addDropTarget ="+child);
mDragController.addDropTarget((DropTarget) child);
}
}
ViewmQsb=null;
public CellLayout copyNewFirstWorkspaceScreen(int insertIndex) {
CellLayout endPage = copyInsertNewWorkspaceScreen(insertIndex);
// Always add a QSB on the first screen.
if (mQsb ==null) {
// In transposed layout, we add the QSB in the Grid. As workspace does not touch the
// edges, we do not need a full width QSB.
mQsb = LayoutInflater.from(getContext())
.inflate(R.layout.copy_search_container_workspace, endPage,false);
}
CellLayout.LayoutParams lp =new CellLayout.LayoutParams(0,0, endPage.getCountX(),1);
lp.canReorder =false;
if (!endPage.addViewToCellLayout(mQsb,0, R.id.copy_search_container_workspace, lp,true)) {
Log.e(TAG,"Failed to add to item at (0, 0) to CellLayout");
}
return endPage;
}
public CellLayout copyInsertNewWorkspaceScreen(int insertIndex) {
// Inflate the cell layout, but do not add it automatically so that we can get the newly
// created CellLayout.
CellLayout newScreen = (CellLayout) LayoutInflater.from(getContext()).inflate(
R.layout.workspace_screen,this,false );
newScreen.getShortcutsAndWidgets().setId(R.id.workspace_page_container);
int paddingLeftRight =mLauncher.getDeviceProfile().cellLayoutPaddingLeftRightPx;
int paddingBottom =mLauncher.getDeviceProfile().cellLayoutBottomPaddingPx;
newScreen.setPadding(paddingLeftRight,0, paddingLeftRight, paddingBottom);
addView(newScreen, insertIndex);
mStateTransitionAnimation.applyChildState(
mLauncher.getStateManager().getState(), newScreen, insertIndex);
if (mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) {
newScreen.enableAccessibleDrag(true, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
}
return newScreen;
}
@Override
protected void onPageEndTransition() {
super.onPageEndTransition();
Log.e(TAG,"onPageEndTransition");
if (CYCLE_MODE && getPageCount()>1){//处理滑动完成后页面切换,实现循环效果
if (getCurrentPage() == getPageCount() -1) {
addFirstCellLayout();
CellLayout curCellLayout = (CellLayout) getPageAt(getCurrentPage());
if (curCellLayout !=null && curCellLayout.getId() ==LAST_SCREEN_ID) {
//切换到第一页
setCurrentPage(1);
}
}else if (getCurrentPage() ==0) {
addEndCellLayout();
CellLayout curCellLayout = (CellLayout) getPageAt(getCurrentPage());
if (curCellLayout !=null && curCellLayout.getId() ==FIRST_SCREEN_ID) {
//切换到最后一页
setCurrentPage(getChildCount() -2);
}
}
}
}
@Override
protected void onPageBeginTransition() {
super.onPageBeginTransition();
}
//记录拖动前的分页数量
private int mPageCount=0;
@Override
public void onDragStart(DragObject dragObject, DragOptions options) {
if (CYCLE_MODE && getPageCount()>2){//移除首位两个View
removeViewAt(getChildCount()-1);
removeViewAt(0);}
super.onDragStart(dragObject, options);
mPageCount=getPageCount();
Log.e(TAG,"onDragStart mPageCount="+mPageCount);
}
@Override
public void onDragEnd() {
super.onDragEnd();
int pageCount=getPageCount();
Log.e(TAG,"onDragEnd currentPage="+getCurrentPage() +",pageCount="+pageCount );
if (CYCLE_MODE && getPageCount()>1){
int cur =getCurrentPage();
addFirstCellLayout();
addEndCellLayout();
//如果添加了分页或者不变则切换当前的分页位置,如果减少了位置则保持不变,避免出现空屏(子view 还未绘制完)的情况
if (pageCount<=mPageCount){
++cur;
setCurrentPage(cur);
}
}
}
}