开发者

Add footer to Android TouchListView

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