从viewpager中的活动开始新活动?

时间:2021-08-25 20:43:10

I am new to using viewpager to swipe between activities and currently have it set up like this:

我是新手使用viewpager在活动之间滑动,目前设置如下:

MainActivity.Java:

    package com.example.elfho.nceatracker;

    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
            viewPager.setAdapter(new CustomPagerAdapter(this));

        }
    }

ModelObject.Java:

public enum ModelObject {

        ONE(R.string.level1, R.layout.activity_level1),
        TWO(R.string.level2, R.layout.activity_level2),
        THREE(R.string.level3, R.layout.activity_level3);

        private int mTitleResId;
        private int mLayoutResId;

        ModelObject(int titleResId, int layoutResId) {
            mTitleResId = titleResId;
            mLayoutResId = layoutResId;
        }

        public int getTitleResId() {
            return mTitleResId;
        }

        public int getLayoutResId() {
            return mLayoutResId;
        }
     }

CustomPagerAdapter.java:

public enum ModelObject {

ONE(R.string.level1, R.layout.activity_level1),
TWO(R.string.level2, R.layout.activity_level2),
THREE(R.string.level3, R.layout.activity_level3);

private int mTitleResId;
private int mLayoutResId;

ModelObject(int titleResId, int layoutResId) {
    mTitleResId = titleResId;
    mLayoutResId = layoutResId;
}

public int getTitleResId() {
    return mTitleResId;
}

public int getLayoutResId() {
    return mLayoutResId;
 }

}

This makes the app swipe between my three activities, activity_level1, activity_level2, and activity_level3. However, I have not created specific java files for these activities because I don't need to in order to open them correctly. If I now want to open a new activity from one of these 3 activities, where would I need to write the code to make this work?

这使得应用程序可以在我的三个活动(activity_level1,activity_level2和activity_level3)之间滑动。但是,我没有为这些活动创建特定的java文件,因为我不需要为了正确打开它们。如果我现在想要从这3个活动之一中打开一个新活动,那么我需要在哪里编写代码才能使其工作?

1 个解决方案

#1


2  

Context https://developer.android.com/reference/android/content/Context.html. Here you can see why we use context.

上下文https://developer.android.com/reference/android/content/Context.html。在这里你可以看到为什么我们使用上下文

Like this way you can call another activity.

像这样你可以调用另一个活动。

public class CustomPagerAdapter extends Your_adapter_method {
 private Context context; 

 public CustomPagerAdapter(Context context) {
      this.context = context;     //Main Activity Context
 }

 public View getView(...){
     View v;
     v.setOnClickListener(new OnClickListener() {
         void onClick() {
              Intent intent= new Intent(context, secondactivity.class);
              intent.putextra("your_extra","your_class_value");
              context.startActivity(intent); //Must use context to start activity.
         }
     });
 }
}

Hope it helps.!

希望能帮助到你。!

#1


2  

Context https://developer.android.com/reference/android/content/Context.html. Here you can see why we use context.

上下文https://developer.android.com/reference/android/content/Context.html。在这里你可以看到为什么我们使用上下文

Like this way you can call another activity.

像这样你可以调用另一个活动。

public class CustomPagerAdapter extends Your_adapter_method {
 private Context context; 

 public CustomPagerAdapter(Context context) {
      this.context = context;     //Main Activity Context
 }

 public View getView(...){
     View v;
     v.setOnClickListener(new OnClickListener() {
         void onClick() {
              Intent intent= new Intent(context, secondactivity.class);
              intent.putextra("your_extra","your_class_value");
              context.startActivity(intent); //Must use context to start activity.
         }
     });
 }
}

Hope it helps.!

希望能帮助到你。!