如何为每个列表视图项制作翻译动画

2022-09-01 12:17:06

我有一个列表视图,覆盖getView方法来填充它。现在,我想使列表的每个项目都以动画形式或从屏幕右侧移动到项目通常应该出现的左侧。

每个项目的动画不应该同时开始,它必须延迟几个毫秒,然后其他项目移动...

好吧,这是我的适配器类:

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


答案 1

@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);
}

答案 2

使用布局动画!

在文档中,您可以通过android:layoutAnimation xml attr进行设置

看到这里


推荐