有人加入MergeAdapter、StickyListHeaders和ListViewAnimations安卓库吗?
我的需求是:
- 一个垂直滚动视图中的多个ListView
- 异构项目视图
- 由标题分隔的多个列表项,这些标题应该是粘性的
- 扩展某些列表项的能力
- 拖掉一些
- 支持android 14+
我的附加功能:
- 依赖CursorAdapters
Cherrypick:
- 有时我最上面的标题(这是一个单独的视图,不在我的列表中,我更希望它保持这种状态)需要滑到顶部;我的组合列表应该在后面,但同时动画扩展它的高度,以便始终附加到底部
提到的图书馆有:
- MergeAdapter-https://github.com/commonsguy/cwac-merge
- 粘贴列表标题-https://github.com/emilsjolander/StickyListHeaders
- ListView动画-https://github.com/nhaarman/ListViewAnimations
如果可能的话,请给我一些希望,以及一些如何避免陷阱的有用提示。也许我应该用一些其他的libs。或者我只能自己写:(
===编辑===
最后,我设法建立了一个我希望做的事情的存根(在2014年初)。它是功能可扩展和可拖动的列表视图和适配器库,具有漂亮的动画(还没有粘性的标题)。这是回购:
- https://github.com/fada21/HydraListAndroid
由于RecyclerView现在可用,因此无需使用过于复杂的列表视图代码。这是快速切换指南-http://andraskindler.com/2014/11/22/migrating-to-recyclerview/.
从ListViewAnimations wiki:http://nhaarman.github.io/ListViewAnimations/#getting-启动
ListViewAnimations还支持上的外观动画StickyListHeaderListViews。您必须将AnimationAdapter包装在粘贴列表标题适配器装饰器:
StickyListHeadersListView listView = (...); AlphaInAnimationAdapter
animationAdapter = new AlphaInAnimationAdapter(adapter);
StickyListHeadersAdapterDecorator stickyListHeadersAdapterDecorator =
new StickyListHeadersAdapterDecorator(animationAdapter);
stickyListHeadersAdapterDecorator.setStickyListHeadersListView(listView);
listView.setAdapter(stickyListHeadersAdapterDecorator);
就像普通的ListView一样,您可以使用AnimationAdapter类。
结合StickyListHeaders和MergeAdapter的解决方案,超级MergeAdapter应该是一种可能性:p
我最终创建了一个适配器来处理标头。
使用适配器类:
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);
}
}