2

编辑:最近又开始工作了,让它工作了,所以不再需要答案了。 关联

我制作了一个可过度滚动的列表视图,一切正常,除非您使用方向键/轨迹球滚动超出最后一项。当用手指在屏幕上滚动时,所有检查顶部和底部过度滚动的工作,即使使用 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);
            }
        }
    }
    };
}

虽然它的大部分工作,我可以忍受,如果有人能看到出了什么问题,我会很感激,因为它只是让我烦恼......

谢谢。

4

1 回答 1

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();
    }
}

这似乎是更方便的方式。这样header/footer的高度就可以设置为它的LsitView的高度。

于 2013-01-11T14:53:24.097 回答