通过MergeAdapter的强大功能,StickyListHeaders和ListViewAnimations结合起来我是队长Android

时间:2013-12-06 22:54:56

标签: android listview drag-and-drop expandable commonsware-cwac

有没有人加入MergeAdapter,StickyListHeaders和ListViewAnimations安卓库?

我的需求是:

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

我的演员:

  • 依赖CursorAdapters

Cherrypick:

  • 有时我的顶部标题(这是单独的视图不属于我的列表,我宁愿保持这种方式)需要滑动到顶部;我的组合列表应该遵循,但同时动画扩展它的高度,以便始终连接到底部。

提到的图书馆是:

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

==== EDITED ====

最后,我设法建立了我希望做的事情(2014年初)。它是功能可扩展和可拖动的listview和适配器库,具有漂亮的动画(还没有粘性标头)。回复:

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

2 个答案:

答案 0 :(得分: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 and MergeAdapter的解决方案,uber-mergeadapter应该是可能的:P

答案 1 :(得分: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);

Adapter类:

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