商品展示的网站,seo引擎搜索网站,咨询公司排行榜,烟台市建设工程招标投标协会网站实现效果描述#xff1a; 1、点击recyclerview中item#xff0c;列表下方出现其他样式的item#xff0c;作为子item#xff0c;如下所示
所需要的java文件和xml文件有#xff1a;
1、创建FoldAdapteradapter, 在FoldAdapter中#xff0c;定义两种不同的类型#xff…实现效果描述 1、点击recyclerview中item列表下方出现其他样式的item作为子item如下所示
所需要的java文件和xml文件有
1、创建FoldAdapteradapter, 在FoldAdapter中定义两种不同的类型分别对应父item和子item对于不同布局的item需要设置两种不同的viewHoder进行设置。 2、在onCreateViewHolder进行对于布局的绑定 3、在onBindViewHoder中进行数据的操作 4、在getItemViewType返回对应的类型 5、在getItemCount中返回对应的大小
FoldAdapter
package com.example.expandtworecyclerviewdemo;import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;import org.w3c.dom.Text;import java.util.ArrayList;
import java.util.List;
import java.util.PropertyResourceBundle;
import java.util.zip.Inflater;public class FoldAdapteer extends RecyclerView.AdapterRecyclerView.ViewHolder {private static final int PARENT_TYPE 0X0000;private static final int CHILD_TYPE 0X0001;private Context mContext;private ListITyple dataList new ArrayList();public FoldAdapteer(Context context){mContext context;}public void setData(ListITyple iTyples){//对于list的数据不能简单的是等于的操作this.dataList.clear();this.dataList.addAll(iTyples);notifyDataSetChanged();}Overridepublic int getItemViewType(int position) {if (dataList.get(position).getTyple() EType.PARENT_TYPE) {return PARENT_TYPE;}if (dataList.get(position).getTyple() EType.CHILD_TYPE){return CHILD_TYPE;}return super.getItemViewType(position);}NonNullOverridepublic RecyclerView.ViewHolder onCreateViewHolder(NonNull ViewGroup parent, int viewType) {if (viewType PARENT_TYPE){View mView LayoutInflater.from(mContext).inflate(R.layout.parent_layout, parent, false);return new ParentHoder(mView);}else if (viewType CHILD_TYPE){View mView LayoutInflater.from(mContext).inflate(R.layout.child_layout, parent, false);return new ChildHoder(mView);}return null;}Overridepublic void onBindViewHolder(NonNull final RecyclerView.ViewHolder holder, final int position) {if (holder instanceof ParentHoder){ParentHoder parentHoder (ParentHoder)holder;final DateBean.ParentBean parentBean (DateBean.ParentBean) dataList.get(position);parentHoder.itemView.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View view) {Log.d(lucky, onClick: parentBean);if (parentBean.isExpand()){closeParent(position, parentBean);((ParentHoder) holder).mImage.setImageResource(R.drawable.icon_parent_down);}else {openParent(position, parentBean);((ParentHoder) holder).mImage.setImageResource(R.drawable.icon_parent_right);}parentBean.setExpand(!parentBean.isExpand());}});DateBean.ParentBean bindata (DateBean.ParentBean) dataList.get(position);((ParentHoder) holder).mImage.setImageResource(R.drawable.icon_parent_down);((ParentHoder) holder).text.setText(bindata.getTextParent());}else if (holder instanceof ChildHoder){DateBean.ChildBean childBean (DateBean.ChildBean)dataList.get(position);((ChildHoder) holder).text.setText(childBean.getTextChild());}}private void openParent(int position , DateBean.ParentBean parentBean) {Log.d(lucky, openParent: dataList.get(position));// DateBean.ParentBean parentBean (DateBean.ParentBean)dataList.get(position);ListDateBean.ChildBean child parentBean.getChildBeans();dataList.addAll(position1, child);// notifyDataSetChanged();notifyItemRangeInserted(position1, child.size());notifyItemRangeChanged(position1, child.size());
// // notifyItemChanged(position);}private void closeParent(int position, DateBean.ParentBean parentBean) {// (DateBean.ParentBean)dataList.get(position);ListDateBean.ChildBean child parentBean.getChildBeans();dataList.removeAll(child);notifyItemRangeRemoved(0,child.size());notifyDataSetChanged();// notifyItemRangeInserted(position1, child.size());// notifyItemRangeChanged(position1, child.size());}Overridepublic int getItemCount() {return dataList.size();}private static class ParentHoder extends RecyclerView.ViewHolder {private TextView text;private ImageView mImage;public ParentHoder(NonNull View itemView) {super(itemView);text itemView.findViewById(R.id.parent_id);mImage itemView.findViewById(R.id.paren_image);}}private static class ChildHoder extends RecyclerView.ViewHolder{private TextView text;public ChildHoder(NonNull View itemView) {super(itemView);text itemView.findViewById(R.id.child_id);}}
}
DataBean
package com.example.expandtworecyclerviewdemo;import android.widget.TextView;import java.util.List;public class DateBean {private ListParentBean parentBean;public ListParentBean getParentBean() {return parentBean;}public void setParentBean(ListParentBean parentBean) {this.parentBean parentBean;}public static class ParentBean implements ITyple {private ListChildBean childBeans;private boolean isExpand false;private String textParent;public String getTextParent() {return textParent;}public void setTextParent(String textParent) {this.textParent textParent;}public boolean isExpand() {return isExpand;}public void setExpand(boolean expand) {isExpand expand;}public ListChildBean getChildBeans() {return childBeans;}public void setChildBeans(ListChildBean childBeans) {this.childBeans childBeans;}Overridepublic EType getTyple() {return EType.PARENT_TYPE;}}public static class ChildBean implements ITyple{private String textChild;public String getTextChild() {return textChild;}public void setTextChild(String textChild) {this.textChild textChild;}Overridepublic EType getTyple() {return EType.CHILD_TYPE;}}
}接口的作用是为了使得ParentBean和ChildBean实现接口从而返回对应的类型执行相关的操作。 在写代码过程中有个点可能会比较疑问为什么在adapter中有泛型为IType的集合 因为这样的话根据不同的类型执行后获取的数据可通过强制装换获取存在的数据为身为子类的ParenBean和ChildBean。
接口IType
package com.example.expandtworecyclerviewdemo;public interface ITyple {EType getTyple();
}列举不同的数据枚举是默认从0开始计数。
枚举EType
package com.example.expandtworecyclerviewdemo;public enum EType {PARENT_TYPE,CHILD_TYPE
}MainActivity
package com.example.expandtworecyclerviewdemo;
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Adapter;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {ListITyple iTyples new ArrayList();Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//绑定对应的layout文件this.setContentView(R.layout.activity_main);RecyclerView mRecyclerView this.findViewById(R.id.recyclerview);//加载recycler中的布局缺少会报错RecyclerView.LayoutManager layoutManager new LinearLayoutManager(this);FoldAdapteer recyclerAdapter new FoldAdapteer(this);mRecyclerView.setLayoutManager(layoutManager);mRecyclerView.setAdapter(recyclerAdapter);//数据的输入for (int i 0; i 10 ; i){DateBean.ParentBean parentBean new DateBean.ParentBean();ListDateBean.ChildBean childBean new ArrayList();parentBean.setTextParent(i - parren );for (int j 0 ; j 10 ; j){DateBean.ChildBean childBean1 new DateBean.ChildBean();childBean1.setTextChild(j - child);childBean.add(childBean1);}parentBean.setChildBeans(childBean);iTyples.add(parentBean);}//数据传送到FolderAdapter中recyclerAdapter.setData(iTyples);}
}main_xml
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityandroidx.recyclerview.widget.RecyclerViewandroid:idid/recyclerviewandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintLeft_toLeftOfparentapp:layout_constraintRight_toRightOfparentapp:layout_constraintTop_toTopOfparent //androidx.constraintlayout.widget.ConstraintLayoutparent_layout ?xml version1.0 encodingutf-8?
LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalandroid:background#5FC0ECxmlns:androidhttp://schemas.android.com/apk/res/androidImageViewandroid:idid/paren_imageandroid:layout_width30dpandroid:layout_height12dpandroid:layout_gravitycenter/TextViewandroid:idid/parent_idandroid:layout_widthmatch_parentandroid:layout_height30dp/
/LinearLayout
child_layout
?xml version1.0 encodingutf-8?
TextView xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:idid/child_idandroid:layout_widthmatch_parentandroid:layout_height40dpandroid:background#BA9CF0
/TextView三级标题确实是一样的处理方式就是在childData中再加一个list放数据然后在adapter进行点击处理的效果。