ListView设置逐条加载动画,第一条总是不断重复
来源:互联网 发布:数据库信息如何填写 编辑:程序博客网 时间:2024/06/07 10:04
项目是在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 stubthis.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 stubthis.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 stubthis.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 stubthis.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 stubthis.context = context;this.mContentList = contentList;this.isPicTitle = isPicTitle;this.mStyle = style;this.cancelName = cancelName;this.sureName = sureName;this.setCancelable(false);// 设置点击空白处不被关闭}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic 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() {@Overridepublic void run() {btnSure.setEnabled(true);btnCancel.setEnabled(true);btnSure.setTextColor(Color.parseColor("#3693fd"));btnCancel.setTextColor(Color.parseColor("#3693fd"));}}, 1000);}}class KeyValueMsgAdapter extends BaseAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn mContentList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn mContentList.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic 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() {@Overridepublic 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);}}@Overridepublic void onClick(View view) {// TODO Auto-generated method stubswitch (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() {@Overridepublic void resultClick(boolean click) {// TODO Auto-generated method stubint 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);
阅读全文
0 0
- ListView设置逐条加载动画,第一条总是不断重复
- Android 给listview/gridview设置加载动画(逐条加载条目动画)
- Android逐条加载ListView,实现动画更新
- Android逐条加载ListView,实现动画更新
- Android逐条加载ListView,实现动画更新
- Android逐条加载ListView,实现动画更新
- Android中给listview/gridview设置动画(逐条加载条目动画)
- Android中给listview/gridview设置动画(逐条加载条目动画)
- Android中给listview/gridview设置动画(逐条加载条目动画)
- Android中给listview/gridview设置动画(逐条加载条目动画)
- 给LinearLayout设置动画(可以逐条出来动画效果)
- listview重复混乱加载
- 动画加载ListView
- ListView添加加载动画
- dropdownlist总是选择第一条数据
- ListView显示第一条数据
- ListView 返回顶部第一条
- ListView设置滑动到底部自动加载 重复多次加载问题
- 欧拉函数知识点及其求法模板
- android studio 降低编译版本出错
- 数字增益和模拟增益
- 在linux上使用yum安装JDK
- HDU 6096 String (字典树, 2017 Multi-Univ Training Contest 6)
- ListView设置逐条加载动画,第一条总是不断重复
- JVM学习笔记-01-Java及Jvm发展历史
- osg Nofity使用
- 51Nod1005 大数加法(C语言)
- Java 修改jdk不起作用
- android 字体颜色随机
- hdu5922Minimum’s Revenge
- opencv 图像的载入显示和输出
- Python默认参数是[]