6

有没有人加入 MergeAdapter、StickyListHeaders 和 ListViewAnimations android 库?

我的需求是:

  • 一个垂直滚动视图中的多个 ListView
  • 异构项目视图
  • 由标题分隔的多个列表项,应该是粘性的
  • 扩展一些列表项的能力
  • 拖放其中一些
  • 支持安卓14+

我的额外内容:

  • 依赖 CursorAdapters

樱桃采摘:

  • 有时我最上面的标题(这是单独的视图,不是我列表的一部分,我希望它保持这种状态)需要滑到顶部一点;我的组合列表应该遵循但同时动画扩展它的高度,以便始终连接到底部。

提到的库是:

如果可能的话,请给我一些希望,以及一些如何避免陷阱的有用提示。也许我应该使用其他一些库。或者我只需要自己写:(

====已编辑====

最后,我设法构建了我希望做的事情的存根(在 2014 年初)。它是功能可扩展和可拖动的列表视图和适配器库,具有漂亮的动画(还没有粘性标题)。这是回购:

由于 RecyclerView 现在可用,因此无需使用过于复杂的列表视图代码。这是快速切换指南 - http://andraskindler.com/2014/11/22/migrating-to-recyclerview/

4

2 回答 2

1

来自 ListViewAnimations wiki: http: //nhaarman.github.io/ListViewAnimations/#getting-started

ListViewAnimations 还支持 StickyListHeaderListViews 上的外观动画。您必须将 AnimationAdapter 包装在 StickyListHeadersAdapterDecorator 中:

StickyListHeadersListView listView = (...); AlphaInAnimationAdapter
animationAdapter = new AlphaInAnimationAdapter(adapter);
StickyListHeadersAdapterDecorator stickyListHeadersAdapterDecorator =
new StickyListHeadersAdapterDecorator(animationAdapter);
stickyListHeadersAdapterDecorator.setStickyListHeadersListView(listView);
listView.setAdapter(stickyListHeadersAdapterDecorator); 

就像使用普通的 ListView 一样,您可以使用 AnimationAdapter 类的任何实现。

结合StickyListHeaders 和 MergeAdapter的解决方案,超级合并适配器应该是一种可能性:P

于 2014-08-26T20:27:23.143 回答
1

我最终创建了一个适配器来处理标题。

使用适配器类:

StickyHeaderMergeAdapter stickyHeaderMergeAdapter = new StickyHeaderMergeAdapter(this.getActivity(), R.layout.list_item_header, R.id.text1);
stickyHeaderMergeAdapter.addAdapter(
                myTypcalAdapter,
                R.string.stringName
);
this.stickyHeaderListView.setAdapter(stickyHeaderMergeAdapter);

适配器类:

package mypackagename.widget;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.commonsware.cwac.merge.MergeAdapter;
import org.apache.commons.lang3.NotImplementedException;
import java.util.LinkedHashMap;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

public class StickyHeaderMergeAdapter extends MergeAdapter implements StickyListHeadersAdapter {
    private final LinkedHashMap<ListAdapter,Integer> adapterViewHashMap = new LinkedHashMap<>();
    private final Context context;
    private final int headerLayoutId;
    private final int headerLayoutTextId;

    public StickyHeaderMergeAdapter(Context context, @LayoutRes int headerLayoutId, @IdRes int headerLayoutTextId) {
        this.context = context;
        this.headerLayoutId = headerLayoutId;
        this.headerLayoutTextId = headerLayoutTextId;
    }

    public void addAdapter(ListAdapter listAdapter, @StringRes int stringId) {
        super.addAdapter(listAdapter);
        this.adapterViewHashMap.put(listAdapter, stringId);
    }

    @Override
    public void addAdapter(ListAdapter adapter) {
        throw new NotImplementedException("You should use addAdapter(ListAdapter, View)");
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = View.inflate(this.context, this.headerLayoutId, null);

        ((TextView) convertView.findViewById(this.headerLayoutTextId)).setText(
                this.adapterViewHashMap.get(this.getAdapter(position))
        );

        return convertView;
    }

    @Override
    public long getHeaderId(int i) {
        ListAdapter listAdapter = this.getAdapter(i);
        if (!this.adapterViewHashMap.containsKey(listAdapter))
            return 0;

        return this.adapterViewHashMap.get(listAdapter);
    }
}
于 2014-09-29T19:57:24.540 回答