Add footer to Android TouchListView

╄→尐↘猪︶ㄣ 提交于 2019-12-14 02:17:24

问题


Hi I'm using the TouchListView control from here: https://github.com/commonsguy/cwac-touchlist and I've added some buttons to add to the list in the footer:

    mFooter = getLayoutInflater().inflate(R.layout.edit_homepage_footer_layout, null);
    mListView = (TouchListView) findViewById(R.id.sectionList);
    mListView.addFooterView(mFooter);

It all seems to be working fine until I drag an item in the list, at which point the footer collapses (to the height of one list item I think) obscuring the buttons I have added.

Can anyone suggest a fix/workaround for this?


回答1:


I actually worked this out shortly after asking it (always the way...)

The issue is in the doExpansion() and unExpandViews() methods which were modifying every item in the list including the footer. To fix it I created a method to check whether we are dealing with a draggable item or the footer:

private boolean isDraggableItem(View view) {
    View dragger = view.findViewById(grabberId);
    return dragger != null;
}

And then modified the methods mentioned as follows:

private void unExpandViews(boolean deletion) {
    for (int i = 0; ; i++) {
        View v = getChildAt(i);
        if (v == null) {
            if (deletion) {
                // HACK force update of mItemCount
                int position = getFirstVisiblePosition();
                int y = getChildAt(0).getTop();
                setAdapter(getAdapter());
                setSelectionFromTop(position, y);
                // end hack
            }
            layoutChildren(); // force children to be recreated where needed
            v = getChildAt(i);
            if (v == null) {
                break;
            }
        }
        if (isDraggableItem(v)) { //check this view isn't the footer
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.height = mItemHeightNormal;
            v.setLayoutParams(params);
            v.setVisibility(View.VISIBLE);
        }
    }
}

private void doExpansion() {
    Log.d(logTag, "Doing expansion");
    int childnum = mDragPos - getFirstVisiblePosition();
    if (mDragPos > mFirstDragPos) {
        childnum++;
    }

    View first = getChildAt(mFirstDragPos - getFirstVisiblePosition());

    for (int i = 0; ; i++) {
        View vv = getChildAt(i);
        if (vv == null) {
            break;
        }
        int height = mItemHeightNormal;
        int visibility = View.VISIBLE;
        if (vv.equals(first)) {
            // processing the item that is being dragged
            if (mDragPos == mFirstDragPos) {
                // hovering over the original location
                visibility = View.INVISIBLE;
            } else {
                // not hovering over it
                height = 1;
            }
        } else if (i == childnum) {
            if (mDragPos < getCount() - 1) {
                height = mItemHeightExpanded;
            }
        }
        if (isDraggableItem(vv)) { //check this view isn't the footer
            ViewGroup.LayoutParams params = vv.getLayoutParams();
            params.height = height;
            vv.setLayoutParams(params);
            vv.setVisibility(visibility);
        }
    }
}

Would be worth updating the github project to include this I think.



来源:https://stackoverflow.com/questions/6709782/add-footer-to-android-touchlistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!