2016-03-04 51 views

回答

0

这取决于你想添加什么样的顶面布置的。

如果你想添加图片或查看传呼机与滑动图像,那么它最好使用CollapsingToolbarLayoutAppBarLayout里面CoordinatorLayout,然后添加您的RecyclerView。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/profile_parentlayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="260dp" 
      android:fitsSystemWindows="true" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/collapsing_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:fitsSystemWindows="true" 
       app:contentScrim="?attr/colorAccent" 
       app:expandedTitleMarginEnd="64dp" 
       app:expandedTitleMarginStart="48dp" 
       app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

       <LinearLayout 
        android:id="@+id/avatar_bg" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="?attr/colorAccent" 
        android:gravity="center" 
        android:minHeight="160dp" 
        android:orientation="vertical" 
        app:layout_collapseMode="parallax"> 
        <!-- Here you can add your header layout only if its not too big--> 
        <ImageView 
         android:id="@+id/myprofile_avatar" 
         android:layout_width="@dimen/profile_avatar_size" 
         android:layout_height="@dimen/profile_avatar_size" /> 



       </LinearLayout> 
       <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:local="http://schemas.android.com/apk/res-auto" 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        android:minHeight="?attr/actionBarSize" 
        local:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
        local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
        app:layout_collapseMode="pin"/> 

      </android.support.design.widget.CollapsingToolbarLayout> 

     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipeRefreshLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
      <android.support.v7.widget.RecyclerView 
       android:id="@+id/profile_list" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       /> 
</android.support.v4.widget.SwipeRefreshLayout> 


    </android.support.design.widget.CoordinatorLayout> 
    <ProgressBar 
     android:id="@+id/progress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     /> 

</RelativeLayout> 
-1

你可以做到这一点的方式,包括多个视图:

@Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     switch (viewType) { 
      case 0: return new ViewHolder0(...); 
      case 2: return new ViewHolder2(...); 

     } 
    } 

参考RecyclerView with multiple view type

+0

感谢乌尔快速回复艾米特。但是当我从内容顶部进行操作时,recyclerview内容的底部将被聚焦到屏幕的顶部。怎么做? – Krishna

+0

USE recyclerview.requestFocus(); –

0

它可以通过重写该方法中,getItemViewType(INT位置)来完成这指定膨胀为回收站视图

@Override 
    public int getItemViewType(int position) { 
     if (position == 0) 
      return LAYOUT_TYPE_0; 
     else if (position == 1) 
      return LAYOUT_TYPE_1; 
     else 
      return LAYOUT_TYPE_ITEM; 
    } 

这里的perticular位置,其布局是完整适配器代码

public class RecyclerViewAdapter extends RecyclerView.Adapter { 

     public static final int LAYOUT_TYPE_0 = 0; 
     public static final int LAYOUT_TYPE_1 = 1; 
     public static final int LAYOUT_TYPE_ITEM = 2; 


     @Override 
     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      if (viewType == LAYOUT_TYPE_0) { 
       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_0, parent, false); 
       return new LayoutPos0(view); 
      } else if (viewType == LAYOUT_TYPE_1) { 
       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_1, parent, false); 
       return new LayoutPos1(view); 
      } else { 
       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_common, parent, false); 
       return new LayoutCommon(view); 
      } 

     } 

     @Override 
     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {    
     } 

     @Override 
     public int getItemViewType(int position) { 
      if (position == 0) 
       return LAYOUT_TYPE_0; 
      else if (position == 1) 
       return LAYOUT_TYPE_1; 
      else 
       return LAYOUT_TYPE_ITEM; 
     } 



     public class LayoutPos0 extends RecyclerView.ViewHolder { 
       //your code goes here 
     } 

     private class LayoutPos1 extends RecyclerView.ViewHolder { 
     } 

     private class LayoutCommon extends RecyclerView.ViewHolder{   
    } 

@Override 
     public int getItemCount() { 
      return 100; 
     } 

} 
相关问题