ListView设置逐条加载动画,第一条总是不断重复

项目是在dialog中加入listview,但是需要逐条加载动画效果,项目写出来之后,发现第一个条目的动画总是不断重复,但是明明每个条目的动画都是调用的同一个方法doAnimation(),即都设置为不重复了的,可是结果却不理想,所以一直不知道为什么……

目标效果图:

实际效果图:
1

自定义dialog的文件KeyValueMessageDialog.java:
package com.example.listdialogdemo.dialog;
import java.util.List;
import com.example.listdialogdemo.
import com.example.listdialogdemo.data.KeyValueDialogContent;
import com.example.listdialogdemo.view.UnScrollListView;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class KeyValueMessageDialog extends Dialog implements android.view.View.OnClickListener {


private Context context;
private String title = "温馨提示", cancelName = "取消", sureName = "确定";
private List<KeyValueDialogContent> mContentList;
private ICallBack callBack;
private TextView txtTitle;
private Button btnCancel, btnSure;
private boolean isPicTitle = false;
private UnScrollListView msgListView;
private ImageView imgDialogNotice;
private LinearLayout llDialogTitle;
private int mStyle = 1;
/**
* 前面有序号,并且序号作为键
*/
public static int ORDINAL_STYLE = 1;
/**
* 前面无序号,键值都需要提供
*/
public static int UNORDINAL_STYLE = 2;


public interface ICallBack {
void resultClick(boolean click);
}


public void onResultCallBack(ICallBack callBack) {
// TODO Auto-generated method stub
this.callBack = callBack;
}


/**
* @param context
*            上下文
* @param contentList
*            传过来的键值对内容list
* @param style
*            内容显示的样式:有序:ORDINAL_STYLE、无序:UNORDINAL_STYLE
*/
public KeyValueMessageDialog(Context context, List<KeyValueDialogContent> contentList, int style) {
super(context, R.style.MessageDialogStyle);
// TODO Auto-generated constructor stub
this.context = context;
this.mContentList = contentList;
this.mStyle = style;
this.setCancelable(false);// 设置点击空白处不被关闭
}


public KeyValueMessageDialog(Context context, String title, List<KeyValueDialogContent> contentList, int style) {
super(context, R.style.MessageDialogStyle);
// TODO Auto-generated constructor stub
this.context = context;
this.mContentList = contentList;
this.title = title;
this.mStyle = style;
this.setCancelable(false);// 设置点击空白处不被关闭
}


public KeyValueMessageDialog(Context context, String title, List<KeyValueDialogContent> contentList, int style,
String cancelName, String sureName) {
super(context, R.style.MessageDialogStyle);
// TODO Auto-generated constructor stub
this.context = context;
this.mContentList = contentList;
this.title = title;
this.mStyle = style;
this.cancelName = cancelName;
this.sureName = sureName;
this.setCancelable(false);// 设置点击空白处不被关闭
}
public KeyValueMessageDialog(Context context, boolean isPicTitle, List<KeyValueDialogContent> contentList,
int style, String cancelName, String sureName) {
super(context, R.style.MessageDialogStyle);
// TODO Auto-generated constructor stub
this.context = context;
this.mContentList = contentList;
this.isPicTitle = isPicTitle;
this.mStyle = style;
this.cancelName = cancelName;
this.sureName = sureName;
this.setCancelable(false);// 设置点击空白处不被关闭
}


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_keyvalue_msg);
initView();
}


private void initView() {
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
DisplayMetrics dm = context.getResources().getDisplayMetrics();
float density = dm.density;
density = 1;
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
// int height = wm.getDefaultDisplay().getHeight()/4;
params.width = (int) (width * density) * 3 / 4;
// params.height = (int) (height * density);
// params.gravity = Gravity.BOTTOM;
window.setAttributes(params);


imgDialogNotice = (ImageView) findViewById(R.id.img_dialog_notice);
llDialogTitle = (LinearLayout) findViewById(R.id.ll_dialog_title);
txtTitle = (TextView) findViewById(R.id.txt_title);
btnCancel = (Button) findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(this);
btnSure = (Button) findViewById(R.id.btn_sure);
btnSure.setOnClickListener(this);
msgListView = (UnScrollListView) findViewById(R.id.msg_listview);
msgListView.setAdapter(new KeyValueMsgAdapter());


msgListView.setOnTouchListener(new OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
return true;
default:
break;
}
return true;
}
});


