简体   繁体   English

列表视图项目点击时的Android活动过渡

[英]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项目单击打开新活动时,如何隐含幻灯片等动画过渡?

 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. santoash kumar的答案是正确的。 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. 如果您在R.anim.layout资源上苦苦挣扎,那么在单击listview项时,动画代码的工作方式就和其他聊天应用程序(幻灯片动画)的工作方式一样。

Add those into your anim resource 将它们添加到您的动画资源中

R.anim.push_left_in 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 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 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 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() 在activity1或您希望在启动动画时执行此动画的任何活动中,在设置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. 现在,当您尝试通过按后退按钮或单击代表后退按钮的视图导航回到listView活动时,将会发现问题,动画似乎仍然是默认动画,因此当您尝试结束自己的动画时,请这样做当前活动。

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 保持和淡入将是动画xmls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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