我有一个带有覆盖getView方法的listview来填充它。 现在,我想让列表中的每个项目动画或从右侧移动 通常出现该项目的左侧的屏幕。
每个项目的动画不应该同时开始,它必须在其他项目移动之前延迟几毫秒......
好吧,这是我的适配器类:
public class MyAdapter extends ArrayAdapter<String>{
private Context context;
private String[] info;
public MyAdapter(Context context, int resource,
String[] objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.info = objects;
}
protected class RowViewHolder {
public TextView text1;
public CheckBox cb;
public String ss;
}
@Override
public View getView(int pos, View inView, ViewGroup parent) {
View vix = inView;
RowViewHolder holder;
if (vix == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vix = inflater.inflate(R.layout.check_list, null);
}
holder = new RowViewHolder();
holder.text1 = (TextView) vix.findViewById(R.id.info_group);
holder.text1.setText(info[pos]);
holder.ss = info[pos];
holder.cb = (CheckBox) vix.findViewById(R.id.check);
holder.cb.setTag(holder.ss);
holder.cb.setOnCheckedChangeListener(CbListen);
vix.setTag(holder);
return vix;
}
private OnCheckedChangeListener CbListen = new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton com, boolean pool) {
// TODO Auto-generated method stub
String state = (com.getTag()).toString();
if(com.isChecked()){
System.out.println(state+" CHECKED");
}else{
System.out.println(state+" UNCHECKED");
}
}
};
}
任何想法? :)
更新
嗯,肯定是!哈哈:p
只需下载那些ApiDemos“就像Farhan所说的那样” 你们会在包视图中找到类似LayoutAnimation2样本的东西。
在那里,列表的每个项目都被动画化以向下填充 当alpha分别改变时,翻译动画。
这就是我为我的案子所做的事情:
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(500);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 50.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(1000);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
group_list.setLayoutAnimation(controller);
我把它放在我的setAdapter()函数下面, 你们可以通过加速减速效果使它看起来更加舒适。
:P
答案 0 :(得分:7)
@ user724861 给出了完美答案! 我怎么发现它把他建议的代码放在哪里令人困惑...我把这些代码放在我的 ListFragment活动中,如下所示..
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(300);
set.addAnimation(animation);
/*animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 50.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(10);
set.addAnimation(animation); just comment if you don't want :) */
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.5f);
lv.setLayoutAnimation(controller);
adapter = new LazyAdapter(getActivity(), numResults, nodes, tabType);
setListAdapter(adapter);
}
答案 1 :(得分:3)
答案 2 :(得分:2)
每个项目的动画不应该同时开始
如果你想要它,你可以这样做:
layout_controller.xml:
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="30%"
android:animation="@anim/scale" />
scale.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="0.1"
android:toXScale="1"
android:fromYScale="0.1"
android:toYScale="1.0"
android:duration="800"
android:pivotX="10%"
android:pivotY="10%"
android:startOffset="100" />
</set>
然后在您的Java中,在SetListAdapter()之后粘贴以下代码:
LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(
this, R.anim.layout_controller);
getListView().setLayoutAnimation(controller);
请注意&#34; android:delay&#34;制作动画从上一首动画开始后延迟。
答案 3 :(得分:1)
getView()填充活动中的每个项目。尝试使用Timer将动画创建到getView中。
答案 4 :(得分:1)
package com.Animation1;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
import java.util.ArrayList;
public class Animation1Activity extends ListActivity implements
AdapterView.OnItemSelectedListener {
Animation anim = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
anim = AnimationUtils.loadAnimation( this, R.anim.magnify );
setContentView(R.layout.main);
ListView v = getListView(); // set up item selection listener
v.setOnItemSelectedListener( this ); // see OnItemSelectedListener methods below
ArrayList<String> items = new ArrayList<String>();
items.add( "Red" );
items.add( "Grey" );
items.add( "Cyan" );
items.add( "Blue" );
items.add( "Green" );
items.add( "Yellow" );
items.add( "Black" );
items.add( "White" );
ArrayAdapter itemsAdapter = new ArrayAdapter( this, R.layout.row, items );
setListAdapter( itemsAdapter );
}
protected void onListItemClick(ListView l,
View v,
int position,
long id) {
v.startAnimation( anim );
}
// --- AdapterView.OnItemSelectedListener methods ---
public void onItemSelected(AdapterView parent,
View v,
int position,
long id) {
v.startAnimation( anim );
}
public void onNothingSelected(AdapterView parent) {}
}
答案 5 :(得分:0)
您还可以使用ListView的xml属性布局动画设置动画。 您可以使用 res / anim 文件夹下的xml文件创建任何类型的动画。如果您想将其重用于任何其他列表视图并且还能很好地管理您的代码,将会很有帮助。
如果您需要有关xml中动画制作的帮助,请与我们联系。
答案 6 :(得分:0)
只需添加listview活动父版面
即可机器人:animatelayoutchanges = “真”
享受!