Android java - 为 2 个或更多 viewpager 制作 1 个 ImageAdapter(目前为 2 个 viewpager 制作 2 个适配器)

Android java - making 1 ImageAdapter for 2 or more viewpagers (currently 2 adapters for 2 viewpagers)

我制作了一个包含图像的 ImageAdapter class 和一个包含 2 个按钮的 activity,单击每个按钮都会打开一个带有 viewpager 的新 activity。 2 个 viewpager 有不同的图像,这就是我缺乏经验的地方。

我采用了简单的方法复制了 ImageAdapter(将其命名为 ImageAdapter2)并将其链接到第二个 viewpager。 visual representation

它工作正常,但我想做的是清理它并通过 1 个适配器和 1 个 viewpager 完成所有操作。我试图通过 Intent getStringExtra 来完成,但它无法识别 Intent 方法。这是我的 classes:

ImageAdapter(ImageAdapter2 相同,只是在 sliderImageId 中有不同的图像):

    package hr.cnzd.prepoznajmoenasilje;

    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.LinearLayout;

    import androidx.viewpager.widget.PagerAdapter;
    import androidx.viewpager.widget.ViewPager;


    public class ImageAdapter extends PagerAdapter {

        Context mContext;

        ImageAdapter(Context context){
            this.mContext = context;
        }

        @Override
        public boolean isViewFromObject(View view, Object o){
            return view == ((ImageView) o);
        }

        private int[] sliderImageId = new int[]{
                R.drawable.djeca01, R.drawable.djeca02, R.drawable.djeca03, R.drawable.djeca04, R.drawable.djeca05, R.drawable.djeca06, R.drawable.djeca07, R.drawable.djeca08
        };

        @Override
        public Object instantiateItem(ViewGroup viewGroup, int position){
            ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setImageResource(sliderImageId[position]);
            ((ViewPager) viewGroup).addView(imageView, 0);
            return imageView;
        }


        @Override
        public void destroyItem(ViewGroup viewGroup, int position, Object o){
            ((ViewPager) viewGroup).removeView((ImageView) o);
        }

        @Override
        public int getCount(){
            return  sliderImageId.length;
        }

    }

Activity 按钮:

package hr.cnzd.prepoznajmoenasilje;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
public class MA_Savjeti extends AppCompatActivity {
    private ImageButton imageButtonDjeca;
    private ImageButton imageButtonOdrasli;

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

        imageButtonDjeca = findViewById(R.id.imgBtnDjeca);
        imageButtonDjeca.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivitySavjetiDjeca();
            }
        });

        imageButtonOdrasli = findViewById(R.id.imgBtnOdrasli);
        imageButtonOdrasli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivitySavjetiOdrasli();
            }
        });
    }

    public void openActivitySavjetiDjeca() {
        Intent intent = new Intent(this, Savjeti_Djeca.class);
        intent.putExtra("savjeti", "djeca");
        startActivity(intent);
    }

    public void openActivitySavjetiOdrasli() {
        Intent intent = new Intent(this, Savjeti_Odrasli.class);
        intent.putExtra("savjeti", "odrasli");
        startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
}

Activity with viewpager(其他activity with viewpager代码相同,只是调用了ImageAdapter2)

package hr.cnzd.prepoznajmoenasilje;

import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

public class Savjeti_Djeca extends AppCompatActivity {

    ViewPager viewPager;
    ImageAdapter imageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.savjeti_djeca);
        viewPager = findViewById(R.id.viewpager);
        imageAdapter = new ImageAdapter(this);
        viewPager.setAdapter(imageAdapter);
    }

}

TLDR:我有 2 个 ImageAdapter 和 2 个 Viewpager,但我想通过 1 个 ImageAdapter 和 1 个 Viewpager(前一个 activity 上的 2 个按钮决定哪个集合图片数量显示在 viewpager 上)

使用 ViewPager2 而不是这种方式解决了它。