使用smoothScrollToPosition滚动时ListView中的动画

时间:2015-07-07 09:47:43

标签: java android

当我以编程方式滚动时,我正在尝试为listView的滚动设置动画。

是否可以在listView的滚动上设置动画?我在这里经历了几个答案,没有什么是具体的。

请帮助!!

3 个答案:

答案 0 :(得分:0)

我认为您正在寻找类似的内容,请查看以下链接。

https://github.com/nhaarman/ListViewAnimations

您可以在滚动时为列表视图项设置动画,本教程将指导您轻松实现如何实现。

在依赖项中不要忘记添加以下行,因为在该教程中,他们忘记将此行包含在gradle文件中。

  

compile' com.nineoldandroids:library:2.4。+'

你的依赖关系最终会是这样的,

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.nhaarman.listviewanimations:lib-core:3.1.0@aar'
compile 'com.nhaarman.listviewanimations:lib-manipulation:3.1.0@aar'
compile 'com.nhaarman.listviewanimations:lib-core-slh:3.1.0@aar'
compile 'com.nineoldandroids:library:2.4.+'
}

答案 1 :(得分:0)

您可以在适配器中添加动画,例如

 public class MyCursorAdapterOffers extends CursorAdapter {

    public MyCursorAdapterOffers(Context context, Cursor c) {
        super(context, c,0);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {



        ViewHolder holder = (ViewHolder) view.getTag();

        holder.title.setText("");
        holder.placeName.setText("");
        holder.prevousPrice.setText("");
        holder.price.setText("");

        ObjectAnimator.ofFloat(holder.ll, "translationX", -200, 0).setDuration(600).start();
        ObjectAnimator.ofFloat(holder.ll, "scaleX", 0.2f, 1.0f).setDuration(500)
        .start();
        ObjectAnimator.ofFloat(holder.ll, "scaleY", 0.2f, 1.0f).setDuration(500)
        .start();
    }

    private class ViewHolder{
        TextView title;
        TextView placeName;
        TextView prevousPrice;
        TextView price;
        ImageView imgOffers;
        LinearLayout ll;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.cella_offerte, null);


        TextView txtPlaceName = (TextView) view.findViewById(R.id.txtPlaceName);
        TextView txtTitleOf = (TextView) view.findViewById(R.id.txtTileOf);
        TextView txtPrevPrice = (TextView) view.findViewById(R.id.txtPreviusPrice);
        TextView txtPrice = (TextView) view.findViewById(R.id.txtPrice);
        ImageView imgOf = (ImageView) view.findViewById(R.id.imgOffers);
        LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_single_offer);

        ViewHolder myHolder = new ViewHolder();
        myHolder.title = txtTitleOf;
        myHolder.placeName = txtPlaceName;
        myHolder.prevousPrice = txtPrevPrice;
        myHolder.price = txtPrice;
        myHolder.imgOffers = imgOf;
        myHolder.ll = ll;

        view.setTag(myHolder);

        return view;
    }

}

<强> cella_offerte.xml

    <?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="match_parent"

    android:orientation="vertical" >

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

        <ImageView
            android:id="@+id/imgOffers"
            android:layout_width="wrap_content"
            android:layout_height="170dp"
            android:scaleType="centerCrop"
            android:src="@drawable/sp" />

        <TextView
            android:id="@+id/txtTileOf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="4 portate a scelta e vino"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txtPlaceName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Al castello"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#726e6e" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="right"
                android:orientation="vertical" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right" >

                    <TextView
                        android:id="@+id/txtPreviusPrice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="56,20 €"
                        android:gravity="center"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#726e6e" />

                    <View
                        android:id="@+id/view1"
                        android:layout_width="50dp"
                        android:layout_height="1dp"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:background="#726e6e" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/txtPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="39,20 €"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#76c2af"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

答案 2 :(得分:0)

我认为简单的方法是,

TranslateAnimation animation=new TranslateAnimation(-100, 0, 50, 0);
    animation.setDuration(300);
//now set animation to your listview item
    viewHolder.setAnimation(animation);