欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Recycleview

程序员文章站 2022-04-06 16:44:53
...

#

Recycleview
##Recycleview的使用:
###1.配置build.gradle
dependencies {
compile fileTree(include: ‘*.jar’, dir: ‘libs’)
compile ‘com.android.support:appcompat-v7:26.1.0’
compile ‘com.android.support:recyclerview-v7:26.1.0’
}
###2.布局的编写
####2.1主布局
#####用于显示Recycleview里面的数据
<?xml version="1.0" encoding="utf-8"?>

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

</RelativeLayout>

####2.2子布局
#####用于显示每个条目的数据
<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/tv_item"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:text="moon" />
</FrameLayout>

###3适配器(Adapter)的编写
####Adapte最大的改进就是对ViewHolder进行了封装定义,我们只需定义一个ViewHolder继承RecycleView.ViewHolder就可以了。另外,Adapter继承了RecycleView.Adapter,在onCreateViewHolder中加载条目布局,在onBindViewHolder中将视图与数据绑定,在getItemCount()中返回Recycleview的条目的长度,在MyViewHolder中获取自布局控件的id,在adapter中提供了OnItemClickListener()接口里面封装了onItemClick()和onItemLongClick()两个方法分别是对点击和长按的监听还有removeData()方法用于删除Recycleview里面的条目

class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> implements
    View.OnClickListener, View.OnLongClickListener {
private List<String> mList;
private Context mContext;
private OnItemClickListener mOnItemClickListener;

public HomeAdapter(Context mContext, List<String> mList) {
    this.mContext = mContext;
    this.mList = mList;
}


public interface OnItemClickListener {
    void onItemClick(View view, int position);

    void onItemLongClick(View view, int position);

}

public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
    this.mOnItemClickListener = mOnItemClickListener;
}

public void removeData(int position) {
    mList.remove(position);
    notifyItemRemoved(position);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(
            mContext).inflate(R.layout.item_recycler, parent,
            false);
    MyViewHolder holder = new MyViewHolder(view);
    view.setOnClickListener(this);
    view.setOnLongClickListener(this);
    return holder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.itemView.setTag(position);
    holder.tv.setText(mList.get(position));
}

@Override
public void onClick(View view) {
    if (mOnItemClickListener != null) {
        mOnItemClickListener.onItemClick(view, (int) view.getTag());
    }
}

@Override
public boolean onLongClick(View view) {
    if (mOnItemClickListener != null) {
        mOnItemClickListener.onItemLongClick(view, (int) view.getTag());
    }
    return true;
}

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

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView tv;

    public MyViewHolder(View view) {
        super(view);
        tv = view.findViewById(R.id.tv_item);
    }
}
}

###4主界面的编写
####在主界面中定义一个泛型list通过initData方法将数据放入list中通过 mHomeAdaper = new HomeAdapter(this,mList);将list传入Homeadapter对list里面的数据进行绑定和填充,mRecyclerView.setLayoutManager(new LinearLayoutManager(this))是设置布局管理器,mRecyclerView.setItemAnimator(new DefaultItemAnimator());设置删除和增加时的动画最后mRecyclerView.setAdapter(mHomeAdaper)是设置adapter,setlister()里面分别写了长按和点击的效果
public class RecyclerViewActivity extends AppCompatActivity {
private List mList;
private HomeAdapter mHomeAdaper;
private RecyclerView mRecyclerView;
private StaggeredHomeAdapter mStaggeredHomeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
initData();
mRecyclerView = findViewById(R.id.id_recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mHomeAdaper = new HomeAdapter(this,mList);
mRecyclerView.setAdapter(mHomeAdaper);
setLister();

}

private void initData() {
    mList=new ArrayList<>();
    for (int i = 1; i < 20; i++)
    {
        mList.add(i+"");
    }
}
private void setLister(){
    mHomeAdaper.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(RecyclerViewActivity.this, "点击第" + (position + 1) + "条", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onItemLongClick(View view, final int position) {
            new AlertDialog.Builder(RecyclerViewActivity.this)
                    .setTitle("确认删除吗?")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            mHomeAdaper.removeData(position);
                        }
                    })
                    .show();
        }
    });
}
}
相关标签: Recycleview的使用