点击RecyclerView中的Item条目用ViewPager显示大图

效果图展示

a.gif

1.activity_main.xml



    
    

    

    

2.MainActivity中

public class MainActivity extends AppCompatActivity implements MyWelfareAdapter.OnClickListener {

    private RecyclerView lv;
    private ArrayList list;
    private MyWelfareAdapter adapter;
    private String url = "接口地址";
    private static final String TAG = "MainActivity----";
    private ArrayList viewList;
    private ViewPager vp;
    private TextView txt;
    private MyPagerAdapter pagerAdapter;
    private ImageView img;
    private int position;

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

    private void initData() {
        OkHttpClient httpClient = new OkHttpClient.Builder().build();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = httpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        final Gson gson = new Gson();
                        WelfareBean welfareBean = gson.fromJson(string, WelfareBean.class);
                        list.addAll(welfareBean.getResults());
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    private void initView() {
        lv = (RecyclerView) findViewById(R.id.lv);
        vp = (ViewPager) findViewById(R.id.vp);
        txt = (TextView) findViewById(R.id.txt);
        list = new ArrayList<>();
        viewList = new ArrayList<>();
        adapter = new MyWelfareAdapter(this, list);
        lv.setAdapter(adapter);

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);//不设置的话,图片闪烁错位,有可能有整列错位的情况。
        lv.setLayoutManager(layoutManager);

        pagerAdapter = new MyPagerAdapter(this, viewList);
        vp.setAdapter(pagerAdapter);

        //RecyclerView的监听方法
        adapter.setOnClickListener(this);
        //ViewPager的监听方法
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                txt.setText(i + 1 + "/" + 20);//索引是从0开始,所以让i+1
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

   
    @Override
    public void OnClick(int position, WelfareBean.ResultsBean resultsBean, ArrayList list) {
        //点击Item条目时让RecyclerView隐藏,ViewPager和TextView显示
        lv.setVisibility(View.GONE);
        vp.setVisibility(View.VISIBLE);
        txt.setVisibility(View.VISIBLE);
        this.position = position;

        for (int i = 0; i < list.size(); i++) {
            //自定义一个布局,用于显示图片的.
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_image, null);
            img = view.findViewById(R.id.img);
            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  //当点击图片的时候让RecyclerView显示,vp和txt隐藏
                    lv.setVisibility(View.VISIBLE);
                    vp.setVisibility(View.GONE);
                    txt.setVisibility(View.GONE);
                }
            });
            //点击哪个图片显示哪个图片
            vp.setCurrentItem(position);
            Glide.with(MainActivity.this).load(list.get(i).getUrl()).into(img);
            viewList.add(view);
        }
        pagerAdapter.notifyDataSetChanged();
    }

你可能感兴趣的:(点击RecyclerView中的Item条目用ViewPager显示大图)