张萌&韩墨羽——Recyclerview

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Zephyr_0312/article/details/98885983

Recyclerview

Recyclerview
RecyclerView比ListView的优势:
1.RecyclerView比listview实现效果更多
2.RecycelrView支持多布局;
3.RecyclerView根据项目需要插拔功能

二.重要的方法

1.RecyclerView横向滑动:
LinearLayoutManager.HORIZONTAL横向滑动LinearLayoutManager.VERTICAL垂直滑动

2.RecyclerView.Adapter中刷新方法区别:
notifyDataSetChanged();整体刷新+没有动画效果
notifyItemInserted(int position,Object data):有动画效果+添加一条数据在position位置
notifyItemRemoved(position);有动画效果+删除一条数据并刷新
3.RecyclerView多布局展示:
public int getItemViewType(int position)返回当前数据的itemview类型
4.RecyclerView常见方法:
LinearLayoutManager:recyclerview线性管理器(垂直水平方向);
GridLayoutManager:网格布局管理器;
StaggeredGridLayoutManager:瀑布流布局管理器;
RecyclerView.setLayoutManager(LayoutManager manager):添加布局管理器
RecyclerView.addItemDecoration(ItemDecoration decoration):添加分割线
RecyclerView.setItemAnimator(ItemAnimator animator):添加动画方法

package com.example.jxxapplication.util7;

import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.widget.LinearLayout;

import com.example.jxxapplication.R;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {
private RecyclerView recyclerviewId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

    recyclerviewId = findViewById(R.id.recyclerview_id);

    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setOrientation(LinearLayout.VERTICAL);//列表

    GridLayoutManager layoutManager = new GridLayoutManager(this, 2);

    StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

    recyclerviewId.setLayoutManager(layoutManager);

    ArrayList<Integer> integers = new ArrayList<>();
    integers.add(R.drawable.weitwo);
    integers.add(R.drawable.weitwo);
    integers.add(R.drawable.weitwo);
    integers.add(R.drawable.weitwo);
    integers.add(R.drawable.weitwo);
    integers.add(R.drawable.weitwo);
    integers.add(R.drawable.weitwo);
    integers.add(R.drawable.weitwo);

    MyAdpter adpter = new MyAdpter(integers, this);
    recyclerviewId.setAdapter(adpter);

    DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();
    defaultItemAnimator.setAddDuration(2000);
    defaultItemAnimator.setRemoveDuration(2000);
    recyclerviewId.setItemAnimator(defaultItemAnimator);//添加删除和添加的动画

    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);

// dividerItemDecoration.setDrawable(new ColorDrawable(234234));
recyclerviewId.addItemDecoration(dividerItemDecoration);
}
}

适配器代码

package com.example.jxxapplication.util7;

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

import com.example.jxxapplication.R;

import java.util.ArrayList;

public class MyAdpter extends RecyclerView.Adapter<MyAdpter.MyViewHolder> {
ArrayList strings;
Context context;

public MyAdpter(ArrayList<Integer> strings, Context context) {
    this.strings = strings;
    this.context = context;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(context).inflate(R.layout.rejxx, null);
    MyViewHolder viewHolder = new MyViewHolder(itemView);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder,final int i) {
    ViewGroup.LayoutParams params = myViewHolder.imageView.getLayoutParams();
    params.height = (int) (Math.random()*801)+100;
    myViewHolder.imageView.setLayoutParams(params);
    myViewHolder.imageView.setImageResource(strings.get(i));
    myViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            strings.remove(i);
            notifyItemRemoved(i);//更新
            notifyItemChanged(i,strings.size());
        }
    });
    myViewHolder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            strings.add(R.mipmap.ic_launcher);
            notifyItemInserted(i);
            return true;
        }
    });
}

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

class MyViewHolder extends RecyclerView.ViewHolder{
    ImageView imageView;
     MyViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.jxximage_id);
    }
}

}
展开阅读全文

没有更多推荐了,返回首页