瀑布流(点击跳转)

添加的依赖

 compile 'com.android.support:recyclerview-v7:21.0.3'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.squareup.okio:okio:1.5.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.orhanobut:logger:1.15'
    compile 'com.android.support:mediarouter-v7:25.0.0'


 "android.permission.INTERNET" />
    "android.permission.WRITE_EXTERNAL_STORAGE">
    "android.permission.WRITE_EXTERNAL_STORAGE">
    "android.permission.MOUNT_UNMOUNT_FILESYSTEMS">

activity_main

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bawie.www.yuekaodemo.MainActivity">

   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/recyclerView">

   

item

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/a" />


MainActivity

package com.bawie.www.yuekaodemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;


import com.bawie.www.yuekaodemo.adapter.MyRecyclerViewAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    /**
     * 复用视图的控件
     */
    private RecyclerView recyclerView;

    /**
     * 显示的数据
     */
    private ArrayList mDatas;

    /**
     * RecyclerView的适配器
     */
    private MyRecyclerViewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1.找到控件
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //2.声名为瀑布流的布局方式: 3列,垂直方向
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        //3.为recyclerView设置布局管理器
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        initData();//初始化数据
        //3.创建适配器
        adapter = new MyRecyclerViewAdapter(this, mDatas);
        //设置添加,移除item的动画,DefaultItemAnimator为默认的
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //4.设置适配器
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent in=new Intent(MainActivity.this,GouWuCheActivity.class);
                startActivity(in);
            }
        });


    }

    //初始化数据
    protected void initData(){
        mDatas = new ArrayList();
        for (int i = 'A'; i < 'z'; i++){
            mDatas.add("" + (char) i);
            mDatas.add("R.drawable.a");
            mDatas.add("R.drawable.r");

        }
    }

}
MyRecyclerViewAdapter
package com.bawie.www.yuekaodemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bawie.www.yuekaodemo.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter implements RecyclerView.OnClickListener{
    private OnItemClickListener mOnItemClickListener = null;
    private Context context;
    private List list;//数据

    private List heightList;//装产出的随机数

    public MyRecyclerViewAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        //记录为每个控件产生的随机高度,避免滑回到顶部出现空白
        heightList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            int height = new Random().nextInt(200) + 100;//[100,300)的随机数

            heightList.add(height);

        }

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //找到item的布局
        View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        view.setOnClickListener(this);
        return new MyViewHolder(view);//将布局设置给holder
    }

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

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        //填充数据
        //  holder.imageView.setImageURI(Uri.parse(list.get(position)));
        // holder.tv


        //holder.itemView.setTag(position);
        //由于需要实现瀑布流的效果,所以就需要动态的改变控件的高度了
        ViewGroup.LayoutParams params = holder.imageView.getLayoutParams();
        params.height=heightList.get(position);
        holder.imageView.setLayoutParams(params);
        holder.itemView.setTag(position);

    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }
    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        // ImageView imageView2;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            // imageView2 = (ImageView) itemView.findViewById(R.id.imageView2);
        }
    }


}

 

转载于:https://www.cnblogs.com/yu12/p/7883532.html

你可能感兴趣的:(瀑布流(点击跳转))