RecycleView实现查看全部,折叠效果
程序员文章站
2022-03-04 19:45:34
...
首先先看图示效果
这个是收起的一个效果
这是展示全部的一个效果
代码实现
第一步
题目已经指出,咱用的是recycle 所以就要创建一个recycleview
recyc.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
有了recycleview之后,咱得自定义一个item
pay_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<com.yichen.xuezhifan.util.XCRoundImageView
android:id="@+id/concern_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:scaleType="fitCenter"
app:type="circle"
android:src="@mipmap/xiaofan_headline"
/>
<TextView
android:id="@+id/tea_name"
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:textSize="10dp"-->
<!--android:padding="5dp"-->
<!--android:text="数学老师"-->
<!--/>-->
</LinearLayout>
我一般把这种列表开发称为‘三件套’开发:recycleview(listview)、item、适配器
接下来就是适配器,注意前方高能 因为本人手懒所以一些view都没换, 如果有幸被看到这篇文章而有些地方有些晦涩难懂的话,可以留言评论
public class PayMyTeaAdapter2 extends RecyclerView.Adapter<PayMyTeaAdapter2.MyViewHolder> {
private Context context;
private List<MyTeacherModel> list;
//判断是footview还是正常的item
private static final int NORMAL_VIEW = 0;
private static final int FOOT_VIEW = 1;
//这个是作为以后扩展留的
private int view = 0;
//判断是否显示全部
private int status = 0;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public void setNewDate(List<MyTeacherModel> list) {
//为了延续开发
this.list = list == null ? new ArrayList<MyTeacherModel>() : list;
notifyDataSetChanged();
}
private OnItemClickListener onItemClickListener = null;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public PayMyTeaAdapter2(Context context, List<MyTeacherModel> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(context).inflate(R.layout.my_pay_user, viewGroup, false);
return new MyViewHolder(itemView, i);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
if (getItemViewType(i) == NORMAL_VIEW) {
initView(myViewHolder, i);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(v, i, view);
}
}
});
} else {
if (status == 0) {
changeStaus(myViewHolder);
} else {
resumeStatus(myViewHolder);
}
}
}
public void changeStaus(MyViewHolder myViewHolder) {
myViewHolder.textView.setText("查看全部");
GlideApp.with(context).load(R.mipmap.open).into(myViewHolder.imageView);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
onItemClickListener.onItemClickAll();
}
}
});
}
public void resumeStatus(MyViewHolder myViewHolder) {
myViewHolder.textView.setText("收起");
GlideApp.with(context).load(R.mipmap.pack).into(myViewHolder.imageView);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
onItemClickListener.onItemPick();
}
}
});
}
public void initView(MyViewHolder myViewHolder, int i) {
if (list.get(i) != null) {
if (list.get(i).getYc_avatar() != null) {
GlideApp.with(context).load(list.get(i).getYc_avatar()).into(myViewHolder.imageView);
} else {
GlideApp.with(context).load(R.mipmap.xiaofan_headline).into(myViewHolder.imageView);
}
if (list.get(i).getYc_video_name().equals("2")) {
myViewHolder.textView.setText(list.get(i).getYc_nickname());
} else {
myViewHolder.textView.setText(list.get(i).getYc_username());
}
}
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1 && list.size() > 7) {
return FOOT_VIEW;
}
return NORMAL_VIEW;
}
//返回item数量
@Override
public int getItemCount() {
if (status == 0) {
if (list.size() > 7) {
return 7;
} else {
return list.size();
}
} else {
return list.size() + 1;
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public MyViewHolder(@NonNull View itemView, int v) {
super(itemView);
textView = itemView.findViewById(R.id.tea_name);
imageView = itemView.findViewById(R.id.concern_img);
}
}
//回调接口
public interface OnItemClickListener {
void onItemClick(View v, int position, int view);
void onItemClickAll();
void onItemPick();
}
}
好了以上就是三件套的内容至于在activity中怎么实现,我只给关键代码咯因为接下来巨简单
recyclerView2.setAdapter(adapter3);
adapter3.setOnItemClickListener(new PayMyTeaAdapter2.OnItemClickListener() {
@Override
public void onItemClick(View v, int position, int view) {
}
@Override
public void onItemClickAll() {
//这是展示回调
adapter3.setStatus(1);
adapter3.setNewDate(myTeacherModel);
}
@Override
public void onItemPick() {
//这是收起回调
adapter3.setStatus(0);
adapter3.setNewDate(myTeacherModel);
}
});
以上就是文章开头效果图的代码实现~
下一篇: xctf部分题目-2