文章目录
上接博客: https://blog.csdn.net/wang342626/article/details/85796699
十一.RecycleView
Build.gradle中添加, 后面的数字可以相应的修改
implementation 'com.android.support:recyclerview-v7:23.2.1'
导入包,不要导错了
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
RecycleView最简洁的写法
- 建立一个xml
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGrayDark">
</android.support.v7.widget.RecyclerView>
- 再建立一个xml, 名叫 layout_linear_item.xml,规定每一项的样式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#000000"
android:text="Hello"
android:textSize="20sp"/>
</LinearLayout>
package com.taifua.androidlearning.recyclerview;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.taifua.androidlearning.R;
public class LinearRecyclerViewActivity extends AppCompatActivity
{
private RecyclerView mRvMain;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_recycle_view);
mRvMain = findViewById(R.id.rv_main);
mRvMain.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this));
mRvMain.addItemDecoration(new MyDecoration());
mRvMain.setAdapter(new LinearAdapter(LinearRecyclerViewActivity.this));
}
// 装饰item的内部类
class MyDecoration extends RecyclerView.ItemDecoration
{
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state)
{
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, 0, getResources().getDimensionPixelOffset(R.dimen.dividerHeight));
}
}
}
- 新建一个java 名叫 LinearAdapter.java 继承RecyclerView.Adapter
E需要修改为下面的内部类对象
package com.example.xuanke;
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 android.widget.TextView;
public class LinearAdapter extends RecyclerView.Adapter<XKinfoRvAdapter.RvViewHolder> {
private Context mContext;
public XKinfoRvAdapter(Context context){
this.mContext = context;
}
// 注入每一个item的xml
@Override
public RvViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
return new RvViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_item_xk, viewGroup, false));
}
@Override
public void onBindViewHolder(RvViewHolder holder, final int position) {
//修改UI
((RvViewHolder)holder).tv_courseId.setText("Haha");
// 添加每一项的点击事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtil.showMsg(mContext, "你点击了第"+position+"项");
}
});
// 添加每一项的长按事件
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true; //返回true后,就不会再调用上面的点击事件了
}
});
}
@Override
public int getItemCount() {
return 10;
}
class RvViewHolder extends RecyclerView.ViewHolder{
private TextView tv_courseId;
public RvViewHolder(@NonNull View itemView)
{
super(itemView);
tv_courseId = itemView.findViewById(R.id.tv_title);
System.out.println("标题是:");
}
}
}
外接点击事件写法
- 再新建一个Activity,名叫LinearRecycleViewActivity,系统会自动完成3个任务,
编辑LinearRecycleViewActivity.java
package com.example.qing.myapplication2.recycleview;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.example.qing.myapplication2.R;
import com.example.qing.myapplication2.listViewActivity.ListViewActivity;
public class LinearRecycleViewActivity extends AppCompatActivity {
private RecyclerView mRvMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_recycle_view);
mRvMain = (RecyclerView) findViewById(R.id.rv_main);
mRvMain.setLayoutManager(new LinearLayoutManager(LinearRecycleViewActivity.this));
//添加分割线
mRvMain.addItemDecoration(new MyDecoration());
//添加监听器
mRvMain.setAdapter(new LinearAdapter(LinearRecycleViewActivity.this, new LinearAdapter.OnItemClickListener() {
//通过回调方法实现了点击事件放在外面
@Override
public void onClick(int pos) {
Toast.makeText(LinearRecycleViewActivity.this,"在外面LinearRecycleViewActivity.java处理点击事件",Toast.LENGTH_SHORT).show();
}
}));
}
//为了给每一项下面添加一条分割线
class MyDecoration extends RecyclerView.ItemDecoration{
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect,view,parent,state);
//底部添加一条分割线,需要引用values下面的dimens.xml中的dividerHight
//左 上 右 下
outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHight));
}
//另外还可以重写onDraw方法,在加载每一项之前添加装饰
//另外还可以重写onDrawOver方法,在加载每一项之后添加装饰
}
}
- 新建一个LinearAdapter.java
package com.example.qing.myapplication2.recycleview;
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.TextView;
import android.widget.Toast;
import com.example.qing.myapplication2.R;
import static android.R.attr.id;
public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {
private Context mContext;
private OnItemClickListener mListener;
public LinearAdapter(Context context,OnItemClickListener listener){
this.mContext = context;
this.mListener = listener;
}
@Override
public LinearAdapter.LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,parent,false));
}
@Override
public void onBindViewHolder(LinearViewHolder holder, final int position) {
holder.textView.setText("Hello World");
//为每一项添加点击事件
holder.itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(mContext,"在里面LinearAdapter.java处理点击事件"+position+"项",Toast.LENGTH_SHORT).show();
//调用自定义的接口
mListener.onClick(position);
}
});
}
@Override
public int getItemCount() {
return 30;
}
//定义一个holder
class LinearViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public LinearViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.tv_title);
}
}
//定义一个接口
public interface OnItemClickListener{
void onClick(int pos);
}
}
- 使用代码
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGrayDark">
</android.support.v7.widget.RecyclerView>
- layout下面新建一个layout_linear_item.xml, 这里设定每个子项目的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#000"
android:textSize="20sp"
android:gravity="center"
android:text="000"
/>
</LinearLayout>
- values下面新建dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dividerHight">1dp</dimen>
</resources>
- 效果图
RecycleView实现水平方向滚动
- 编辑 activity_hor_recycle_view.xml, 添加一个id为rv_hor的RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.qing.myapplication2.recycleview.HorRecycleViewActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_hor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
- layout下面新建一个layout_hor_item.xml, 这是每一个item项,编写内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorGrayDark"
>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textColor="#000000"
android:text="0"
android:textSize="20sp"
/>
</LinearLayout>
- 编辑HorRecycleViewActivity.java:
先获取到id为rv_hor的RecyclerView
然后设置布局管理器,mRvHor.setLayoutManager
装饰线可加可不加, mRvHor.addItemDecoration,装饰线定义为一个内部类
再添加监听器,mRvHor.setAdapter, 待会儿还要新建一个监听器类
package com.example.qing.myapplication2.recycleview;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.example.qing.myapplication2.R;
public class HorRecycleViewActivity extends AppCompatActivity {
private RecyclerView mRvHor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hor_recycle_view);
mRvHor = (RecyclerView) findViewById(R.id.rv_hor);
//设置布局为水平方向的,和垂直方向的唯一不同点
LinearLayoutManager linearLayoutManager = new LinearLayoutManager