我正在使用listview viewHolder模式来呈现listview行。每行包含一个带背景的2垂直文本视图。 我在最后添加的视图中添加了一个动画。动画将行的宽度从父宽度更改为wrap_content。现在的问题是当我滚动时,一旦最后一行隐藏并出现第一个隐藏视图,新视图宽度就等于最后隐藏视图。
我认为这是因为listview recycleler正在回收最后隐藏的视图并提供新的可见视图,但如何调整宽度以包装内容而不是最后一个可见的视图宽度。
这是listview行xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/contentWithBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:background="@null"
android:textSize="14sp"
android:autoLink="all"
android:lineSpacingMultiplier="1.1"
android:layout_height="wrap_content"
android:paddingBottom="3dp"
android:textColor="@android:color/black"
android:maxWidth="250dp" />
<TextView
android:id="@+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_gravity="right"
android:textSize="9sp"
android:textColor="#802B2B2B" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
public static class ViewHolder {
public TextView txtMessage;
public TextView txtInfo;
public LinearLayout content;
public LinearLayout contentWithBG;
}
private ViewHolder createViewHolder(View v) {
ViewHolder holder = new ViewHolder();
holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
holder.content = (LinearLayout) v.findViewById(R.id.content);
holder.contentWithBG = (LinearLayout) v.findViewById(R.id.contentWithBackground);
holder.txtInfo = (TextView) v.findViewById(R.id.txtInfo);
return holder;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflator.inflate(R.layout.list_item_message, parent, false);
holder = createViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final TextView msg = holder.txtMessage;
final View contentBackground = holder.content;
msg.setText((chatMessage.getMsg()));
holder.txtInfo.setText(getTimeText(chatMessage));
//only add animation on last item
if((position == getCount() -1)){
TextView text = holder.txtMessage;
text.measure(0, 0);
int textWidth = text.getMeasuredWidth();
AnimationSet animationSet = new AnimationSet(false);
Animation anim = AnimationUtils.loadAnimation(context, R.anim.bottom_up);
ResizeAnimation a = new ResizeAnimation(text);
Display mDisplay = context.getWindowManager().getDefaultDisplay();
int width = mDisplay.getWidth();
width *= 0.75;
a.setDuration(250);
a.setParams(width, textWidth);
a.setStartOffset(150);
animationSet.addAnimation(anim);
animationSet.addAnimation(a);
holder.contentWithBG.startAnimation(animationSet);
}
}