瀑布流的适配器

- 编辑:小昭 -

瀑布流的适配器

activity里面
显示
rv.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
 
适配器
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 android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zxwl.zxyph.Bean.RvBean;
import com.zxwl.zxyph.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/8/28.
 */

public class RvTwoAdapter extends RecyclerView.Adapter<RvTwoAdapter.ViewHolder> {
    private ArrayList<RvBean> list;
    private Context context;
    private ButtonInterface buttonInterface;
    private LongButtonInterface longButtonInterface;
    public RvTwoAdapter(ArrayList<RvBean> list, Context context) {
        this.list = list;
        this.context = context;
    }



    public void buttonSetOnclick(ButtonInterface buttonInterface){
        this.buttonInterface=buttonInterface;

    }
    public void  setLongButtonInterface(LongButtonInterface longButtonInterface){
        this.longButtonInterface=longButtonInterface;
    }



    public interface ButtonInterface{
        public void onclick( View view,int position);
    }
    public interface LongButtonInterface{
        public void onlongclick( View view,int position);
    }










    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_two,parent,false));
    }



    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        holder.yyname.setText(list.get(position).getTitle());

        Glide.with(context).load(list.get(position).getImg().get(position)).into(holder.yyimg);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(buttonInterface!=null){
                    buttonInterface.onclick(view,position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(longButtonInterface!=null){
                    longButtonInterface.onlongclick(view,position);
                }
                return false;
            }
        });
//
//        ViewGroup.LayoutParams li = holder.itemView.getLayoutParams();
//
//        if(position%2==0){
//            li.height=200;
//            holder.itemView.setBackgroundColor(Color.parseColor("#33ff0000"));
//        }else{
//            li.height=100;
//            holder.itemView.setBackgroundColor(Color.parseColor("#33ff00ff"));
//        }
//        holder.itemView.setLayoutParams(li);
    }



    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder  {
        private final TextView yyname;
        private final ImageView yyimg;

        public ViewHolder(View itemView) {
            super(itemView);
            yyname = itemView.findViewById(R.id.RvTwo_name);
            yyimg = itemView.findViewById(R.id.RvTwo_img);
//            itemView.setOnClickListener(RvTwoAdapter.this);
//            itemView.setOnLongClickListener(RvTwoAdapter.this);

        }



    }

}
接口监听事件

rvAdapter.buttonSetOnclick(new RvTwoAdapter.ButtonInterface() {
    @Override
    public void onclick(View view, int position) {
         Toast.makeText(SevenSpfy.this, "duan", Toast.LENGTH_SHORT).show();


    }
});
rvAdapter.setLongButtonInterface(new RvTwoAdapter.LongButtonInterface() {
    @Override
    public void onlongclick(View view, int position) {
        Toast.makeText(SevenSpfy.this, "chang", Toast.LENGTH_SHORT).show();
    }
});




添加头布局
compile 'com.bartoszlipinski:recyclerviewheader:1.2.1'


布局
<com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader
    android:id="@+id/rcyhea"
    android:layout_width="match_parent"
    android:layout_height="200dp">

</com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader>
关联
rcyhea.attachTo(rcy,true);






 

你会喜欢下面的文章? You'll like the following article.