可以在新的RecyclerView中使用可扩展列表项吗?像ExpandableListView?
答案 0 :(得分:118)
这对布局管理器来说很简单,这完全取决于您管理适配器的方式。
如果要扩展某个部分,只需在标题后面向适配器添加新项目。记得在执行此操作时调用notifyItemRangeInserted。要折叠部分,只需删除相关项,然后调用notifyItemRangeRemoved()。对于适当通知的任何数据更改,回收器视图将为视图设置动画。添加项目时,将填充新项目的区域,新项目将逐渐淡入。删除项目正好相反。除了适配器之外,您需要做的就是设置视图样式以将逻辑结构传达给用户。
更新:Ryan Brooks现在写了article如何做到这一点。
答案 1 :(得分:8)
从here
获取示例代码实现在ViewHolder onClick上设置ValueAnimator
@Override
public void onClick(final View view) {
if (mOriginalHeight == 0) {
mOriginalHeight = view.getHeight();
}
ValueAnimator valueAnimator;
if (!mIsViewExpanded) {
mIsViewExpanded = true;
valueAnimator = ValueAnimator.ofInt(mOriginalHeight, mOriginalHeight + (int) (mOriginalHeight * 1.5));
} else {
mIsViewExpanded = false;
valueAnimator = ValueAnimator.ofInt(mOriginalHeight + (int) (mOriginalHeight * 1.5), mOriginalHeight);
}
valueAnimator.setDuration(300);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
view.getLayoutParams().height = value.intValue();
view.requestLayout();
}
});
valueAnimator.start();
}
这是最终代码
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mFriendName;
private int mOriginalHeight = 0;
private boolean mIsViewExpanded = false;
public ViewHolder(RelativeLayout v) {
super(v);
mFriendName = (TextView) v.findViewById(R.id.friendName);
v.setOnClickListener(this);
}
@Override
public void onClick(final View view) {
if (mOriginalHeight == 0) {
mOriginalHeight = view.getHeight();
}
ValueAnimator valueAnimator;
if (!mIsViewExpanded) {
mIsViewExpanded = true;
valueAnimator = ValueAnimator.ofInt(mOriginalHeight, mOriginalHeight + (int) (mOriginalHeight * 1.5));
} else {
mIsViewExpanded = false;
valueAnimator = ValueAnimator.ofInt(mOriginalHeight + (int) (mOriginalHeight * 1.5), mOriginalHeight);
}
valueAnimator.setDuration(300);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
view.getLayoutParams().height = value.intValue();
view.requestLayout();
}
});
valueAnimator.start();
}
}
答案 2 :(得分:3)
https://github.com/gabrielemariotti/cardslib
此库具有带有recyclerview的可扩展列表的实现(请参阅“CardViewNative”下的演示应用程序 - >“List,Grid和RecyclerView” - >“可扩展卡片”)。它还有许多其他很酷的卡/列表组合。
答案 3 :(得分:0)
有人抱怨说上面提到的解决方案不适用于listview作为可扩展内容。但是有一个简单的解决方案:创建一个列表视图并使用您的行手动填写此列表视图。
懒惰解决方案:如果您不想更改代码,那么这是一个简单的解决方案。 只需手动使用适配器创建视图并将其添加到LinearLayout
。
以下是例子:
if (mIsExpanded)
{
// llExpandable... is the expandable nested LinearLayout
llExpandable.removeAllViews();
final ArrayAdapter<?> adapter = ... // create your adapter as if you would use it for a ListView
for (int i = 0; i < adapter.getCount(); i++)
{
View item = adapter.getView(i, null, null);
// if you want the item to be selectable as if it would be in a default ListView, then you can add following code as well:
item.setBackgroundResource(Functions.getThemeReference(context, android.R.attr.selectableItemBackground));
item.setTag(i);
item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// item would be retrieved with:
// adapter.getItem((Integer)v.getTag())
}
});
llExpandable.addView(item);
}
ExpandUtils.expand(llExpandable, null, 500);
}
else
{
ExpandUtils.collapse(llExpandable, null, 500);
}
辅助函数:getThemeReference
public static int getThemeReference(Context context, int attribute)
{
TypedValue typeValue = new TypedValue();
context.getTheme().resolveAttribute(attribute, typeValue, false);
if (typeValue.type == TypedValue.TYPE_REFERENCE)
{
int ref = typeValue.data;
return ref;
}
else
{
return -1;
}
}
助手类:ExpandUtils
Kavin Varnan已经发布了如何为布局设置动画... 但如果你想使用我的课程,请随意这样做,我发布了一个要点:https://gist.github.com/MichaelFlisar/738dfa03a1579cc7338a
答案 4 :(得分:0)
你可以像使用平滑的展开/折叠动画CheckBox一样使用ExpandableLayout,这样你就可以在ListView和RecyclerView中将它用作CheckBox。
答案 5 :(得分:0)
这是 @TonicArtos 所提及的示例代码,用于添加和删除项目以及在执行时为其设置动画,这取自RecyclerView Animations和GitHub sample < / p>
1)在 onCreateViewHolder()中添加监听器以注册onClick
2)在适配器中创建自定义 OnClickListener
private View.OnClickListener mItemListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) v.findViewById(R.id.tvItems);
String selected = tv.getText().toString();
boolean checked = itemsList.get(recyclerView.getChildAdapterPosition(v)).isChecked();
switch (selected){
case "Item1":
if(checked){
deleteItem(v);
itemsList.get(recyclerView.getChildAdapterPosition(v)).setChecked(false);
}else {
addItem(v);
itemsList.get(recyclerView.getChildAdapterPosition(v)).setChecked(true);
}
break;
case "Item2":
if(checked){
deleteItem(v);
itemsList.get(recyclerView.getChildAdapterPosition(v)).setChecked(false);
}else {
addItem(v);
itemsList.get(recyclerView.getChildAdapterPosition(v)).setChecked(true);
}
break;
default:
//In my case I have checkList in subItems,
//checkItem(v);
break;
}
}
};
3)添加你的addItem()和deleteItem()
private void addItem(View view){
int position = recyclerView.getChildLayoutPosition(view);
if (position != RecyclerView.NO_POSITION){
navDrawItems.add(position+1,new mObject());
navDrawItems.add(position+2,new mObject());
notifyItemRangeInserted(position+1,2);
}
}
private void deleteItem(View view) {
int position = recyclerView.getChildLayoutPosition(view);
if (position != RecyclerView.NO_POSITION) {
navDrawItems.remove(position+2);
navDrawItems.remove(position+1);
notifyItemRangeRemoved(position+1,2);
}
}
4) 如果您的RecyclerViewAdapter与Recycler View不在同一个活动,请在创建时将 recyclerView 的实例传递给适配器
5) itemList是一个类型为 mObject 的ArrayList,它有助于维护项目(打开/关闭),名称,项目类型(子项/主项)的状态并设置基于值的主题
public class mObject{
private String label;
private int type;
private boolean checked;
}
答案 6 :(得分:0)