简体   繁体   English

如何正确使用notify()来启动新活动?

[英]How to use notify() correctly to start new activity?

I have recyclerview connected to my SecondActivity and in Adapter's onclick method I call a method in my Second Activity. 我已经将recyclerview连接到SecondActivity,并且在Adapter的onclick方法中,我在Second Activity中调用了一个方法。 But that method needs to be static. 但是该方法必须是静态的。 I want to animate between slides, so when I click on one of the options of recyclerview screen should do slide_out_right animation onto third activity. 我想在幻灯片之间制作动画,因此当我单击recyclerview屏幕的选项之一时,应该在第三个活动上执行slide_out_right动画。 Following is code in Adapter 以下是适配器中的代码

     holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String selectedOption = item.getSelectedOption;//item is object of Item class(class used to populate recyclerview)
            SecondActivity.startThirdActivity(selectedOption);
        }
    });

Following is the method in SecondActivity 以下是SecondActivity中的方法

public static void startThirdActivity(String recyclerSelectedOption) {
    Intent intent = new Intent(this, ThirdActivity.class);
    Bundle extras = new Bundle();
    extras.putString(MESSAGE_FROM_SECOND, recyclerSelectedOption);
    intent.putExtras(extras);
    startActivity(intent);
    overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

}

Now as many of you will know that startActivity is a non-static method and I can't call it from static method. 现在,许多人都知道startActivity是一个非静态方法,我无法从静态方法中调用它。 But I also can't call non-static method from static recycler view. 但是我也不能从静态回收者视图调用非静态方法。 So I want to create a variable in my secondActivity and set it to null. 所以我想在secondActivity中创建一个变量并将其设置为null。 And I want a method to be called when this variable is set with a selected option from recyclerList, which will be non-static and which can jump to ThirdActivity using startActivity() method. 我希望在使用recyclerList中的选定选项设置此变量时调用该方法,该方法将是非静态的,并且可以使用startActivity()方法跳转到ThirdActivity。

I know it's a bit of a big question. 我知道这是个大问题。 Obviousely if you know of another way by which I can avoid doing all this and just animate directly from Adapter to ThirdActivity then let me know. 显然,如果您知道另一种方法可以避免执行所有操作,而直接从Adapter到ThirdActivity进行动画处理,请告诉我。 Thanks, HyperCoder 谢谢,HyperCoder

Use interface. 使用界面。

public interface OnItemClickListener {
    void onItemClick(String item);
}

Now adapter constructor will be 现在适配器构造器将是

private final OnItemClickListener listener;
public CustomAdapterAdapter(OnItemClickListener listener) {
    this.listener = listener;
}

Now onclick listener pass selcted option 现在onclick监听器传递选择的选项

holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            String selectedOption = item.getSelectedOption;
            listener.onItemClick(selectedOption );
        }
    });

You have to implement OnItemClickListener on SecondActivity 您必须在OnItemClickListener上实现SecondActivity

public class SecondActivity extends AppCompatActivity implements OnItemClickListener{
        @Override
        public void onItemClick(String item){
            startThirdActivity(item)
            }

}

You can pass the context from the view to SecondActivity like this: 您可以像这样将contextview传递到SecondActivity

SecondActivity.startThirdActivity(selectedOption, view.getContext())

And then use the context to start the activity like this: 然后使用上下文启动活动,如下所示:

context.startActivity(intent);

I believe you can call overridePendingTransition in onCreate of ThirdActivit 我相信您可以在ThirdActivit onCreate中调用overridePendingTransition

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

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