我正在尝试创建一个包含可以展开以显示更多选项的卡片的RecyclerView。但是我不能让它正常工作,正如我在这里解释http://imgur.com/a/fC7pZ。我觉得我真的很接近答案,但是我已经撞墙了,我不知道我在哪里出错了。
cardAdapter.java
public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.JobViewHolder> {
LinearLayout mLinearLayoutOptions;
LinearLayout mLinearLayoutItem;
ValueAnimator mAnimator;
public static class JobViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cv;
TextView homeName;
TextView homeCity;
TextView homeSt;
TextView homeZip;
ImageView homePhoto;
JobViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
homeName = (TextView) itemView.findViewById(R.id.home_address);
homeCity = (TextView) itemView.findViewById(R.id.home_city);
homeSt = (TextView) itemView.findViewById(R.id.home_state);
homeZip = (TextView) itemView.findViewById(R.id.home_zip);
homePhoto = (ImageView) itemView.findViewById(R.id.home_photo);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
}
}
List<Homes> homes;
CardViewAdapter(List<Homes> home) {
this.homes = home;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public JobViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_home_item, viewGroup, false);
JobViewHolder pvh = new JobViewHolder(v);
mLinearLayoutOptions = (LinearLayout) v.findViewById(R.id.homeOptions);
mLinearLayoutItem = (LinearLayout) v.findViewById(R.id.homeDetails);
//expndIcon = (ImageView) v.findViewById(R.id.expand_details);
//sets up and handles cardview expand
mLinearLayoutOptions.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
mLinearLayoutOptions.getViewTreeObserver().removeOnPreDrawListener(this);
mLinearLayoutOptions.setVisibility(View.GONE);
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mLinearLayoutOptions.measure(widthSpec, heightSpec);
mAnimator = slideAnimator(0, mLinearLayoutOptions.getMeasuredHeight());
return true;
}
});
mLinearLayoutItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mLinearLayoutOptions.getVisibility() == View.GONE) {
//expndIcon.setImageResource(R.drawable.up);
expand();
} else {
//expndIcon.setImageResource(R.drawable.down);
collapse();
}
}
});
return pvh;
}
@Override
public void onBindViewHolder(JobViewHolder jobViewHolder, int i) {
jobViewHolder.homeName.setText(homes.get(i).address);
jobViewHolder.homeCity.setText(homes.get(i).city);
jobViewHolder.homeSt.setText(homes.get(i).st);
jobViewHolder.homeZip.setText(Integer.toString(homes.get(i).zip));
jobViewHolder.homePhoto.setImageResource(homes.get(i).photoId);
}
@Override
public int getItemCount() {
return homes.size();
}
//code for expanding
private void expand() {
//set Visible
mLinearLayoutOptions.setVisibility(View.VISIBLE);
mAnimator.start();
}
private void collapse() {
int finalHeight = mLinearLayoutOptions.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
//Height=0, but it set visibility to GONE
mLinearLayoutOptions.setVisibility(View.GONE);
}
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
mAnimator.start();
}
private ValueAnimator slideAnimator(int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//Update Height
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = mLinearLayoutOptions.getLayoutParams();
layoutParams.height = value;
mLinearLayoutOptions.setLayoutParams(layoutParams);
}
});
return animator;
}
}
我希望按钮对应于他们的卡,所以我假设我需要处理在创建它们的适配器中扩展卡。如果我错了,请纠正我,但我觉得我需要将它们绑在那里。
答案 0 :(得分:0)
mLinearLayoutOptions将始终具有在适配器中调用onCreateViewHolder的最后一个视图的引用。
相反,您应该将mLinearLayoutOptions存储在列表中,并将项目的索引设置为视图标记。
repositores {
mavenCentral()
maven {
url "https://your-maven-repository"
}
}