RecyclerView多条目加属性动画及点击事件

首先是依赖

 //ok
 compile 'com.squareup.okhttp3:okhttp:3.10.0'
 //gson
 compile 'com.google.code.gson:gson:2.8.2'
 //glide
 compile 'com.github.bumptech.glide:glide:4.7.1'
 //recyclerview
 compile 'cn.lemon:RefreshRecyclerView:1.4.1'
 compile 'com.android.support:recyclerview-v7:27.1.1'

Main布局

这里只是一个RecyclerView即可,没必要粘布局

RecyclerView的子布局根据条目类型决定,没必要粘布局,根据需求自行写布局,主要写逻辑代码

RecyclerView的适配器主要判断就在这里

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private NewBean.DataBeanX dlist;
    //判断条目类型的变量
    private int TE_IMG = 0;
    private int TE_THREE_IMG = 1;
    //自定义点击接口
    private OnItem onItem;

    public MyAdapter(Context context, NewBean.DataBeanX dlist) {
        this.context = context;
        this.dlist = dlist;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        //判断类型决定条目类型
        if(viewType == TE_IMG){
            View view = LayoutInflater.from(context).inflate(R.layout.holder_one, null);
            MyHoldero myHoldero = new MyHoldero(view);
            return myHoldero;
        }else if(viewType == TE_THREE_IMG){
            View view = LayoutInflater.from(context).inflate(R.layout.holder_two, null);
            MyHoldert myHoldert = new MyHoldert(view);
            return myHoldert;
        }

        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {

        if(holder instanceof MyHoldero){

            String qianurl = "http://365jia.cn/uploads/";
            ((MyHoldero) holder).hone_title.setText(dlist.getData().get(position).getTitle());
            ((MyHoldero) holder).hone_zan.setText(dlist.getData().get(position).getComment_amount()+"评论"+dlist.getData().get(position).getViews()+"赞");

            Glide.with(context).load(qianurl+dlist.getData().get(position).getPics().get(0)).into(((MyHoldero) holder).hone_img);
            //图片的属性动画
            ((MyHoldero) holder).hone_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   ObjectAnimator alpha = ObjectAnimator.ofFloat(((MyHoldero) holder).hone_img,"alpha",1,0,1);
                   alpha.setDuration(3000);
                   alpha.start();
                }
            });
            //点击事件
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItem.onItemLong(position);
                    return true;
                }
            });
        }else if(holder instanceof MyHoldert){

            String qianurl = "http://365jia.cn/uploads/";
            ((MyHoldert) holder).htwo_title.setText(dlist.getData().get(position).getTitle());
            ((MyHoldert) holder).htwo_zan.setText(dlist.getData().get(position).getComment_amount()+"评论"+dlist.getData().get(position).getViews()+"赞");

            //加载图片拼接图片地址
            Glide.with(context).load(qianurl+dlist.getData().get(position).getPics().get(0)).into(((MyHoldert) holder).htwo_imgo);
            Glide.with(context).load(qianurl+dlist.getData().get(position).getPics().get(1)).into(((MyHoldert) holder).htwo_imgt);
            Glide.with(context).load(qianurl+dlist.getData().get(position).getPics().get(2)).into(((MyHoldert) holder).htwo_imgh);

            //图片属性的动画
            ((MyHoldert) holder).htwo_imgo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ObjectAnimator alpha = ObjectAnimator.ofFloat(((MyHoldert) holder).htwo_imgo,"alpha",1,0,1);
                    alpha.setDuration(3000);
                    alpha.start();
                }
            });

            ((MyHoldert) holder).htwo_imgt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ObjectAnimator alpha = ObjectAnimator.ofFloat(((MyHoldert) holder).htwo_imgt,"alpha",1,0,1);
                    alpha.setDuration(3000);
                    alpha.start();
                }
            });

            ((MyHoldert) holder).htwo_imgh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ObjectAnimator alpha = ObjectAnimator.ofFloat(((MyHoldert) holder).htwo_imgh,"alpha",1,0,1);
                    alpha.setDuration(3000);
                    alpha.start();
                }
            });
            //点击事件
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItem.onItemLong(position);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {

       return dlist.getData().size();
    }

    @Override
    public int getItemViewType(int position) {
        //根据图片的多少决定使用那个类型
        int pic_amount = dlist.getData().get(position).getPic_amount();
        if (pic_amount>=3){
            return TE_THREE_IMG;
        }else {
            return TE_IMG;
        }

    }

    public void setOnLongClickListener(OnItem onItem) {
        this.onItem = onItem;
    }
}

MainActivity主体

public class MainActivity extends AppCompatActivity implements NewacFace {
    private static final String TAG = "MainActivityppppp";

    private RecyclerView rec_view;
    private NewPresenter newPresenter;
    String Wurl = "http://365jia.cn/news/api3/365jia/news/headline";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "创建====");
        rec_view = findViewById(R.id.rec_view);
        rec_view.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
        newPresenter = new NewPresenter(this);
        Log.d(TAG, "开始请求====");
        newPresenter.getnewdata(Wurl+"?page=1");
    }

    @Override
    public void newac(final NewBean newBean) {
        Log.d(TAG, "开始成功====");
        Log.d(TAG, "--------------- "+newBean);
        Log.d(TAG, "--------------- "+newBean.getData());
        final MyAdapter myAdapter = new MyAdapter(MainActivity.this,newBean.getData());
        rec_view.setAdapter(myAdapter);

        myAdapter.setOnLongClickListener(new OnItem() {
            @Override
            public void onItemLong(final int position) {

                final AlertDialog.Builder abuilder = new AlertDialog.Builder(MainActivity.this);
                abuilder.setTitle("删除选项");
                abuilder.setMessage("确定删除吗?");
                abuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        newBean.getData().getData().remove(position);
                        myAdapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                });

                abuilder.setNegativeButton("取消",null);

                abuilder.show();

            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值