滑动即可查看图像阵列中的下一张图片

时间:2019-05-21 04:27:20

标签: java image android-listview imageview

我有一个列表视图,该列表视图显示条目,当用户单击某个选项时,它将在第二个活动中显示图片。我希望用户随后能够滑动(或按下按钮)以查看阵列中的下一个图像。我的代码当前正在编译并将图像传递给imageDisplay活动。我想知道我需要实现什么以便用户能够滑动或按下按钮来查看下一张图片。有人有示例代码可以帮助我解决此问题吗?

ListView代码:

public class adultcardiaclist extends AppCompatActivity {
    private AdView mAdView;
    ListView listview;


    private int proceduresone[] = {
            R.drawable.procedures1,
            R.drawable.procedures2,
            R.drawable.procedures3,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adultcardiaclist);
        MobileAds.initialize(this, "ca-app-pub-9944401739416572~8067453677");

        mAdView = (AdView) findViewById(R.id.adviews);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        Button b1 = (Button) findViewById(R.id.homebutton);

        //Home Button-----------------------------------------------------------------------------------------------------
        b1.setOnClickListener(new View.OnClickListener () {

            public void onClick(View view) {
                Intent protocolcata = new Intent(adultcardiaclist.this, HomeScreen.class);
                startActivity(protocolcata);
            }
        });

// myImageList.add(new ImageDisplay("", R.drawable.ekghomepic));

        final Context context = getApplicationContext();

        //list contains image name, image location
        final List<ImageDisplay> myImageList = new ArrayList<ImageDisplay>();
        myImageList.add(new ImageDisplay("Asystole / PEA", proceduresone[1]));
        myImageList.add(new ImageDisplay("Asystole / PEA Page 2", R.drawable.procedures6));
        myImageList.add(new ImageDisplay("Bradycardia", R.drawable.procedures5));

        //define ListView and create onItemClick Listener
        listview = (ListView) findViewById(R.id.adultcardiaclistview);

        ArrayAdapter<ImageDisplay> arrayAdapters = new ArrayAdapter<ImageDisplay>(this, R.layout.support_simple_spinner_dropdown_item, myImageList);
        listview.setAdapter(arrayAdapters);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent myIntent = new Intent(context, imagedisplay.class);

                myIntent.putExtra("name", myImageList.get(position).getName());
                myIntent.putExtra("imagePath", myImageList.get(position).getPath());
                startActivityForResult(myIntent, 0);
            }
        });
    }

    public class ImageDisplay {
        private String Name;
        private int Path; // use String if you use a path, in here i'm storing image in drawable

        @Override
        public String toString() {
            return Name;
        }

        public ImageDisplay() {
        }

        public ImageDisplay(String name, int path) {
            this.Name = name;
            this.Path = path;
        }

        public String getName() {
            return Name;
        }

        public void setName(String name) {
            Name = name;
        }

        public int getPath() {
            return Path;
        }

        public void setPath(int path) {
            this.Path = path;
        }
    }
}

ImageDislay活动代码:

public class imagedisplay extends AppCompatActivity {


    private Integer images[] = {R.drawable.procedures1, R.drawable.procedures2, R.drawable.procedures3};
    private int currImage = 0;


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




        ImageView imageView = (ImageView) findViewById(R.id.imageView3);
        Intent myItent = getIntent();

        String Name = myItent.getStringExtra("name"); //Do whatever you need with image title
        int Path = myItent.getIntExtra("imagePath", 0); //pass this to ImageView


        imageView.setImageResource(Path);


        TouchImageView img = new TouchImageView(this);
        img.setImageResource(Path);
        img.setMaxZoom(4f);
        setContentView(img);
    }
    }

1 个答案:

答案 0 :(得分:0)

按照以下步骤使用ViewPager实施图片滑动

第1步:通过意图数据将图像列表和位置传递给图像显示活动。

步骤2:在imagedisplay活动中实现ViewPager。

第3步:创建PagerAdapter并将图像列表传递给适配器。

第4步:将pageradapter设置为viewpager