CoordinatorLayout +RecyclerView+加载不同布局的item

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 05:58   50   0

最近在做一个仿海词词典的demo,首页主要用到了CoordinatorLayout 折叠标题,并加载下方不同布局的数据。海词词典的的首页是非常漂亮的,先让我们看下它的首页吧。直接上图片


这里我们可以看到,首页向上滑动的过程中,只有上部分滑出了屏幕,而搜索框并没有,而是滑到了屏幕的上方看上去非常漂亮
在下边的listview中,装载了不同布局的item(可以看到第一部分和第二部分的布局是不同的),那这是怎么实现的呢?具体我不清楚人家是用的什么方法,但是我使用了CoordinatorLayout 的折叠标题功能实现了上滑折叠功能,然后下边是用了RecyclerView嵌套了不同布局的item,刚开始做的时候我没有使用RecyclerView而是直接用的ListView嵌套不同布局的item,但是做成后没有折叠效果,查了网上的资料后发现在RecyclerView中的layout_behavior是对折叠效果支持的,具体用listview怎么实现,能否实现?小弟bu知,望大神指点!
下边看下我做的demo吧



1、CoordinatorLayout
先看下我的整个布局文件activity_mainsearch.xml
[java] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:fitsSystemWindows="true">
  8. <android.support.design.widget.AppBarLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:fitsSystemWindows="true"
  12. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  13. <android.support.design.widget.CollapsingToolbarLayout
  14. android:id="@+id/collapse_toolbar"
  15. android:layout_width="match_parent"
  16. android:layout_height="180dp"
  17. android:fitsSystemWindows="true"
  18. app:contentScrim="?attr/colorPrimary"
  19. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  20. <ImageView
  21. android:id="@+id/header"
  22. android:layout_width="match_parent"
  23. android:layout_height="match_parent"
  24. android:background="@drawable/photo"
  25. android:fitsSystemWindows="true"
  26. android:scaleType="centerCrop"
  27. app:layout_collapseMode="parallax" />
  28. <android.support.v7.widget.Toolbar
  29. android:id="@+id/toolbar"
  30. android:layout_width="match_parent"
  31. android:layout_height="40dp"
  32. android:gravity="top"
  33. android:minHeight="?attr/actionBarSize"
  34. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  35. app:titleMarginTop="0dp" >
  36. <TextView
  37. android:id="@+id/toolbar_title"
  38. android:text="海工词典"
  39. style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_gravity="center"/>
  43. </android.support.v7.widget.Toolbar>
  44. <EditText
  45. android:id="@+id/et_search"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:layout_gravity="bottom"
  49. app:tabIndicatorColor="@color/colorAccent"
  50. android:padding="5dip"
  51. android:layout_marginLeft="10dp"
  52. android:layout_marginRight="10dp"
  53. android:layout_marginBottom="5dp"
  54. android:background="@drawable/bg_edittext"
  55. android:textColorHint="#AAAAAA"
  56. android:textSize="15dip"
  57. android:singleLine="true"
  58. android:hint="请输入要查询的单词..."/>
  59. </android.support.design.widget.CollapsingToolbarLayout>
  60. </android.support.design.widget.AppBarLayout>
  61. <android.support.v7.widget.RecyclerView
  62. android:id="@+id/rv_mainsearch"
  63. android:layout_width="match_parent"
  64. android:layout_height="match_parent"
  65. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  66. </android.support.design.widget.CoordinatorLayout>

CoordinatorLayout 我在这里嵌套了:AppBarLayout 、CollapsingToolbarLayout 和 RecyclerView,其中CollapsingToolbarLayout 中又包含了上部显示的图片ImageView 、标题Toolbar 和 搜索框EditText,在Toolbar 中可以设置app:layout_collapseMode="pin"属性来决定当上滑的时候标题是否隐藏,这里添加一点当我们加载出Toolbar后会发现上边有个向左的按钮,如果我们不想要的话可以在Acticity中设置 ,(代码片段,完整代码请稍候...)
[java] view plain copy
  1. private void setupToolbar() {
  2. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  3. setSupportActionBar(toolbar);
  4. getSupportActionBar().setTitle("TabbedCoordinatorLayout");//设置标题
  5. getSupportActionBar().setDisplayHomeAsUpEnabled(true);//删除,则没有向左的箭头
  6. }
