安卓fastScroll只覆盖了列表的一部分列表、fastScroll

2023-09-04 08:21:16 作者:不再醒来

我有一个类,它实现扩展列表活动。 在XML code(或者我可以在Java做),我设置 fastScrollEnabled 。这并在行动上实现快速滚动。但快速滚动只能在列表的顶部。就像我可以使用fastscroll拇指条滚动的整个列表,但只能在滚动条的顶部。这不是成比例的整个列表。我可以拇指栏拖动到列表的底部,但它确实因为列表视图已滚动至底部没有滚动由于只工作在奇数行为列表的顶部。 令人困惑的,我知道,我可以尝试澄清更多,如果需要的......

I have a class which implements expandable list activity. In the XML code (or I can do it in java), I set fastScrollEnabled to true. This does in deed enable fast scroll. BUT fast scroll only works in the top portion of the list. Like I can use the fastscroll thumb bar to scroll the whole list but only works in the top section of the scroll bar. It's not proportionate to the entire list. I can drag the thumb bar to the bottom of the list but it does no scrolling since the listview is already scrolled to the bottom due to the odd behaviour of it only working in the top portion of the list. Confusing I know, I can try to clarify more if needed....

我实现一个自定义 BaseExpandableListAdapter。

推荐答案

我刚刚找到了一个解决方法,以prevent系统显示此错误的行为。

I've just found a workaround to prevent the system to display this wrong behaviour.

有两种情况使用不同的code为 SectionIndexer 工作

There are two scenarios which use different code for the SectionIndexer to work.

第一种情形是,你使用FastScrollbar拇指导航到下一部分的情况。假设群体是你的路段被覆盖的方法实施 SectionIndexer 看起来像:

The first scenario is the case that you use the FastScrollbar-Thumb to navigate to the next section. Assuming that the groups are your sections the overriden methods for implementing the SectionIndexer would look like that:

@Override
public int getPositionForSection(int section) {
    return section;
}

// Gets called when scrolling the list manually
@Override
public int getSectionForPosition(int position) {
    return ExpandableListView.getPackedPositionGroup(
               expandableListView
                   .getExpandableListPosition(position));
}

第二种情况是,在手动滚动列表和快速滚动条根据部分移动,而不是将所有项目的情况。因此,code样子的:

The second scenario is the case that you scroll the list manually and the fast scrollbars move according to the sections, not to all items. The code therefore looks like that:

@Override
public int getPositionForSection(int section) {
    return expandableListView.getFlatListPosition(
               ExpandableListView.getPackedPositionForGroup(section));
}

// Gets called when scrolling the list manually
@Override
public int getSectionForPosition(int position) {
    return ExpandableListView.getPackedPositionGroup(
               expandableListView
                   .getExpandableListPosition(position));
}

人们可以看到这两种行为不能一起玩,无需进一步的采用。

As one can see these two behaviours can not play together without further adoption.

解决方法,使得它的工作是捕捉的情况下,当有人每手滚动(即通过触摸滚动)。这可以实现与适配器类 OnScrollListener 界面来完成,并将其设置到 ExpandableListView

The workaround to make it both work is to catch the case when someone is scrolling per hand (i.e. scrolling via touch). This can be done with implementing the OnScrollListener interface with the adapter class and set it onto the ExpandableListView:

public class MyExpandableListAdapter extends BaseExpandableListAdapter 
                 implements SectionIndexer, AbsListView.OnScrollListener {

    // Your fields here
    // ...
    private final ExpandableListView expandableListView;
    private boolean manualScroll;

    public MyExpandableListAdapter(ExpandableListView expandableListView
                                   /* Your other arguments */) {
        this.expandableListView = expandableListView;
        this.expandableListView.setOnScrollListener(this);
        // Other initializations
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        this.manualScroll = scrollState == SCROLL_STATE_TOUCH_SCROLL;
    }

    @Override
    public void onScroll(AbsListView view, 
                         int firstVisibleItem, 
                         int visibleItemCount, 
                         int totalItemCount) {}

    @Override
    public int getPositionForSection(int section) {
        if (manualScroll) {
            return section;
        } else {            
            return expandableListView.getFlatListPosition(
                       ExpandableListView.getPackedPositionForGroup(section));
        }
    }

    // Gets called when scrolling the list manually
    @Override
    public int getSectionForPosition(int position) {
        return ExpandableListView.getPackedPositionGroup(
                   expandableListView
                       .getExpandableListPosition(position));
    }

    // Your other methods
    // ...
}

这是固定的错误给我。

 
精彩推荐
图片推荐