我制作了一个可以过度滚动的列表视图,一切都有效,除非你用方向键盘/轨迹球滚动到最后一个项目之外。当用手指在屏幕上滚动时,所有检查顶部和底部过度滚动的工作,即使用d-pad /轨迹球滚动超出列表中的第一项...但不是底部。这就是为什么它让我吓坏了,因为其他检查都工作了......我不会发布整个活动因为它非常大,我只会发布我认为是必要的。所有检查都在OverscrollListview类中完成。
活动:
package com.somepackage;
public class SomeActivity extends ListActivity
{
private OverscrollListview mNotesList;
protected void onCreate(Bundle savedInstanceState)
{
mNotesList = (OverscrollListview) findViewById(android.R.id.list);
/* just a view in xml that can be sized so that the header and footer are always
the height of the complete screen no matter what device it runs on ...
*/
header = LayoutInflater.from(this).inflate(R.layout.listview_overscrollview, null);
header.findViewById(R.id.overscroll)
.setMinimumHeight(getWindowManager()
.getDefaultDisplay()
.getHeight());
mNotesList.addHeaderView(header, null, false);
mNotesList.addFooterView(header, null, false);
mNotesList.setOnScrollListener(mNotesList);
mNotesList.setAdapter(mainadapter);
populateNotesList();
}
...
}
public void populateNotesList()
{
...
// whenever the listview gets populated, these values need to be 0 again
mNotesList.item = mNotesList.itemOffset = 0;
}
overscrollview类:
package com.somepackage;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Toast;
public class OverscrollListview extends ListView implements OnScrollListener
{
public int item = 0, itemOffset = 0, first = 0, count = 0, total = 0;
private int currentScrollState = OnScrollListener.SCROLL_STATE_IDLE;
private Handler mHandler = new Handler();
private Toast toast;
private View listitem;
public OverscrollListview(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
}
public OverscrollListview(Context context, AttributeSet attrs)
{
super(context, attrs);
toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
}
public OverscrollListview(Context context)
{
super(context);
toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
first = firstVisibleItem;
count = visibleItemCount;
total = totalItemCount;
mHandler.postDelayed(checkListviewTopAndBottom, 100);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
currentScrollState = scrollState;
mHandler.postDelayed(checkListviewTopAndBottom, 100);
}
private final Runnable checkListviewTopAndBottom = new Runnable()
{
@Override
public void run() {
toast.setText("first="+first+"\ncount="+count+"\nlast="+getLastVisiblePosition()+"\ntotal="+total+"\nitem="+item);
toast.show();
if ( getCount() <= 2 ) return; // do nothing, listview has no items, only header & footer
if ( currentScrollState != OnScrollListener.SCROLL_STATE_IDLE ) return; // do nothing, still scrolling
if ( getFirstVisiblePosition() < 1 ) {
setSelectionFromTop(1, getDividerHeight());
return;
}
if ( getLastVisiblePosition() == getCount()-getHeaderViewsCount() ) {
if ( item == 0 ) {
if ( getFirstVisiblePosition() < 1 ) {
item = 1;
itemOffset = getDividerHeight();
} else {
item = getCount()-getHeaderViewsCount();
listitem = getChildAt(item);
if ( listitem != null ) {
itemOffset = getHeight()-listitem.getHeight()+getDividerHeight();
} else {
itemOffset = getHeight()+getDividerHeight();
}
}
}
//toast.setText("LastVisPos()==getCount()-1\nitem="+item+"\nitemOffset="+itemOffset);
//toast.show();
if ( item == getCount()-getHeaderViewsCount() || (item == 1 && getFirstVisiblePosition() < 1) ) {
setSelectionFromTop(item, itemOffset);
}
}
}
};
}
虽然它的大部分都可以使用,我可以忍受,如果有人能看到出现问题,我会很感激,因为它只是错误的'我...
感谢。
答案 0 :(得分:1)
这不是你主要问题的答案,只是想留下一些笔记。
页眉/页脚的高度可以这样设置:
public class OverscrollListview extends ListView implements OnScrollListener
{
// ...
@Override
protected void layoutChildren() {
View v = findViewById(R.id.HEADER_VIEW_ID);
ViewGroup.LayoutParams lp = v.getLayoutParams();
lp.height = getHeight();
v.setLayoutParams(lp);
v = findViewById(R.id.FOOTER_VIEW_ID);
v.setLayoutParams(lp);
super.layoutChildren();
}
}
似乎这是更方便的方式。通过这种方式,页眉/页脚的高度可以设置为其LsitView的高度。