CollapsingToolbarLayout具体可以参考这位大神的讲解,我这边没有用到那么多属性:
或者参考官方文档:

代码实现:SearchActivity


[java] view plain copy
  1. package com.dictionary.activity;
  2. import android.os.Bundle;
  3. import android.support.design.widget.CollapsingToolbarLayout;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.support.v7.widget.LinearLayoutManager;
  6. import android.support.v7.widget.RecyclerView;
  7. import android.support.v7.widget.Toolbar;
  8. import com.dictionary.adapter.RecyclerAdapter;
  9. import com.dictionary.entity.News;
  10. import com.example.dictionary.R;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. public class SearchActivity extends AppCompatActivity {
  14. private RecyclerView recyclerview;
  15. private RecyclerAdapter adapter;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.acty_mainsearch);
  20. //设置标题
  21. setupToolbar();
  22. //设置滑动标题
  23. setupCollapsingToolbar();
  24. }
  25. private void setupCollapsingToolbar() {
  26. final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(
  27. R.id.collapse_toolbar);
  28. collapsingToolbar.setTitleEnabled(false);
  29. }
  30. //设置标题
  31. private void setupToolbar() {
  32. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  33. setSupportActionBar(toolbar);
  34. getSupportActionBar().setTitle("");
  35. }
  36. }

不过要在build.guild文件中添加依赖包,到这里就已经实现了上滑折叠的效果,其实很简单,可见这个控件的强大之处

[java] view plain copy
  1. dependencies {
  2. compile fileTree(dir: 'libs', include: '*.jar')
  3. compile 'com.android.support:appcompat-v7:23.0.1'
  4. compile 'com.android.support:design:23.0.1'
  5. compile 'com.android.support:recyclerview-v7:23.0.1'
  6. compile 'com.android.support:cardview-v7:23.0.1'
  7. compile 'com.android.support:recyclerview-v7:23.1.1'
  8. }

2、RecyclerView
通过使用RecyclerView控件,我们可以在APP中创建带有Material Design风格的复杂列表。RecyclerView控件和ListView的原理有很多相似的地方,都是维护少量的View来进行显示大量的数据,不过RecyclerView控件比ListView更加高级并且更加灵活。当我们的数据因为用户事件或者网络事件发生改变的时候也能很好的进行显示。和ListView不同的是,RecyclerView不用在负责Item的显示相关的功能,在这边所有有关布局,绘制,数据绑定等都被分拆成不同的类进行管理。具体有关RecyclerView的讲解可以参考:
讲得非常详细,我参考了一些,再次感谢。现在我想说下加载不同布局的实现方法。RecyclerView的布局文件上边已经给出了,在此不再赘述,加载不同布局我们要重写RecyclerAdapter适配器,在这里我写了一个News的实体类来记录每个布局需要显示的信息,而通过这个实体类中的Type_View值来确定需要加载哪个布局文件:(那三个不同的item布局我就不贴了)

