简体   繁体   English

动画活动始于Android

[英]animate activity start in Android

I need to animate my Activity when it starts up. 启动Activity时,我需要为其动画。 The Activity is started from a BaseAdapter class. 该活动从BaseAdapter类开始。 I tried using overridePendingTransition() but I can't seem to use that in the on click event. 我尝试使用overridePendingTransition()但似乎无法在on click事件中使用它。 How can I over come this? 我怎么能克服这个?

holder.userpic.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Log.d(" ", " value " + obj.get(position).get_post_id());
        Intent appInfo = new Intent("android.intent.action.Profile");
        appInfo.putExtra("pk", obj.get(position).get_foodie_id());

        context.startActivity(appInfo);
        overridePendingTransition(R.anim.full_side_up,0); // cant use this
    }
});

You need to attach the Context as: 您需要将Context附加为:

context.overridePendingTransition(R.anim.full_side_up,0); 

Or you can use the transition in onResume method inside the new Activity : 或者,您可以在新Activity中的onResume方法中使用过渡:

@Override
public void onResume() {
    super.onResume();
    overridePendingTransition(R.anim.full_side_up,0);
}  

Let me know if this works. 让我知道这个是否奏效。

overridePendingTransition is a method of the Activity class. overridePendingTransitionActivity类的方法。 You would have to hold a reference to an activity. 您将必须保留对活动的引用。

An alternative would be specifying your listener in XML using onClick attribute as follows: 一种替代方法是使用onClick属性以XML指定侦听器,如下所示:

<ImageView
    id="@+id/user_pic"
    ...
    onClick="onClickUserPic" />

Then you create method onClickUserPic(View) inside your activity (which contains the ListView filled by your adapter class): 然后,在活动(其中包含由适配器类填充的ListView onClickUserPic(View)创建方法onClickUserPic(View) ):

public void onClickUserPic(View view) {
  int position = (Integer)view.getTag(); // here is stored position in list

  Log.d(" ", " value " + obj.get(position).get_post_id());
  Intent appInfo = new Intent("android.intent.action.Profile");
  appInfo.putExtra("pk", obj.get(position).get_foodie_id());

  context.startActivity(appInfo);
  overridePendingTransition(R.anim.full_side_up,0);
}

Note that this method has to be inside this and any other activity which contains the clickable image view. 请注意,此方法必须在此方法以及包含可单击图像视图的任何其他活动中。 If it weren't, you would get a NoSuchMethodException on click. 如果不是,则单击时将出现NoSuchMethodException

Finally add this line to your adapter: 最后将此行添加到您的适配器:

holder.userpic.setTag(Integer.valueOf(position)); // save position for reference

Also make sure that you have access to obj from inside the activity. 还要确保您可以从活动内部访问obj

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

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