安卓可扩展回收器查看不同的卡高度
2022-09-02 22:27:02
我有一个 RecyclerView,其中包含一个卡片列表,每个卡片都扩展为子卡片。
每张卡片都有不同的文本。我希望当用户单击儿童卡时,它将展开以显示其中的文本。扩展高度基于卡片包含的文本量。
我尝试使用以下方法测量目标高度:
view.Measure(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
然后将卡片扩展到“测量高度”(请参阅此处)。但是,它为所有卡提供相同的测量高度。
这是主适配器,用于创建和绑定父卡和子卡:
public class HalachaExpandableAdapter : ExpandableRecyclerAdapter<HalachaParentViewHolder, HalachaChildViewHolder>, View.IOnClickListener
{
LayoutInflater _inflater;
bool expand;
int targetHeight;
bool wave = false;
public HalachaExpandableAdapter(Context context, List<IParentObject> itemList) : base(context, itemList)
{
_inflater = LayoutInflater.From(context);
}
public override void OnBindChildViewHolder(HalachaChildViewHolder childViewHolder, int position, object childObject)
{
var halachaChild = (HalachaChild)childObject;
childViewHolder.halachaChildTitle.Text = halachaChild.Title.ToString();
targetHeight = childViewHolder.halachaChildCard.Height;
childViewHolder.halachaChildCard.LayoutParameters.Height = 100;
childViewHolder.halachaChildCard.SetOnClickListener(this);
expand = childViewHolder.expand;
}
public override void OnBindParentViewHolder(HalachaParentViewHolder parentViewHolder, int position, object parentObject)
{
var halacha = (HalachaItem)parentObject;
parentViewHolder._halachaTitleTextView.Text = halacha.Title();
parentViewHolder._halachaContentTextView.Text = halacha.Content;
if (halacha.ChildObjectList.Count == 1)
wave = true;
}
public void OnClick(View v)
{
if (v.Height == 100)
{
AnimationCollapse anim = new AnimationCollapse(v, targetHeight, 100);
anim.Duration = 300;
v.StartAnimation(anim);
expand = false;
}
else
{
AnimationCollapse anim = new AnimationCollapse(v, 100, v.Height);
anim.Duration = 300;
v.StartAnimation(anim);
expand = true;
}
}
public override HalachaChildViewHolder OnCreateChildViewHolder(ViewGroup childViewGroup)
{
var view = _inflater.Inflate(Resource.Layout.halachotListItem, childViewGroup, false);
return new HalachaChildViewHolder(view);
}
public override HalachaParentViewHolder OnCreateParentViewHolder(ViewGroup parentViewGroup)
{
var view = _inflater.Inflate(Resource.Layout.halachotListHeader, parentViewGroup, false);
wave = false;
return new HalachaParentViewHolder(view);
}
}
我认为这是需要完成代码的地方,但是如果您需要其他类的其他一些代码,我将很乐意发布它们。您还可以查看上面的链接,以了解其工作原理。
希望有人能帮助我。
谢谢!