怎么让recyclerview进入界面全部控件加载完成
发布网友
发布时间:2023-09-20 09:50
我来回答
共1个回答
热心网友
时间:2024-12-01 17:09
代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/expendContainer"
android:background="@color/colorAccent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/expendItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="展开"/>
<ToggleButton
android:gravity="center"
android:id="@+id/expendToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ViewStub
android:id="@+id/expendView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout="@layout/expend_view"/>
</LinearLayout>
上面是item布局的代码
主布局就只是RecyclerView。
然后ManActivity的代码很简单仅仅是加载RecyclerView而已。
下面是adapter的代码:
全选复制放进笔记public class ExpendRecyclerAdapter extends RecyclerView.Adapter<ExpendViewHolder> {
public Context mContext;
private View view;
public ExpendRecyclerAdapter(Context context) {
mContext = context;
}
@Override
public ExpendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view = LayoutInflater.from(mContext).inflate(R.layout.expend_item,parent,false);
ExpendViewHolder expendViewHolder = new ExpendViewHolder(view);
return expendViewHolder;
}
@Override
public void onBindViewHolder(final ExpendViewHolder holder, final int position) {
holder.expendItem.setText("需要展示的数据");
holder.expendToggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.expendToggleButton.isChecked()){
holder.expendView.setVisibility(View.VISIBLE);
}else {
holder.expendView.setVisibility(View.INVISIBLE);
holder.expendContainer.removeView(holder.expendView);
notifyItemChanged(position);
}
}
});
}
@Override
public int getItemCount() {
return 1;
}
}