本文將介紹列表頭/尾的添加功能實現以及整個視圖在沒有足夠item可以鋪滿控件時,讓顯示內容劇中顯示。
爲什麼要實現添加頭尾視圖,這個我個人也不是很清楚,畢竟在開發過程中很少會有使用頭尾視圖的需要;不過爲了學習,還爲了以後也許可能有這方面的需求,所以還是選擇了實現這個功能;對於內容劇中顯示功能,是因爲在使用這個控件時剛好有這個需求。
有一點值得注意:頭/尾視圖在設計和使用的概念上不是作爲列表中的item,如果這一點沒有弄清楚,那麼在閱讀源代碼時會較爲困難,其中八個概念性封裝的方法就尤爲體現出這一點(八個方法具體見代碼)
先上代碼:
package com.hss.os.horizontallistview.history_version;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.Scroller;
import java.util.LinkedList;
import java.util.Queue;
/**
* 添加頭/尾視圖及居中顯示
* Created by sxyx on 2017/8/8.
*/
public class HorizontalListView3 extends AdapterView<ListAdapter> {
private Queue<View> cacheView = new LinkedList<>();//列表項緩存視圖
private ListAdapter adapter = null;
private GestureDetector mGesture;
private int firstItemIndex = 0;//顯示的第一個子項的下標
private int lastItemIndex = -1;//顯示的最後的一個子項的下標
private int scrollValue=0;//列表已經發生有效滾動的位移值
private int hasToScrollValue=0;//接下來列表發生滾動所要達到的位移值
private int maxScrollValue=Integer.MAX_VALUE;//列表發生滾動所能達到的最大位移值(這個由最後顯示的列表項決定)
private int displayOffset=0;//列表顯示的偏移值(用於矯正列表顯示的所有子項的顯示位置)
private Scroller mScroller;
private int firstItemLeftEdge=0;//第一個子項的左邊界
private int lastItemRightEdge=0;//最後一個子項的右邊界
private View headView;
private View footView;
private boolean hasHeadView=false;
private boolean hasFootView=false;
private boolean canShowInMid=false;
public HorizontalListView3(Context context) {
super(context);
init(context);
}
public HorizontalListView3(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public HorizontalListView3(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public HorizontalListView3(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(Context context){
mGesture = new GestureDetector(getContext(), mOnGesture);
mScroller=new Scroller(context);
}
private void initParams(){
mScroller.forceFinished(true);//避免在滑動過程中變換視圖內容時,出現列表無法滾動的情況
removeAllViewsInLayout();
if(adapter!=null&&lastItemIndex<adapter.getCount())
hasToScrollValue=scrollValue;//保持顯示位置不變
else hasToScrollValue=0;//滾動到列表頭
scrollValue=0;//列表已經發生有效滾動的位移值
firstItemIndex = 0;//顯示的第一個子項的下標
lastItemIndex = -1;//顯示的最後的一個子項的下標
maxScrollValue=Integer.MAX_VALUE;//列表發生滾動所能達到的最大位移值(這個由最後顯示的列表項決定)
displayOffset=0;//列表顯示的偏移值(用於矯正列表顯示的所有子項的顯示位置)
firstItemLeftEdge=0;//第一個子項的左邊界
lastItemRightEdge=0;//最後一個子項的右邊界
if(hasHeadView||hasFootView) {
if (hasHeadView) {
scrollValue = headView.getMeasuredWidth();
headView.layout(0, 0, 0, 0);
setHeadView(headView);
}
if (hasFootView) {
footView.layout(0, 0, 0, 0);
setFootView(footView);
}
}else requestLayout();
}
private DataSetObserver mDataObserver = new DataSetObserver() {
@Override
public void onChanged() {
//執行Adapter數據改變時的邏輯
initParams();
}
@Override
public void onInvalidated() {
//執行Adapter數據失效時的邏輯
initParams();
}
};
@Override
public ListAdapter getAdapter() {
return adapter;
}
@Override
public void setAdapter(ListAdapter adapter) {
if(adapter!=null){
adapter.registerDataSetObserver(mDataObserver);
}
if(this.adapter!=null){
this.adapter.unregisterDataSetObserver(mDataObserver);
}
this.adapter=adapter;
requestLayout();
}
@Override
public View getSelectedView() {
return null;
}
@Override
public void setSelection(int i) {
}
private void addAndMeasureChild(View child, int viewIndex) {
LayoutParams params = child.getLayoutParams();
params = params==null ? new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT):params;
addViewInLayout(child, viewIndex, params, true);
child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.UNSPECIFIED));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.e("","============>>>>>left:"+left+" top:"+top+" right:"+right+" bottom:"+bottom);
//需要先佈局列表項再根據餘下的空間佈局列表頭尾
//佈局列表項
/*
1.計算這一次整體滾動偏移量
2.根據偏移量提取需要緩存視圖
3.根據偏移量顯示新的列表項
4.根據整體偏移值整頓所有列表項位置
5.計算最大滾動位移值,記錄已經發生有效滾動的位移值
6.根據顯示的最終效果,判斷是否要居中顯示
*/
int dx=calculateScrollValue();
removeNonVisibleItems(dx);
showListItem(dx);
adjustItems();
//佈局列表頭、尾
adjustHeadAndFootView(dx);
calculateMaxScrollValue();
adjustShow();
//繼續滾動
if(!mScroller.isFinished()){
post(new Runnable(){
@Override
public void run() {
requestLayout();
}
});
}
}
/**
* 計算這一次整體滾動偏移量
* @return
*/
private int calculateScrollValue(){
int dx=0;
if(mScroller.computeScrollOffset()){
hasToScrollValue = mScroller.getCurrX();
}
if(hasToScrollValue<=0){
hasToScrollValue=0;
mScroller.forceFinished(true);
}
if(hasToScrollValue >= maxScrollValue) {
hasToScrollValue = maxScrollValue;
mScroller.forceFinished(true);
}
dx=hasToScrollValue-scrollValue;
scrollValue=hasToScrollValue;
return -dx;
}
/**
* 計算最大滾動值
*/
private void calculateMaxScrollValue(){
if(getListItemCount()>0) {
if(lastItemIndex==adapter.getCount()-1) {//已經顯示了最後一項
if(getChildAt(getChildCount() - 1).getRight()>=getShowEndEdge()) {
maxScrollValue = scrollValue + getChildAt(getChildCount() - 1).getRight() - getShowEndEdge();
}else{
maxScrollValue=0;
}
}
}else{
if(adapter!=null&&adapter.getCount()>0){
}else {
if (getChildCount() > 0
&& getChildAt(getChildCount() - 1).getRight() >= getShowEndEdge()) {
maxScrollValue = scrollValue + getChildAt(getChildCount() - 1).getRight() - getShowEndEdge();
} else {
maxScrollValue = 0;
}
}
}
}
/**
* 根據偏移量提取需要緩存視圖
* @param dx
*/
private void removeNonVisibleItems(int dx) {
if(getListItemCount()>0) {
//移除列表頭
View child = getChildAt(getStartItemIndex());
while (getListItemCount()>0&&child != null && child.getRight() + dx <= getShowStartEdge()) {
displayOffset += child.getMeasuredWidth();
cacheView.offer(child);
removeViewInLayout(child);
firstItemIndex++;
child = getChildAt(getStartItemIndex());
}
//移除列表尾
child = getChildAt(getEndItemIndex());
while (getListItemCount()>0&&child != null && child.getLeft() + dx >= getShowEndEdge()) {
cacheView.offer(child);
removeViewInLayout(child);
lastItemIndex--;
child = getChildAt(getEndItemIndex());
}
}
}
/**
* 根據偏移量顯示新的列表項
* @param dx
*/
private void showListItem(int dx) {
if(adapter==null)return;
int firstItemEdge = getFirstItemLeftEdge()+dx;
int lastItemEdge = getLastItemRightEdge()+dx;
displayOffset+=dx;//計算偏移量
//顯示列表頭視圖
while(firstItemEdge > getShowStartEdge() && firstItemIndex-1 >= 0) {
firstItemIndex--;//往前顯示一個列表項
View child = adapter.getView(firstItemIndex, cacheView.poll(), this);
addAndMeasureChild(child, getStartItemIndex());
firstItemEdge -= child.getMeasuredWidth();
displayOffset -= child.getMeasuredWidth();
}
//顯示列表未視圖
while(lastItemEdge < getShowEndEdge() && lastItemIndex+1 < adapter.getCount()) {
lastItemIndex++;//往後顯示一個列表項
View child = adapter.getView(lastItemIndex, cacheView.poll(), this);
addAndMeasureChild(child, getEndItemIndex()+1);
lastItemEdge += child.getMeasuredWidth();
}
}
/**
* 調整各個item的位置
*/
private void adjustItems() {
if(getListItemCount() > 0){
int left = displayOffset+getShowStartEdge();
int top = getPaddingTop();
int endIndex = getEndItemIndex();
int startIndex = getStartItemIndex();
int childWidth,childHeight;
for(int i=startIndex;i<=endIndex;i++){
View child = getChildAt(i);
childWidth = child.getMeasuredWidth();
childHeight = child.getMeasuredHeight();
child.layout(left, top, left + childWidth, top + childHeight);
left += childWidth;
}
firstItemLeftEdge=getChildAt(getStartItemIndex()).getLeft();
lastItemRightEdge=getChildAt(getEndItemIndex()).getRight();
}
}
/**
* 調整列表頭、尾
*/
private void adjustHeadAndFootView(int dx){
if(hasHeadView){
int left,right;
if(getListItemCount()>0){
right=firstItemLeftEdge;
}else{
if(headView.getRight()>0)
right=headView.getRight()+dx;
else
right=getShowStartEdge()+headView.getMeasuredWidth();
}
left=right-headView.getMeasuredWidth();
headView.layout(left, getPaddingTop(), right, headView.getMeasuredHeight()+getPaddingTop());
}
if(hasFootView){
int left,right;
if(getListItemCount()>0){
left=getChildAt(getEndItemIndex()).getRight();
}else{
if(hasHeadView)
left=headView.getRight();
else {
if(footView.getLeft()==0&&dx==0){//第一次賦值
left=getShowStartEdge();
}else{
left=footView.getLeft()+dx;
}
}
}
right=left+footView.getMeasuredWidth();
footView.layout(left, getPaddingTop(), right, footView.getMeasuredHeight()+getPaddingTop());
}
}
private void adjustShow(){
if(isCanShowInMid()){//可以居中顯示
int endEdge=getShowEndEdge();
boolean canAdjust=false;
if(hasFootView){
if(footView.getRight()<endEdge) canAdjust=true;
}else if(getListItemCount()>0){
if(getChildAt(getEndItemIndex()).getRight()<endEdge) canAdjust=true;
}else if(hasHeadView){
if(headView.getRight()<endEdge) canAdjust=true;
}
if(canAdjust){
//居中顯示
int itemsWidth=getChildAt(getChildCount()-1).getRight()-getShowStartEdge();
int left=(getShowWidth()-itemsWidth)/2+getShowStartEdge();
int right;
View child;
for(int i=0;i<getChildCount();i++){
child= getChildAt(i);
right=left+child.getMeasuredWidth();
child.layout(left,child.getTop(),right,child.getBottom());
left=right;
}
}
}
}
//以下八個方法爲概念性封裝方法,有助於往後的擴展和維護
/**
* 獲得列表視圖中item View的總數
* @return
*/
private int getListItemCount(){
int itemCount=getChildCount();
if(hasHeadView)itemCount-=1;
if(hasFootView)itemCount-=1;
return itemCount;
}
/**
* 獲得列表視圖中第一個item View下標
* @return
*/
private int getStartItemIndex(){
if(hasHeadView) return 1;
return 0;
}
/**
* 獲得列表視圖中最後一個item View下標
* @return
*/
private int getEndItemIndex(){
if(hasFootView) return getChildCount()-2;
return getChildCount()-1;
}
/**
* 獲得列表視圖中第一個item View左邊界值
* @return
*/
private int getFirstItemLeftEdge(){
if(getListItemCount()>0) {
return firstItemLeftEdge;
}else{
if(hasHeadView) return headView.getRight();
else return 0;
}
}
/**
* 獲得列表視圖中最後一個item View右邊界值
* @return
*/
private int getLastItemRightEdge(){
if(getListItemCount()>0) {
return lastItemRightEdge;
}else{
if(hasFootView) return footView.getLeft();
else return 0;
}
}
/**
* 取得視圖可見區域的左邊界
* @return
*/
private int getShowStartEdge(){
return getPaddingLeft();
}
/**
* 取得視圖可見區域的右邊界
* @return
*/
private int getShowEndEdge(){
return getWidth()-getPaddingRight();
}
/**
* 取得視圖可見區域的寬度
* @return
*/
private int getShowWidth(){
return getWidth()-getPaddingLeft()-getPaddingRight();
}
public void setHeadView(View view){
if(view!=null) {
int headRight=-1;
int width=0;
if (hasHeadView&&headView!=null) {
headRight=headView.getRight();
width=headView.getWidth();
removeViewInLayout(headView);
}
hasHeadView = true;
headView=view;
addAndMeasureChild(headView, 0);
if(getListItemCount()>0) {//有列表內容
if (headRight == -1) {
//新增列表頭
if (firstItemIndex == 0) {//第一個顯示的是第一個列表項
//滾動整個列表,讓其顯示完整列表頭(讓列表往回滾)
scrollValue = headView.getMeasuredWidth()
+ getShowStartEdge() - firstItemLeftEdge;
hasToScrollValue=0;
} else {//不是顯示第一個列表項
//不滾動列表項,增加歷史滾動值
hasToScrollValue += headView.getMeasuredWidth();
scrollValue = hasToScrollValue;
}
} else {
//替換列表頭
hasToScrollValue += headView.getMeasuredWidth()-width;
}
}
maxScrollValue=Integer.MAX_VALUE;
requestLayout();
}
}
public void removeHeadView(){
if(hasHeadView&&headView!=null){
hasHeadView=false;
int left=headView.getLeft();
int width=headView.getMeasuredWidth();
removeViewInLayout(headView);
if(headView.getRight()>=getShowStartEdge()) {//列表頭有顯示
scrollValue = -(width+left-getShowStartEdge());
hasToScrollValue=0;
}else{
scrollValue-=width;
hasToScrollValue-=width;
}
requestLayout();
}else{
hasHeadView=false;
}
}
public void setFootView(View view){
if(view!=null) {
if (hasFootView&&footView!=null) {
removeViewInLayout(footView);
}
hasFootView=true;
footView=view;
addAndMeasureChild(footView, -1);
requestLayout();
}
}
public void removeFootView(){
if(hasFootView&&footView!=null){
hasFootView=false;
int left=footView.getLeft();
removeViewInLayout(footView);
if(left<getWidth()) {
hasToScrollValue -= getWidth()-left-getShowStartEdge();
}
requestLayout();
}else{
hasFootView=false;
}
}
/**
* 在onTouchEvent處理事件,讓子視圖優先消費事件
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGesture.onTouchEvent(event);
}
private GestureDetector.OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
mScroller.forceFinished(true);//點擊時停止滾動
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
mScroller.fling(hasToScrollValue, 0, (int)-velocityX, 0, 0, maxScrollValue, 0, 0);
requestLayout();
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
synchronized(HorizontalListView3.this){
hasToScrollValue += (int)distanceX;
}
requestLayout();
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
if (isEventWithinView(e, child)) {
if(hasHeadView&&i==0){
//點擊列表頭
}else if(hasFootView&&i==getChildCount()-1){
//點擊列表尾
}else {
int position=firstItemIndex + i;
if(hasHeadView) position--;
if (getOnItemClickListener() != null) {
getOnItemClickListener().onItemClick(HorizontalListView3.this, child, position, adapter.getItemId(position));
}
if (getOnItemSelectedListener() != null) {
getOnItemSelectedListener().onItemSelected(HorizontalListView3.this, child, position, adapter.getItemId(position));
}
}
break;
}
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (isEventWithinView(e, child)) {
if(hasHeadView&&i==0){
//點擊列表頭
}else if(hasFootView&&i==getChildCount()-1){
//點擊列表尾
}else {
int position=firstItemIndex + i;
if(hasHeadView) position--;
if (getOnItemLongClickListener() != null) {
getOnItemLongClickListener().onItemLongClick(HorizontalListView3.this, child, position, adapter.getItemId(position));
}
}
break;
}
}
}
private boolean isEventWithinView(MotionEvent e, View child) {
Rect viewRect = new Rect();
int[] childPosition = new int[2];
child.getLocationOnScreen(childPosition);
int left = childPosition[0];
int right = left + child.getWidth();
int top = childPosition[1];
int bottom = top + child.getHeight();
viewRect.set(left, top, right, bottom);
return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
}
};
public synchronized void scrollTo(int x) {
mScroller.startScroll(hasToScrollValue, 0, x - hasToScrollValue, 0);
requestLayout();
}
public boolean isCanShowInMid() {
return canShowInMid;
}
public void setCanShowInMid(boolean canShowInMid) {
this.canShowInMid = canShowInMid;
}
}
這裏實現的頭尾視圖只提供單視圖模式,沒有提供像ListView一樣列表模式,也就是說,頭/尾視圖只能各自設置一個,而不能設置多個。
有一點值得注意:頭/尾視圖在設計和使用的概念上不是作爲列表中的item,如果這一點沒有弄清楚,那麼在讀代碼時會更加困難,其中八個概念性封裝的方法就尤爲體現出這一點
在沒有需要實現頭/尾視圖的時候,在很多代碼段中用到0表示第一個item下標,也用到getPaddingLeft()這個方法的取值作爲可視區域的左邊界值;當需要加入頭/尾視圖時,這些值都需要做出些改變,因而需要引入八個概念性封裝的方法,以降低代碼之間的耦合度,使擴展功能後的代碼和擴展前的代碼有明顯分割線,這樣不僅有利於增強代碼的可讀性,又有利於代碼的維護和擴展。
這樣的實現方式不僅讓橫行ListView的基礎實現保持其原型,讓擴展的代碼完全分離出來,而且對於以後的擴展更加有利,對於功能的剔除也方便很多而不容易出錯。(對於這八個方法的作用,在代碼裏已經有了相應的註解,這裏就不再做討論)
在加入頭/尾視圖之前,這八個概念性封裝的方法實現如下:
//以下八個方法爲概念性封裝方法,有助於往後的擴展和維護
/**
* 獲得列表視圖中item View的總數
* @return
*/
private int getListItemCount(){
int itemCount=getChildCount();
return itemCount;
}
/**
* 獲得列表視圖中第一個item View下標
* @return
*/
private int getStartItemIndex(){
return 0;
}
/**
* 獲得列表視圖中最後一個item View下標
* @return
*/
private int getEndItemIndex(){
return getChildCount()-1;
}
/**
* 獲得列表視圖中第一個item View左邊界值
* @return
*/
private int getFirstItemLeftEdge(){
if(getListItemCount()>0) {
return firstItemLeftEdge;
}else{
return 0;
}
}
/**
* 獲得列表視圖中最後一個item View右邊界值
* @return
*/
private int getLastItemRightEdge(){
if(getListItemCount()>0) {
return lastItemRightEdge;
}else{
return 0;
}
}
/**
* 取得視圖可見區域的左邊界
* @return
*/
private int getShowStartEdge(){
return getPaddingLeft();
}
/**
* 取得視圖可見區域的右邊界
* @return
*/
private int getShowEndEdge(){
return getWidth()-getPaddingRight();
}
/**
* 取得視圖可見區域的寬度
* @return
*/
private int getShowWidth(){
return getWidth()-getPaddingLeft()-getPaddingRight();
}
添加了頭/尾視圖後,這八個概念性封裝的方法實現如下:
//以下八個方法爲概念性封裝方法,有助於往後的擴展和維護
/**
* 獲得列表視圖中item View的總數
* @return
*/
private int getListItemCount(){
int itemCount=getChildCount();
if(hasHeadView)itemCount-=1;
if(hasFootView)itemCount-=1;
return itemCount;
}
/**
* 獲得列表視圖中第一個item View下標
* @return
*/
private int getStartItemIndex(){
if(hasHeadView) return 1;
return 0;
}
/**
* 獲得列表視圖中最後一個item View下標
* @return
*/
private int getEndItemIndex(){
if(hasFootView) return getChildCount()-2;
return getChildCount()-1;
}
/**
* 獲得列表視圖中第一個item View左邊界值
* @return
*/
private int getFirstItemLeftEdge(){
if(getListItemCount()>0) {
return firstItemLeftEdge;
}else{
if(hasHeadView) return headView.getRight();
else return 0;
}
}
/**
* 獲得列表視圖中最後一個item View右邊界值
* @return
*/
private int getLastItemRightEdge(){
if(getListItemCount()>0) {
return lastItemRightEdge;
}else{
if(hasFootView) return footView.getLeft();
else return 0;
}
}
/**
* 取得視圖可見區域的左邊界
* @return
*/
private int getShowStartEdge(){
return getPaddingLeft();
}
/**
* 取得視圖可見區域的右邊界
* @return
*/
private int getShowEndEdge(){
return getWidth()-getPaddingRight();
}
/**
* 取得視圖可見區域的寬度
* @return
*/
private int getShowWidth(){
return getWidth()-getPaddingLeft()-getPaddingRight();
}
實現添加頭/尾視圖功能的步驟如下:
1.實現getHeadView()、getFootView()、removeHeadView()、removeFootView()這四個方法
在實現這四個方法時會遇到需要調整整個列表顯示的情況,這個時候整個列表的顯示調整要通過滾動操作進行調整(即調整scrollValue和hasToScrollValue的值,且調用requestLayout()方法重新佈局視圖),而不是直接操作left/top/right/bottom來調整,
2.實現頭/尾視圖佈局方法adjustHeadAndFootView(int dx);
頭視圖的顯示是以第一個顯示的item View的左邊界作爲基準,即以第一個顯示的itemView的左邊界作爲頭視圖顯示的右邊界(第一個itemView 的left值作爲headView的right值)。
同理,尾視圖的顯示是以最後一個item View的右邊界作爲基準(最後一個itemView 的right值作爲footView的left值)
頭尾視圖的顯示不考慮其是否可見,即不需要考慮頭尾視圖是否在橫向ListView的可見區域內。
3.選擇修改八個概念性封裝的方法,以滿足需求
4.在initParams()方法中添加入頭/尾視圖的初始化操作
如果在initPatams方法中沒有對頭/尾視圖做出調整,那麼整個執行邏輯會出錯。
5.在onLayout(boolean changed, int left, int top, int right, int bottom)方法中添加頭/尾視圖的佈局操作。必須在adjustItems()和calculateMaxScrollValue()方法之間調用,如下代碼:
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int dx=calculateScrollValue();
removeNonVisibleItems(dx);
showListItem(dx);
adjustItems();
//佈局列表頭、尾
adjustHeadAndFootView(dx);
calculateMaxScrollValue();
adjustShow();
//繼續滾動
if(!mScroller.isFinished()){
post(new Runnable(){
@Override
public void run() {
requestLayout();
}
});
}
}
實現居中顯示的功能步驟如下:
1.添加居中顯示開關boolean canShowInMid及相應的設置和訪問方法
public boolean isCanShowInMid() {
return canShowInMid;
}
public void setCanShowInMid(boolean canShowInMid) {
this.canShowInMid = canShowInMid;
}
2.實現adjustShow()方法
private void adjustShow(){
if(isCanShowInMid()){//可以居中顯示
int endEdge=getShowEndEdge();
boolean canAdjust=false;
if(hasFootView){
if(footView.getRight()<endEdge) canAdjust=true;
}else if(getListItemCount()>0){
if(getChildAt(getEndItemIndex()).getRight()<endEdge) canAdjust=true;
}else if(hasHeadView){
if(headView.getRight()<endEdge) canAdjust=true;
}
if(canAdjust){
//居中顯示
int itemsWidth=getChildAt(getChildCount()-1).getRight()-getShowStartEdge();
int left=(getShowWidth()-itemsWidth)/2+getShowStartEdge();
int right;
View child;
for(int i=0;i<getChildCount();i++){
child= getChildAt(i);
right=left+child.getMeasuredWidth();
child.layout(left,child.getTop(),right,child.getBottom());
left=right;
}
}
}
}
3.在onLayout(boolean changed, int left, int top, int right, int bottom)方法中所有視圖佈局完成時調用