简体   繁体   English

在android Studio中,我想要当我单击按钮时,下一个活动/片段应该来自右侧

[英]In android Studio, I want when i click on button , next activity/fragment should come from right side

In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error. 在Android Studio中,我想当我点击按钮时,下一个活动/片段应该来自右侧,而当前活动应该左移。

holder.questions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(DoctorsProfile.this,Questions.class);
            i.putExtra("DOCTOR_ID",doctor_id);
            startActivity(i);
            overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);

        }
    });

overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. OverridePendingTransition在Activity上有效,但在Recyclerview和Listview Adapters上无效,请告知其他选项。 I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition. 我想当我单击recyclerview项时,下一个Activity应该通过使用overridePendingTransition导航或来自右侧。

Fragment fragment = Fragment.newInstance();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
            R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
            R.anim.fragment_slide_right_exit);
    Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
            .content_frame);

This tip features how to change Android's default animation when switching between Activities. 本技巧介绍了在“活动”之间切换时如何更改Android的默认动画。 The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. 在两个Activity之间更改动画的代码非常简单:在启动新的Intent之后,只需从当前Activity调用overridePendingTransition()。 This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity. 此方法可从Android 2.0版(API级别5)获得,它具有两个参数,这些参数用于定义当前Activity的输入和退出动画。 Here's an example: //Calls a new Activity 这是一个示例://调用一个新的Activity

startActivity(new Intent(this, NewActivity.class)); startActivity(new Intent(this,NewActivity.class));

//Set the transition -> method available from Android 2.0 and beyond //设置transition->从Android 2.0及更高版本可用的方法

overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up); overridePendingTransition(R.anim.slide_in_right_up,R.anim.slide_out_right_up);

These two parameters are resource IDs for animations defined with XML files (one for each animation). 这两个参数是使用XML文件定义的动画的资源ID(每个动画一个)。 These files have to be placed inside the app's res/anim folder. 这些文件必须放置在应用程序的res / anim文件夹中。 Examples of these files can be found at the Android API demo, inside the anim folder. 这些文件的示例可以在anim文件夹内的Android API演示中找到。

for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483 例如代码,请访问http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483

Change like this code you must be passing activity as context in adapter 像这样的代码更改,您必须在适配器中将活动作为上下文传递

holder.questions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    Intent i = new Intent(DoctorsProfile.this,Questions.class);
            i.putExtra("DOCTOR_ID",doctor_id);
            Activity activity = (Activity) context;
    activity.startActivity(i);
    activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
        }
    });

Note : Context is the Base Object of Activity 注意:上下文是活动的基础对象

Update : I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. 更新:我已经检查了接受的答案,但是希望您理解,每当您的活动启动时,它就会被调用,那不是最佳实践。 I am suggesting better approach if you want to follow the accepted answer . 如果您要遵循已接受的答案,我建议采用更好的方法。

Alternative : Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also. 替代方法:将捆绑软件中的一个参数传递给新活动,以确保来自该特定适配器的过渡,这样当您也来自任何其他活动时,就不会发生双重转换。

There is a easy way to do this. 有一个简单的方法可以做到这一点。 just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters. 只需将overridePendingTransition放在下一个Activity / Fragment的OnCreate方法上即可,以便下次出现Activity时,它会根据您的选择而来。不需要在适配器上添加overridePendingTransition。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ask_question);
    overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
  }

暂无
暂无

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

相关问题 如何在Android Studio中从活动到片段单击意图 - how to intent with on click button from activity to fragment in android studio Android Studio-单击按钮不会切换到下一个活动 - Android Studio - Button click not switching to next activity Android Studio - 如何在不使用按钮的情况下将数据从活动传递到片段 - Android Studio - How do I pass data from an activity to a fragment without using a button 我想从片段中隐藏片段容器视图(在 MainActivity 布局内),但是当我单击任务按钮然后重新打开应用程序时它不起作用 - i want to hide fragment container view (Inside MainActivity Layout) from fragment but its not working when i click task button and then reopen app 我想单击按钮将我的第一个活动的内容(即Java代码)复制到片段中 - I want to copy the content of my first activity (i.e the java code ) to my fragment on the click of a button 如何在按钮上显示随机活动单击在Android Studio中 - How Can I Display Random Activity On Button Click In Android Studio Android:显示第二天的日期,点击下一个按钮并显示数组中的文本视图 - Android: to display next day's date when I click the next button and to show text views from an array 单击1个菜单项后,我在右侧提供了溢出按钮,它应该转到该活动,但是我无法跳至该活动 - I have given overflow button on right side after clicking on 1 menu item it should go to that activity but I am unable to jump on that activity Android Studio:当它们没有直接连接时,如何在主要活动中获得一个按钮以在片段中执行某些操作? - Android Studio: How do I get a button in main activity to do something in a fragment when they aren't directly connected? 当我单击android中的最小化/返回按钮时,活动消失 - activity gets disappear when i click minimize/back button in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM