【问题标题】:By the power of MergeAdapter, StickyListHeaders and ListViewAnimations combined I'm captain Android借助 MergeAdapter、StickyListHeaders 和 ListViewAnimations 的组合,我是 Android 队长
【发布时间】:2013-12-24 11:05:02
【问题描述】:
有没有人加入 MergeAdapter、StickyListHeaders 和 ListViewAnimations android 库?
我的需求是:
- 一个垂直滚动视图中的多个 ListViews
- 异构项目视图
- 多个列表项以标题分隔,应该是粘性的
- 能够扩展一些列表项
- 拖放其中一些
- 支持安卓14+
我的临时演员:
樱桃采摘:
- 有时我的最上面的标题(这是单独的视图,不属于我的列表,我希望它保持这种状态)需要稍微滑到顶部;我的组合列表应该跟随,但同时动画扩展它的高度,以便始终连接到底部。
提到的库是:
如果可能的话,请给我一些希望,以及一些如何避免陷阱的有用提示。也许我应该使用其他一些库。或者我只需要自己写:(
====已编辑====
最后,我设法构建了我希望做的事情的存根(在 2014 年初)。它是功能可扩展和可拖动的列表视图和适配器库,具有漂亮的动画(还没有粘性标题)。这是回购:
由于 RecyclerView 现在可用,因此无需使用过于复杂的列表视图代码。这是快速切换指南 - http://andraskindler.com/2014/11/22/migrating-to-recyclerview/。
【问题讨论】:
标签:
android
listview
drag-and-drop
expandable
commonsware-cwac
【解决方案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 的解决方案,超级合并适配器应该是一种可能性:P
【解决方案2】:
我最终创建了一个适配器来处理标头。
使用适配器类:
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);
}
}