简体   繁体   中英

Android activity transition on list view item click

How can i impliment slide etc.. animation transition when i open new activity by listview item click ?

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent intent = null;
            // global string to class
            selectedValue = String.valueOf(parent.getItemAtPosition(position));

            if (selectedValue.equals("item1")) {
                                    // ^^^  use any item value here you want
                Intent myIntent = new Intent(view.getContext(), activity1.class);
                startActivityForResult(myIntent,0);
            }

            else if (selectedValue.equals("item2")) {
                Intent myIntent = new Intent(view.getContext(), aactivity4.class);
                startActivityForResult(myIntent,0);
            }
        }
    });

santoash kumar answer is correct. If you struggle on R.anim.layout resource here an animation code work like how other chat application work(slide animation) when you click on the listview item.

Add those into your anim resource

R.anim.push_left_in

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/>
</set>

R.anim.push_left_out

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-20%" android:duration="@android:integer/config_mediumAnimTime"/>
</set>

R.anim.push_right_in

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-20%" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/>
    </set>

R.anim.push_right_out

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%" android:duration="@android:integer/config_mediumAnimTime"/>
</set>

In the activity1 or any activity you will like to perform this animation while it launch,add this code after your set setContentView()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    //rest of your code include setContentView();
}

Now you will find a problem when you try to navigation back to your listView activity either with pressing the back button or click on the view which represent the back button the animation still seem to be the default animation so do this when you try to end your current activity.

finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);

For the back button pressed use this code.

@Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
    }

If the animation I provide doesn't suit the animation you desire you can try to make your own custom animation from this link .

startActivityForResult(myIntent,0);
overridePendingTransition(R.anim.hold, R.anim.fade_in);

hold and fade in will be animation xmls

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