我的xaml中有一个 ListView ,绑定到包含项目的 ReadOnlyObservableCollection 。
如果加载了所有绑定列表,我如何滚动到 ListView 的底部?
我尝试使用 myList.ScrollToBottom()函数,因为我将数据添加到mylist(在ViewModel上) - 滚动不可见,它不滚动直到结束列表。
我尝试订阅列表已加载, CollectionChanged 和 ItemContainerGenerator.ItemsChanged 事件,然后滚动到底部didn' t帮助了。
如果我订阅我的事件(在ViewModel上),我将数据设置为我的列表,然后我稍微延迟调用ScrollToBottom函数 - 滚动是完美的,所以我猜它是时间问题...
任何帮助将不胜感激。
答案 0 :(得分:1)
尝试使用它:
package com.makotokw.android.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Build;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
public class CircularListView extends ListView implements AbsListView.OnScrollListener {
private static final String TAG = CircularListView.class.getSimpleName();
private static final int REPEAT_COUNT = 3;
private int mItemHeight = 0;
private CircularListViewListener mCircularListViewListener;
private InfiniteListAdapter mInfiniteListAdapter;
private boolean mEnableInfiniteScrolling = true;
private CircularListViewContentAlignment mCircularListViewContentAlignment = CircularListViewContentAlignment.Left;
private double mRadius = -1;
private int mSmoothScrollDuration = 80;
public CircularListView(Context context) {
this(context, null);
}
public CircularListView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.listViewStyle);
}
public CircularListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOnScrollListener(this);
setClipChildren(false);
setEnableInfiniteScrolling(true);
}
public void setAdapter(ListAdapter adapter) {
mInfiniteListAdapter = new InfiniteListAdapter(adapter);
mInfiniteListAdapter.setEnableInfiniteScrolling(mEnableInfiniteScrolling);
super.setAdapter(mInfiniteListAdapter);
}
public CircularListViewListener getCircularListViewListener() {
return mCircularListViewListener;
}
public void setCircularListViewListener(CircularListViewListener circularListViewListener) {
this.mCircularListViewListener = circularListViewListener;
}
public void setEnableInfiniteScrolling(boolean enableInfiniteScrolling) {
mEnableInfiniteScrolling = enableInfiniteScrolling;
if (mInfiniteListAdapter != null) {
mInfiniteListAdapter.setEnableInfiniteScrolling(enableInfiniteScrolling);
}
if (mEnableInfiniteScrolling) {
setHorizontalScrollBarEnabled(false);
setVerticalScrollBarEnabled(false);
}
}
public CircularListViewContentAlignment getCircularListViewContentAlignment() {
return mCircularListViewContentAlignment;
}
public void setCircularListViewContentAlignment(
CircularListViewContentAlignment circularListViewContentAlignment) {
if (mCircularListViewContentAlignment != circularListViewContentAlignment) {
mCircularListViewContentAlignment = circularListViewContentAlignment;
requestLayout();
}
}
public double getRadius() {
return mRadius;
}
public void setRadius(double radius) {
if (this.mRadius != radius) {
this.mRadius = radius;
requestLayout();
}
}
public int getCentralPosition() {
double vCenterPos = getHeight() / 2.0f;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child != null) {
if (child.getTop() <= vCenterPos
&& child.getTop() + child.getHeight() >= vCenterPos) {
return getFirstVisiblePosition() + i;
}
}
}
return -1;
}
public View getCentralChild() {
int pos = getCentralPosition();
if (pos != -1) {
return getChildAt(pos - getFirstVisiblePosition());
}
return null;
}
public void scrollFirstItemToCenter() {
if (!mEnableInfiniteScrolling) {
return;
}
int realTotalItemCount = mInfiniteListAdapter.getRealCount();
if (realTotalItemCount > 0) {
setSelectionFromTop(realTotalItemCount, getBaseCentralChildTop());
}
}
public int getBaseCentralChildTop() {
int itemHeight = getItemHeight();
if (itemHeight > 0) {
return getHeight() / 2 - itemHeight / 2;
}
return 0;
}
public int getItemHeight() {
if (mItemHeight == 0) {
View child = getChildAt(0);
if (child != null) {
mItemHeight = child.getHeight();
}
}
return mItemHeight;
}
public void setSelectionAndMoveToCenter(int position) {
if (!mEnableInfiniteScrolling) {
return;
}
int realTotalItemCount = mInfiniteListAdapter.getRealCount();
if (realTotalItemCount == 0) {
return;
}
position = position % realTotalItemCount;
int centralPosition = getCentralPosition() % realTotalItemCount;
int y = getBaseCentralChildTop();
if (centralPosition == position) {
View centralView = getCentralChild();
y = centralView.getTop();
}
setSelectionFromTop(position + realTotalItemCount, y);
}
@TargetApi(Build.VERSION_CODES.FROYO)
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (mEnableInfiniteScrolling) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_UP:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
smoothScrollBy(mItemHeight, mSmoothScrollDuration);
return true;
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
smoothScrollBy(-mItemHeight, mSmoothScrollDuration);
return true;
}
break;
default:
break;
}
}
}
return super.dispatchKeyEvent(event);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (!isInTouchMode()) {
setSelectionAndMoveToCenter(getCentralPosition());
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
if (!mEnableInfiniteScrolling) {
return;
}
View itemView = this.getChildAt(0);
if (itemView == null) {
return;
}
int realTotalItemCount = mInfiniteListAdapter.getRealCount();
if (realTotalItemCount == 0) {
return;
}
if (mItemHeight == 0) {
mItemHeight = itemView.getHeight();
}
if (firstVisibleItem == 0) {
// scroll one unit
this.setSelectionFromTop(realTotalItemCount, itemView.getTop());
}
if (totalItemCount == firstVisibleItem + visibleItemCount) {
// back one unit
this.setSelectionFromTop(firstVisibleItem - realTotalItemCount,
itemView.getTop());
}
if (mCircularListViewContentAlignment != CircularListViewContentAlignment.None) {
double viewHalfHeight = view.getHeight() / 2.0f;
double vRadius = view.getHeight();
double hRadius = view.getWidth();
double yRadius = (view.getHeight() + mItemHeight) / 2.0f;
double xRadius = (vRadius < hRadius) ? vRadius : hRadius;
if (mRadius > 0) {
xRadius = mRadius;
}
for (int i = 0; i < visibleItemCount; i++) {
itemView = this.getChildAt(i);
if (itemView != null) {
double y = Math.abs(viewHalfHeight - (itemView.getTop() + (itemView.getHeight() / 2.0f)));
y = Math.min(y, yRadius);
double angle = Math.asin(y / yRadius);
double x = xRadius * Math.cos(angle);
if (mCircularListViewContentAlignment == CircularListViewContentAlignment.Left) {
x -= xRadius;
} else {
x = xRadius / 2 - x;
}
itemView.scrollTo((int) x, 0);
}
}
} else {
for (int i = 0; i < visibleItemCount; i++) {
itemView = this.getChildAt(i);
if (itemView != null) {
itemView.scrollTo(0, 0);
}
}
}
if (mCircularListViewListener != null) {
mCircularListViewListener.onCircularLayoutFinished(this, firstVisibleItem, visibleItemCount, totalItemCount);
}
}
class InfiniteListAdapter implements ListAdapter {
private boolean mEnableInfiniteScrolling = true;
private ListAdapter mCoreAdapter;
public InfiniteListAdapter(ListAdapter coreAdapter) {
mCoreAdapter = coreAdapter;
}
private void setEnableInfiniteScrolling(boolean enableInfiniteScrolling) {
mEnableInfiniteScrolling = enableInfiniteScrolling;
}
public int getRealCount() {
return mCoreAdapter.getCount();
}
public int positionToIndex(int position) {
int count = mCoreAdapter.getCount();
return (count == 0) ? 0 : position % count;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
mCoreAdapter.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
mCoreAdapter.unregisterDataSetObserver(observer);
}
@Override
public int getCount() {
int count = mCoreAdapter.getCount();
return (mEnableInfiniteScrolling) ? count * REPEAT_COUNT : count;
}
@Override
public Object getItem(int position) {
return mCoreAdapter.getItem(this.positionToIndex(position));
}
@Override
public long getItemId(int position) {
return mCoreAdapter.getItemId(this.positionToIndex(position));
}
@Override
public boolean hasStableIds() {
return mCoreAdapter.hasStableIds();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return mCoreAdapter.getView(this.positionToIndex(position), convertView, parent);
}
@Override
public int getItemViewType(int position) {
return mCoreAdapter.getItemViewType(this.positionToIndex(position));
}
@Override
public int getViewTypeCount() {
return mCoreAdapter.getViewTypeCount();
}
@Override
public boolean isEmpty() {
return mCoreAdapter.isEmpty();
}
@Override
public boolean areAllItemsEnabled() {
return mCoreAdapter.areAllItemsEnabled();
}
@Override
public boolean isEnabled(int position) {
return mCoreAdapter.isEnabled(this.positionToIndex(position));
}
}
}
**CircularListViewContentAlignment.java**
package com.makotokw.android.widget;
public enum CircularListViewContentAlignment {
None,
Left,
Right
}
**CircularListViewListener.java**
package com.makotokw.android.widget;
public interface CircularListViewListener {
void onCircularLayoutFinished(CircularListView circularListView,
int firstVisibleItem,
int visibleItemCount,
int totalItemCount);
}
答案 1 :(得分:0)
如果您有ViewModel,您是如何做到这一点的? ViewModel不知道你在说什么控件。那么你的ViewModel如何引用&#34; myList&#34;?
我还尝试让ListView在更新集合时滚动到最后一项,但XAML中没有ScrollTo功能:这些都无法绑定到它们。这些似乎都是方法,意味着你不能用它来做MVVM属性。
或者我错过了什么?