if (title == null || title.equals("")) {
txtTitle.setVisibility(View.GONE);
} else {
txtTitle.setText(title);
}
if (cancelName == null || cancelName.equals("")) {
btnCancel.setVisibility(View.GONE);
findViewById(R.id.txt_line).setVisibility(View.GONE);
btnSure.setBackgroundResource(R.drawable.selector_click_onel);
} else {
btnCancel.setText(cancelName);
}
btnSure.setText(sureName);


if (isPicTitle) {
llDialogTitle.setVisibility(View.GONE);
imgDialogNotice.setVisibility(View.VISIBLE);
} else {
llDialogTitle.setVisibility(View.VISIBLE);
imgDialogNotice.setVisibility(View.GONE);
}
}


Handler handler = new Handler();


/**
* @param view
*            执行动画的view
* @param postion
*            用于设置延迟执行的时间
*/
private void doAnimation(View view, int postion) {


int height = view.getHeight();
view.setVisibility(View.VISIBLE);
TranslateAnimation animation = new TranslateAnimation(0, 0, height, 0);
animation.setDuration(500);// 设置动画持续时间
animation.setRepeatCount(0);// 设置重复次数
animation.setRepeatMode(Animation.INFINITE);// 设置反方向执行
view.startAnimation(animation);
if (postion == mContentList.size() - 1) {
handler.postDelayed(new Runnable() {


@Override
public void run() {
btnSure.setEnabled(true);
btnCancel.setEnabled(true);
btnSure.setTextColor(Color.parseColor("#3693fd"));
btnCancel.setTextColor(Color.parseColor("#3693fd"));
}
}, 1000);
}
}
class KeyValueMsgAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return mContentList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mContentList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final MyViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.view_keywithvalue, null);
holder = new MyViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (MyViewHolder) convertView.getTag();
}
holder.key.setText(mContentList.get(position).getKey());
holder.value.setText(mContentList.get(position).getValue());

if (position >= 0) {
handler.postDelayed(new Runnable() {


@Override
public void run() {
doAnimation(holder.value, position);
}
}, position  * 700);
}
return convertView;
}
}

class MyViewHolder {
private TextView key;
private TextView value;

MyViewHolder(View convertView) {
key = (TextView) convertView.findViewById(R.id.txt_key_fordialog);
value = (TextView) convertView.findViewById(R.id.txt_value_fordialog);
}
}


@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.btn_sure:
callBack.resultClick(true);
break;
case R.id.btn_cancel:
callBack.resultClick(false);
break;
default:
break;
}
dismiss();
}
}
dialog的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:orientation="vertical" >


    <LinearLayout
        android:id="@+id/ll_dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_radius_left_right_top"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/txt_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="温馨提示"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="#cccccc" />
    </LinearLayout>


    <ImageView
        android:id="@+id/img_dialog_notice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/xhd_img_dialog_notice"
        android:visibility="gone" />


    <com.example.listdialogdemo.view.UnScrollListView
        android:id="@+id/msg_listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:divider="@null"
        android:paddingBottom="15dp" >
    </com.example.listdialogdemo.view.UnScrollListView>


    <TextView
        android:id="@+id/txt_notice_desc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:gravity="center"
        android:paddingBottom="15dp"
        android:paddingTop="15dp"
        android:text="信息一旦提交无法修改,请务必确认。"
        android:textColor="#FF7F18"
        android:textSize="10sp" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:background="#cccccc" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_click_cancel"
            android:enabled="false"
            android:text="取消"
            android:textColor="#96AAC1"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/txt_line"
            android:layout_width="0.5dp"
            android:layout_height="fill_parent"
            android:background="#cccccc" />
        <Button
            android:id="@+id/btn_sure"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_click_sure"
            android:enabled="false"
            android:text="确定"
            android:textColor="#96AAC1"
            android:textSize="17sp" />
    </LinearLayout>
</LinearLayout>




dialog中listview的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="2dp" >


    <TextView
        android:id="@+id/txt_key_fordialog"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:paddingRight="12dp"
        android:text="身份证号"
        android:textSize="14sp"
        android:textColor="#323232" />

    <TextView
        android:id="@+id/txt_value_fordialog"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingLeft="8dp"
        android:textSize="14sp"
        android:text="43042419920605683X"
        android:visibility="invisible"
        android:textColor="#323232">
    </TextView>
