1:布局文件使用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"
android:background="@color/main_bg"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/region_recy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4" />
<!-- 阴影控件设置半透明-->
<TextView
android:id="@+id/txt_yinying"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:background="#b3ffffff" />
</FrameLayout>
<!-- //展开更多,点击收起-->
<RelativeLayout
android:id="@+id/rl_openall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<TextView
android:id="@+id/txt_open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_bg"
android:gravity="center"
android:text="展开更多"
android:textColor="@color/txtblue"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
2:代码中设置全部显示数据和收起显示数据
//regionBeans接口list集合数据
private List<String> mList = new ArrayList<>();
private List<String> HideList = new ArrayList<>();
for (int i = 0; i < regionBeans.size(); i++) {
mList.add(regionBeans.get(i).getName());
}
//收起显示的数据仅显示16条
for (int i = 0; i < 16; i++) {
HideList.add(mList.get(i));
}
//适配器
mRegionAdapter = new RegionAdapter(KeywordActivity.this);
regionRecy.setAdapter(mRegionAdapter);
//默认设置收起时的数据
mRegionAdapter.setHideList(HideList);
RegionAdapter适配器中
public class RegionAdapter extends RecyclerView.Adapter<RegionAdapter.ViewHolder> {
private Activity activity;
private List<String> list;
public RegionAdapter(Activity activity) {
this.activity = activity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(activity).inflate(R.layout.rmsf_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.mRmsfname.setText(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
//显示收起时数据
public void setHideList(List<String> newList) {
this.list = newList;
notifyDataSetChanged();
}
//显示全部数据
public void setOpenList(List<String> openList) {
this.list = openList;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView mRmsfname;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mRmsfname = itemView.findViewById(R.id.rmsf_name);
}
}
}
3:点击收起展开按钮执行适配器的两个方法
case R.id.rl_openall:
if (txtOpen.getText().toString().equals("展开更多")) {
txtOpen.setText("点击收起");
txtYinying.setVisibility(View.GONE);
mRegionAdapter.setOpenList(mList);
} else {
txtOpen.setText("展开更多");
txtYinying.setVisibility(View.VISIBLE);
mRegionAdapter.setHideList(HideList);
}
break;