RecyclerVIew realizes suspended ceiling effect diagram
Write a picture description here
Main page layout
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent"> <. android: android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android: android:layout_width="match_parent" android:layout_height="50dp" android:background="#EFFAE7" android:gravity="center" android:text="Ceiling Text 1" /> <!--<include layout="@layout/layout_sticky_header_view"/>--> </FrameLayout>
RecyclerView sub-entry layout
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android: android:layout_width="match_parent" android:layout_height="30dp"> <TextView android: android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android: android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#ffffff"/> </RelativeLayout> <TextView android: android:layout_width="match_parent" android:layout_height="50dp" android:background="#EFFAE7" android:gravity="center" android:text="Ceiling Text 1" /> </FrameLayout>
activity code
public class MainActivity extends AppCompatActivity { private TextView tvStickyHeaderView; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); initView(); initListener(); } /** * Initialize View */ private void initView() { recyclerView = (RecyclerView) findViewById(); tvStickyHeaderView = (TextView) findViewById(.tv_sticky_header_view); (new LinearLayoutManager(this)); (new StickyExampleAdapter(this, getData())); } /** * Initialize Listener */ private void initListener() { (new () { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { (recyclerView, dx, dy); View stickview = (0, 0); if (stickview != null && () != null) { if (!((), ())) { (()); } } View transInfoView = ( 0, () + 1); if (() != null) { int transViewStatus = (int) (); int top = (); if (transViewStatus == StickyExampleAdapter.HAS_STICKY_VIEW) { if (top > 0) { int dealtY = top - (); (dealtY); } else { (0); } } else if (transViewStatus == StickyExampleAdapter.NONE_STICKY_VIEW) { (0); } } } }); } public List<StickyBean> getData() { List<StickyBean> stickyExampleModels = new ArrayList<>(); for (int index = 0; index < 100; index++) { if (index < 15) { (new StickyBean( "Ceiling Text 1", "name" + index, "gender" + index)); } else if (index < 25) { (new StickyBean( "Ceiling Text 2", "name" + index, "gender" + index)); } else if (index < 35) { (new StickyBean( "Ceiling Text 3", "name" + index, "gender" + index)); } else { (new StickyBean( "Ceiling Text 4", "name" + index, "gender" + index)); } } return stickyExampleModels; } }
adapter code
public class StickyExampleAdapter extends <> { //The first top private static final int FIRST_STICKY_VIEW = 1; //Other top suction static final int HAS_STICKY_VIEW = 2; //Normal View static final int NONE_STICKY_VIEW = 3; private final LayoutInflater mInflate; private final List<StickyBean> datas; StickyExampleAdapter(Context context, List<StickyBean> datas){ mInflate = (context); = datas; } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = (.item_ui, parent, false); return new RecyclerViewHolder(inflate); } @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { StickyBean stickyBean = (position); (); (); if (position == 0) { (); (); (FIRST_STICKY_VIEW); } else { if (!(, (position - 1).sticky)) { (); (); (HAS_STICKY_VIEW); } else { (); (NONE_STICKY_VIEW); } } //Set ContentDescription here, as a content description, you can take it out through getContentDescription, and the effect is similar to setTag. (); } @Override public int getItemCount() { return datas == null ? 0 : (); } public class RecyclerViewHolder extends { TextView tvStickyHeader; RelativeLayout rlContentWrapper; TextView tvName; TextView tvGender; RecyclerViewHolder(View itemView) { super(itemView); tvStickyHeader = (TextView) (.tv_sticky_header_view); rlContentWrapper = (RelativeLayout) (.rl_content_wrapper); tvName = (TextView) (); tvGender = (TextView) (); } } }
StickyBean Code
public class StickyBean { public String name; public String autor; public String sticky; public StickyBean(String sticky, String name, String autor) { = sticky; = name; = autor; } }
The build file of the app
apply plugin: '' android { compileSdkVersion 26 buildToolsVersion "25.0.3" defaultConfig { applicationId "" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(''), '' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile(':espresso-core:2.2.2', { exclude group: '', module: 'support-annotations' }) compile ':appcompat-v7:26.0.0-alpha1' compile ':recyclerview-v7:23.1.0' testCompile 'junit:junit:4.12' }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.