</LinearLayout>

调用dialog:
mKeyValueContentList.clear();

KeyValueDialogContent msg1 = new KeyValueDialogContent();
msg1.setKey("姓        名");
msg1.setValue("XXX");
mKeyValueContentList.add(msg1);
KeyValueDialogContent msg2 = new KeyValueDialogContent();
msg2.setKey("身份证号");
msg2.setValue("4*****************X");
mKeyValueContentList.add(msg2);
KeyValueDialogContent msg3 = new KeyValueDialogContent();
msg3.setKey("签发机关");
msg3.setValue("你猜");
mKeyValueContentList.add(msg3);
KeyValueDialogContent msg4 = new KeyValueDialogContent();
msg4.setKey("有效期限");
msg4.setValue("你猜");
mKeyValueContentList.add(msg4);


// 提交身份证信息


// KeyValueMessageDialog dialog = new KeyValueMessageDialog(this, true, mKeyValueContentList,
// KeyValueMessageDialog.UNORDINAL_STYLE, "有误,去修改", "正确,下一步");
KeyValueMessageDialog dialog = new KeyValueMessageDialog(MainActivity.this, true, mKeyValueContentList, KeyValueMessageDialog.UNORDINAL_STYLE, "有误,去修改", "正确,下一步");
dialog.onResultCallBack(new KeyValueMessageDialog.ICallBack() {


@Override
public void resultClick(boolean click) {
// TODO Auto-generated method stub
int tag;
if (!click) {
tag = 0;
} else {
tag = 1;
}
switch (tag) {
case 0:
break;
case 1:
break;
default:
break;
}
}
});
if (!dialog.isShowing())
dialog.show();

然而,这样实现的效果并不满意,就是第一条总会重复,而其他的就正常。
通过网上找原因,发现可能原因如下:

因为ListView没办法确定它一次需要实例化多少个 convertView,即调用多少次getView方法。而导致这样的结果可能有以下原因:

1、你自己重写的ListView在实例化以后直接使用,而没有给它指定高度和宽度。

2、将ListView布局在xml中高度值指定为了Wrap_Content

3、将ListView布局到一个父组件,ListView本身的height是fillParent,但是父类组件在其父组件中高度为Wrap_Content 


于是取消listview的复用效果
发现问题解决了,成功的实现了效果。
//			if (convertView == null) {
				if (mStyle == ORDINAL_STYLE) {
					convertView = LayoutInflater.from(context).inflate(R.layout.view_keywithvalue2, null);
				} else if (mStyle == UNORDINAL_STYLE) {
					convertView = LayoutInflater.from(context).inflate(R.layout.view_keywithvalue, null);
				}
				holder = new MyViewHolder(convertView);
				convertView.setTag(holder);
//			} else {
//				holder = (MyViewHolder) convertView.getTag();
//			}

			holder.key.setText(mContentList.get(position).getKey());
			holder.value.setText(mContentList.get(position).getValue());
然而,我发现如果条目较多的话,不复用还是不太好的,所以另一种修改方式是
把listview的第一条项目设置成空,也就是说,把第一条设置成空之后,不管它怎么重复,反正我看不到,从而也解决了复用的问题
mKeyValueContentList.clear();
KeyValueDialogContent msg0 = new KeyValueDialogContent();
msg0.setKey("");
msg0.setValue("");
mKeyValueContentList.add(msg0);
KeyValueDialogContent msg1 = new KeyValueDialogContent();
msg1.setKey("姓        名");
msg1.setValue("XXX");
mKeyValueContentList.add(msg1);
KeyValueDialogContent msg2 = new KeyValueDialogContent();
msg2.setKey("身份证号");
msg2.setValue("4****************X");
mKeyValueContentList.add(msg2);
KeyValueDialogContent msg3 = new KeyValueDialogContent();
msg3.setKey("签发机关");
msg3.setValue("你猜");
mKeyValueContentList.add(msg3);
KeyValueDialogContent msg4 = new KeyValueDialogContent();
msg4.setKey("有效期限");
msg4.setValue("你猜");
mKeyValueContentList.add(msg4);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值