简体   繁体   中英

Animation in ListView while scrolling using smoothScrollToPosition

I am trying to animate the scroll of my listView when I scroll it programatically.

Is it possible to set animation on scroll of a listView ? I went through few answers here, nothing was concrete.

Please help!!

I think you are looking for something like this, check out the below link.

https://github.com/nhaarman/ListViewAnimations

You can animate your list view items while scrolling, and this tutorial will guide you easily how to implement.

In the dependency dont forget to add the following line, because in that tutorial they forget to include this line in the gradle file.

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

your dependency will finally look like this,

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.+'
}

You can add animation in your adapter like this

 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>

I think simple way to so is,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM