From GitHub, Address:
Https://github.com/worldline/FoldableLayout
:
Use (in conjunction with Recycleview as an example):
First on each layout file:
Main Page Creation Recycleview
<android. Support. V7. Widgets. RecyclerviewAndroid:id="@+id/rv_shouyefragment"Android:layout_width="Match_parent"android:layout_height="Wrap_content"android:clipchildren="false"android:cliptopadding="false"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"> </android. Support. V7. Widgets. Recyclerview>
List_item_cover:
<?xml version= "1.0" encoding= "Utf-8"?><android.support.v7.widget.CardViewxmlns:android="http://schemas.android.com/apk/ Res/android "android:layout_width="match_parent "android:layout_height=" 100DP "> <relativelayoutandroid:layout_width="Match_parent"android:layout_ Height="Match_parent"> <textview android:textSt YLE = "bold" android:textcolor = "@color/colorprimary" android:gravity = "center" android:id = "@+id/tv_c Over " android:layout_width =" 100DP " android:layout_height = "match_parent" />< /span> <imageview android:layou T_torightof = "@+id/tv_cover" android:scalety PE = "Centercrop" android:id = "@+id/iv_cover" android:layout_width =" match_parent " android:layout_height =" match_parent "/> <button android:id= "@+id/btn_cover" android:layout_alignparentright = android:layout_alignparentbottom= "true" android:text =" Share " android:textcolor =" @android: Color/white " android:backgroundtint =" @color /coloraccent " android:layout_width =" Wrap_co Ntent " android:layout_height =" wrap_content " /> </relativelayout></android.support.v7.widget.CardView>
List_item_detail:
<?xml version= "1.0" encoding= "Utf-8"?><android.support.v7.widget.CardViewxmlns:android="http://schemas.android.com/apk/ Res/android "android:layout_width=" Match_parent "android:layout_height= "200DP"> <relativelayoutandroid:layout_width="Match_parent"android:layout_height ="Match_parent"android:background="@android: Color/holo_green_dark"> <ImageViewandroid:id="@+id/iv_detail"android:layout_width= "Match_parent" android:layout_height="Match_parent"android:scaletype="Centercrop" /> </relativelayout></android.support.v7.widget.CardView>
1-Create Foldablelayout
new FoldableLayout(context);
2. Set the cover page, details
foldableLayout.setupViews(R.layout.list_item_cover, R.layout.list_item_detail, R.dimen.card_cover_height, itemView.getContext());
The first parameter refers to the collapse chart layout, the second is the expansion diagram layout, the third is the height of the folding, the height must be the same as the height of the folding state.
3-Set custom adapter for Recycleview and add Foldablelayout to custom adapter to control the effect of each list item
/ * Custom shouyefragmentadapter*/ Public classShouyefragmentadapter extends Recyclerview.adapter<shouyefragmentadapter.myviewholder> {PrivateString[] mdatalist;//text array Private int[] mimagelist;//Picture to display PrivateMap<integer, boolean> mfoldstates =NewHashmap<> ();//used to determine whether the state is collapsed PrivateContext Mcontext; Public Shouyefragmentadapter(string[] Mdatalist,int[] mimagelist, Context mcontext) { This. mdatalist = mdatalist; This. Mcontext = Mcontext; This. mimagelist = mimagelist; } @Override PublicShouyefragmentadapter.myviewholderOncreateviewholder(ViewGroup parent,intViewType) {return NewMyviewholder (NewFoldablelayout (Parent.getcontext ())); } @Override Public void Onbindviewholder(Final Shouyefragmentadapter.myviewholder Holder,intPosition) {//Bind Data Holder.iv_cover.setImageResource (Mimagelist[position]);Holder.iv_detail.setImageResource (Mimagelist[position]); Holder.tv_cover.setText (Mdatalist[position]);//Bind status if(Mfoldstates.containskey (position)) {if(Mfoldstates.Get(position) = = Boolean.true) {if(!holder.foldablelayout.isfolded ()) {//If it is not collapsed, fold itHolder.foldableLayout.foldWithoutAnimation (); } }Else if(Mfoldstates.Get(position) = = Boolean.false) {//If it is collapsed, expand it if(Holder.foldableLayout.isFolded ()) {holder.foldableLayout.unfoldWithoutAnimation (); } } }Else{holder.foldableLayout.foldWithoutAnimation (); }//Set internal button listenerHolder.btn_cover.setOnClickListener (NewView.onclicklistener () {@Override Public void OnClick(View v) {Toast.maketext (Mcontext,"Btn_cover Clicked", Toast.length_short). Show (); } });//Set the expand and collapse with animation effect when clickedHolder.foldableLayout.setOnClickListener (NewView.onclicklistener () {@Override Public void OnClick(View v) {if(Holder.foldableLayout.isFolded ()) {holder.foldableLayout.unfoldWithAnimation (); }Else{holder.foldableLayout.foldWithAnimation (); } } }); Holder.foldablelayout. Setfoldlistener (NewFoldablelayout.foldlistener () {@Override Public void Onunfoldstart() {if(Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {holder.foldableLayout.setElevation (5); }} @Override Public void Onunfoldend() {if(Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {holder.foldableLayout.setElevation (0); } mfoldstates.put (Holder.getadapterposition (),false); } @Override Public void Onfoldstart() {if(Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {holder.foldableLayout.setElevation (5); }} @Override Public void Onfoldend() {if(Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {holder.foldableLayout.setElevation (0); } mfoldstates.put (Holder.getadapterposition (),true); } }); } @Override Public int GetItemCount() {returnMdatalist.length; } Public classMyviewholder extends Recyclerview.viewholder {protectedFoldablelayout foldablelayout;protectedTextView Tv_cover;protectedImageView Iv_cover;protectedImageView Iv_detail;protectedButton Btn_cover; Public Myviewholder(Foldablelayout Foldablelayoutview) {super (Foldablelayoutview); Foldablelayout = Foldablelayoutview; Foldablelayout.setupviews (R.layout.list_item_cover, R.layout.list_item_details, R.dimen.card_cover_heig HT, Itemview.getcontext ()); Tv_cover = (TextView) foldablelayoutview. Findviewbyid (R.id.tv_cover); Btn_cover = (Button) foldablelayoutview. Findviewbyid (R.id.btn_cover); Iv_cover = (ImageView) foldablelayoutview. Findviewbyid (R.id.iv_cover); Iv_detail = (ImageView) foldablelayoutview. Findviewbyid (R.id.iv_detail); } }}
4-Use in interface (add adapter to Recycleview)
Private void Initrecycleview() {rv_shouyefragment = (recyclerview) view. Findviewbyid (r.id.rv_shouyefragment); Rv_shouyefragment. Setlayoutmanager (NewLinearlayoutmanager (GetContext ()));//canteenname,canteenimage, is the data to be displayed in the list, pictureMadapter =NewShouyefragmentadapter (Canteenname,canteenimage,getcontext ());//Add gaps between sub-list itemsRv_shouyefragment.additemdecoration (NewRecyclerview.itemdecoration () {@Override Public void getitemoffsets(Rect outrect, view view, Recyclerview parent, recyclerview.state State) {Super. Getitemoffsets (Outrect, view, parent, state); Outrect.bottom = Getresources (). Getdimensionpixelsize (R.dimen.activity_vertical_margin); } }); Rv_shouyefragment.setadapter (Madapter); }
5-Problem found:
快速点击第一项或最后一项在列表项从展开状态到折叠状态时会导致程序崩溃,但Logcat未报错。图片加载缓慢,写的时候没考虑内存泄漏等问题导致程序在这个界面卡顿不流畅,动画无法正常显示等问题。
Github--foldablelayout Collapsible Component Learning