Viewpager中的setCurrentItem不起作用

时间:2017-03-15 16:41:08

标签: android android-studio android-viewpager

我正在创建一个图库应用,我正在使用viewpager来显示照片。

图像列表的类。

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class FotosList extends AppCompatActivity {

    public static FotosList m;

    private final Integer image_ids[] = {
            R.drawable.foto1,
            R.drawable.foto2,
            R.drawable.foto3,
            R.drawable.foto4,
            R.drawable.foto5,
            R.drawable.foto6,
            R.drawable.foto8,
            R.drawable.foto9,
            R.drawable.foto10,
            R.drawable.foto11,
            R.drawable.foto12,
            R.drawable.foto13,
            R.drawable.foto14,
            R.drawable.foto15
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        m=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fotos_list);

        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.imagegallery);
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
        recyclerView.setLayoutManager(layoutManager);
        ArrayList<CreateList> createLists = prepareData();
        MyAdapter adapter = new MyAdapter(getApplicationContext(), createLists);
        recyclerView.setAdapter(adapter);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        finish();
        return true;
    }

    public class CreateList {

        private String image_title;
        private Integer image_id;
        public Integer getImage_ID() {
            return image_id;
        }

        public void setImage_ID(Integer android_image_url) {
            this.image_id = android_image_url;
        }
    }

    private ArrayList<CreateList> prepareData(){

        ArrayList<CreateList> theimage = new ArrayList<>();
        for(int i = 0; i< image_ids.length; i++){
            CreateList createList = new CreateList();
            createList.setImage_ID(image_ids[i]);
            theimage.add(createList);
        }
        return theimage;
    }

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private ArrayList<CreateList> galleryList;
        private Context context;

        public MyAdapter(Context context, ArrayList<CreateList> galleryList) {
            this.galleryList = galleryList;
            this.context = context;
        }

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_layout, viewGroup, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder viewHolder,final int i) {
            viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
            viewHolder.img.setImageResource((galleryList.get(i).getImage_ID()));
            viewHolder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent in = new Intent(FotosList.m, Fotos.class);
                    in.putExtra("img",galleryList.get(i).getImage_ID());
                    m.startActivity(in);
                }
            });
        }

        @Override
        public int getItemCount() {
            return galleryList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder{
            private TextView title;
            private ImageView img;
            public ViewHolder(View view) {
                super(view);

                title = (TextView)view.findViewById(R.id.title);
                img = (ImageView) view.findViewById(R.id.img);
            }
        }
    }

}

viewPager类:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import uk.co.senab.photoview.PhotoViewAttacher;

public class Fotos extends Activity {

    int[] mResources = {
            R.drawable.foto1,
            R.drawable.foto2,
            R.drawable.foto3,
            R.drawable.foto4,
            R.drawable.foto5,
            R.drawable.foto6,
            R.drawable.foto8,
            R.drawable.foto9,
            R.drawable.foto10,
            R.drawable.foto11,
            R.drawable.foto12,
            R.drawable.foto13,
            R.drawable.foto14,
            R.drawable.foto15
    };

    int foto;

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

        CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(this);

        ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(mCustomPagerAdapter);


        try {
            foto = getIntent().getIntExtra("img", 0);
            mViewPager.setCurrentItem(foto);
        } catch (Exception e) {
        }

    }


    public class CustomPagerAdapter extends PagerAdapter {

        Context mContext;
        LayoutInflater mLayoutInflater;
        PhotoViewAttacher mAttacher;

        public CustomPagerAdapter(Context context) {
            mContext = context;
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

            ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
            imageView.setImageResource(mResources[position]);

            mAttacher = new PhotoViewAttacher(imageView);


            container.addView(itemView);

            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((LinearLayout) object);
        }
    }

}

单击照片以打开带有图像的viewpager ... 他打开了最后一张图片...... 如何打开点击图片

2 个答案:

答案 0 :(得分:0)

这是你的问题 -

viewHolder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent in = new Intent(FotosList.m, Fotos.class);
                    in.putExtra("img",galleryList.get(i).getImage_ID());
                    m.startActivity(in); // What is m??
                }
            });

in.putExtra("img",galleryList.get(i).getImage_ID()); 您正在传递imageId即resourceID作为额外内容,而您需要在adapter.

中找到图像的位置

取而代之的是 -

viewHolder.img.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
          Intent in = new Intent(FotosList.m, Fotos.class);
          in.putExtra("img", getAdapterPosition()); // getAdapterPosition() will return position of current view
          context.startActivity(in); // What is m?? use context instead
      }
});

答案 1 :(得分:0)

onClickViewPager中的

只是将点击的项目的位置传递给第二个活动

viewHolder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent in = new Intent(FotosList.m, Fotos.class);
                    in.putExtra("img",i);
                    m.startActivity(in);
                }
        });