RecyclerAdapter:
[java] view plain copy
  1. package com.dictionary.adapter;
  2. import android.content.Context;
  3. import android.support.v7.widget.RecyclerView;
  4. import android.support.v7.widget.RecyclerView.ViewHolder;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. import com.dictionary.entity.News;
  11. import com.example.dictionary.R;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. public class RecyclerAdapter extends RecyclerView.Adapter<ViewHolder> {
  15. public final static int TYPE_ONE = 1;
  16. public final static int TYPE_TWO = 2;
  17. public final static int TYPE_THREE = 3;
  18. private Context mContext;
  19. private List<News> newses = new ArrayList<>();
  20. private OnItemClickListener onItemClickListener;
  21. public RecyclerAdapter(List<News> newses,Context context) {
  22. super();
  23. this.mContext = context;
  24. this.newses = newses;
  25. }
  26. public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
  27. this.onItemClickListener = onItemClickListener;
  28. }
  29. public interface OnItemClickListener {
  30. void OnItemClick(View view, int position);
  31. void OnItemLongClick(View view, int position);
  32. }
  33. public void add(News news) {
  34. this.newses.add(news);
  35. notifyDataSetChanged();
  36. }
  37. @Override
  38. public int getItemViewType(int position) {
  39. return newses.get(position).getItemType();
  40. }
  41. @Override
  42. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  43. View view = null;
  44. ViewHolder holder = null;
  45. switch (viewType) {
  46. case TYPE_ONE:
  47. view = LayoutInflater.from(mContext).inflate(R.layout.list_item1_mainsearch, parent, false);
  48. holder = new ViewHolderOne(view);
  49. break;
  50. case TYPE_TWO:
  51. view = LayoutInflater.from(mContext).inflate(R.layout.list_item2_mainsearch, parent, false);
  52. holder = new ViewHolderTwo(view);
  53. break;
  54. case TYPE_THREE:
  55. view = LayoutInflater.from(mContext).inflate(R.layout.list_item3_mainsearch, parent, false);
  56. holder = new ViewHolderThree(view);
  57. break;
  58. }
  59. return holder;
  60. }
  61. @Override
  62. public void onBindViewHolder(final ViewHolder holder, int position) {
  63. switch (getItemViewType(position)) {
  64. case TYPE_ONE:
  65. final ViewHolderOne holderOne = (ViewHolderOne) holder;
  66. onItemEventClick(holderOne);
  67. break;
  68. case TYPE_TWO:
  69. ViewHolderTwo holderTwo = (ViewHolderTwo) holder;
  70. onItemEventClick(holderTwo);
  71. break;
  72. case TYPE_THREE:
  73. ViewHolderThree holderThree = (ViewHolderThree) holder;
  74. onItemEventClick(holderThree);
  75. }
  76. }
  77. @Override
  78. public int getItemCount() {
  79. return newses.size();
  80. }
  81. class ViewHolderOne extends ViewHolder {
  82. TextView tv_item1;
  83. public ViewHolderOne(View itemView) {
  84. super(itemView);
  85. tv_item1 = (TextView) itemView.findViewById(R.id.tv_item1);
  86. tv_item1.setText("我是修改过得文本1");
  87. }
  88. }
  89. class ViewHolderTwo extends ViewHolder {
  90. Button bt_item1;
  91. public ViewHolderTwo(View itemView) {
  92. super(itemView);
  93. bt_item1 = (Button) itemView.findViewById(R.id.bt_item1);
  94. }
  95. }
  96. class ViewHolderThree extends ViewHolder {
  97. TextView textView;
  98. public ViewHolderThree(View itemView) {
  99. super(itemView);
  100. textView = (TextView) itemView.findViewById(R.id.tv_content1);
  101. textView.setText("我是修改过得图片1");
  102. }
  103. }
  104. protected void onItemEventClick(ViewHolder holder) {
  105. final int position = holder.getLayoutPosition();
  106. holder.itemView.setOnClickListener(new View.OnClickListener() {
  107. @Override
  108. public void onClick(View v) {
  109. onItemClickListener.OnItemClick(v, position);
  110. }
  111. });
  112. holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
  113. @Override
  114. public boolean onLongClick(View v) {
  115. onItemClickListener.OnItemLongClick(v, position);
  116. return true;
  117. }
  118. });
  119. }
  120. }

News:
[java] view plain copy
  1. package com.dictionary.entity;
  2. public class News {
  3. private String text;
  4. private String image;
  5. private String button;
  6. private int itemType;
  7. public String getText() {
  8. return text;
  9. }
  10. public void setText(String text) {
  11. this.text = text;
  12. }
  13. public String getImage() {
  14. return image;
  15. }
  16. public void setImage(String image) {
  17. this.image = image;
  18. }
  19. public String getButton() {
  20. return button;
  21. }
  22. public void setButton(String button) {
  23. this.button = button;
  24. }
  25. public int getItemType() {
  26. return itemType;
  27. }
  28. public void setItemType(int itemType) {
  29. this.itemType = itemType;
  30. }
  31. public News(String text, int itemType, String button, String image) {
  32. this.text = text;
  33. this.itemType = itemType;
  34. this.button = button;
  35. this.image = image;
  36. }
  37. }

Activity中的实现:(只需要在最上面那个代码片段中添加以下片段就可以)

[java] view plain copy
  1. recyclerview = (RecyclerView) findViewById(R.id.rv_mainsearch);
  2. recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
  3. //初始化数据
  4. List<News> myDataset = new ArrayList<News>();
  5. myDataset.add(new News("111",1,"bbb","III"));
  6. myDataset.add(new News("222",2,"bbb","III"));
  7. myDataset.add(new News("333",3,"bbb","III"));
  8. //创建Adapter
  9. adapter = new RecyclerAdapter(myDataset,SearchActivity.this);
  10. recyclerview.setAdapter(adapter);
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP