2017-01-18 162 views
2

我試圖做這樣的事情。 enter image description here水平RecyclerView裏面垂直RecyclerView

所以我的想法是,我有垂直recyclerview與渠道,並在渠道的第二個位置我應該有一個relives水平recyclerview。

我不知道如何,我應該做的,我試着用viewholders搞亂你猜我應該在我channel_details佈局只有一個recyclerview,而另一個作爲item_channel_details一個項目,但我不能讓它上班。

這是我的代碼。

ChannelDetailsActivity:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_channel_details); 

    ImageView coverPhoto = (ImageView) findViewById(R.id.image_cover_details); 
    final HexagonImageView avatarPhoto = (HexagonImageView) findViewById(R.id.img_hex); 
    TextView toolbarText = (TextView) findViewById(R.id.txt_toolbar_title); 

    final Bundle b = getIntent().getExtras(); 
    final MNetworkChannel parcelChannel = 
      b.getParcelable(Const.IntentData.H_CHANNEL_LIST); 

    final MVideosForChannel parcelVideosForChannel = b.getParcelable(Const.IntentData.D_VIDEOS_LIST); 




    setChannelsView(); 
    setVideosView(); 


} 

private void setChannelsView() { 

    rvRelive = (RecyclerView) findViewById(R.id.rv_relive_details); 
    rvRelive.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); 
    adapterRelives = new ReliveAdapter(); 
    rvRelive.setAdapter(adapterRelives); 

    if (getIntent() != null && getIntent().getParcelableExtra(Const.IntentData.D_RELIVE_LIST) != null) { 
     adapterRelives.setData(((ReliveMainPojo) getIntent().getParcelableExtra(Const.IntentData.D_RELIVE_LIST)).relives); 
    } 
} 

private void setVideosView() { 

    rvVideos = (RecyclerView) findViewById(R.id.rv_videos); 
    rvVideos.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); 
    adapterVideos = new ChannelVideosAdapter(); 
    rvVideos.setAdapter(adapterVideos); 

    if (getIntent() != null && getIntent().getParcelableExtra(Const.IntentData.D_VIDEOS_LIST) != null) { 
     adapterVideos.setData(((MVideosForChannel) getIntent().getParcelableExtra(Const.IntentData.D_VIDEOS_LIST)).experience); 
    } 
} 

ChannelDetails適配器:

public final class ChannelVideosAdapter extends RecyclerView.Adapter<ChannelVideosAdapter.ViewHolder> { 

private List<MVideo> data = new ArrayList<>(); 

public ChannelVideosAdapter() { 
} 

public void setData(List<MVideo> newData) { 

    if (newData != null && !newData.isEmpty()) { 

     data = newData; 
     notifyDataSetChanged(); 
    } 
} 

public void clearData() { 

    data.clear(); 
    notifyDataSetChanged(); 
} 

@Override 
public final ChannelVideosAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { 
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_recycle_tile, parent, false)); 
} 

@Override 
public final void onBindViewHolder(final ChannelVideosAdapter.ViewHolder holder, final int position) { 

    final MVideo video = data.get(position); 

    final String videoBackgroundImageUrl = video.asset.frame; 
    final String videoName = video.name; 

    ImageLoader.getInstance().displayImage(videoBackgroundImageUrl, holder.coverPhoto, new ImageLoadingListener() { 
     @Override 
     public void onLoadingStarted(String imageUri, View view) { 
      holder.videoLoading.setVisibility(View.VISIBLE); 
     } 

     @Override 
     public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
      holder.videoLoading.setVisibility(View.GONE); 
     } 

     @Override 
     public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
      holder.videoLoading.setVisibility(View.GONE); 
     } 

     @Override 
     public void onLoadingCancelled(String imageUri, View view) { 
      holder.videoLoading.setVisibility(View.GONE); 
     } 
    }); 

    holder.videoName.setText(videoName); 
    holder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      VideoPlayerActivity.StartNewVideoPlayerActivity((ChannelDetailsActivity) holder.itemView.getContext(), video, true); 
     } 
    }); 
} 

@Override 
public final int getItemCount() { 
    return data.size(); 
} 

final class ViewHolder extends RecyclerView.ViewHolder { 

    private final ImageView coverPhoto; 
    private final TextView videoName; 
    private final ProgressBar videoLoading; 

    ViewHolder(final View itemView) { 
     super(itemView); 

     coverPhoto = (ImageView) itemView.findViewById(R.id.img_thumbnail_background_video); 
     videoName = (TextView) itemView.findViewById(R.id.txt_video_name); 
     videoLoading = (ProgressBar) itemView.findViewById(R.id.pb_video_loading); 
    } 
} 
} 

重溫適配器:

public final class ReliveAdapter extends RecyclerView.Adapter<ReliveAdapter.ViewHolder> { 

private List<Relive> data = new ArrayList<>(); 

public ReliveAdapter() { 
} 

public void setData(List<Relive> newData) { 

    if (newData != null && !newData.isEmpty()) { 

     data = newData; 
     notifyDataSetChanged(); 
    } 
} 

public void clearData() { 

    data.clear(); 
    notifyDataSetChanged(); 
} 

@Override 
public final ReliveAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { 
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_relive_recycle_tile, parent, false)); 
} 

@Override 
public void onBindViewHolder(final ReliveAdapter.ViewHolder holder, final int position) { 

    final Relive relive = data.get(position); 

    final String reliveOwnerIconUrl = relive.owner.asset.large; 
    final String reliveCoverPhotoUrl = relive.asset.stream.thumbnail; 
    final String reliveDescription = relive.owner.name; 

    ImageLoader.getInstance().displayImage(reliveCoverPhotoUrl, holder.backgroundImage, new ImageLoadingListener() { 
     @Override 
     public void onLoadingStarted(String imageUri, View view) { 
      holder.imageLoading.setVisibility(View.VISIBLE); 
     } 

     @Override 
     public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
      holder.imageLoading.setVisibility(View.GONE); 
     } 

     @Override 
     public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
      holder.imageLoading.setVisibility(View.GONE); 

      ImageLoader.getInstance().displayImage(reliveOwnerIconUrl, holder.profilePicture, new ImageLoadingListener() { 
       @Override 
       public void onLoadingStarted(String imageUri, View view) { 
        holder.imageLoading.setVisibility(View.VISIBLE); 
       } 

       @Override 
       public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
        holder.imageLoading.setVisibility(View.GONE); 
       } 

       @Override 
       public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
        holder.imageLoading.setVisibility(View.GONE); 
       } 

       @Override 
       public void onLoadingCancelled(String imageUri, View view) { 
        holder.imageLoading.setVisibility(View.GONE); 
       } 
      }); 

      holder.eyeIcon.setImageResource(R.drawable.relive); 
     } 

     @Override 
     public void onLoadingCancelled(String imageUri, View view) { 
      holder.imageLoading.setVisibility(View.GONE); 
     } 
    }); 

    holder.reliveDescription.setText(reliveDescription); 

    holder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RelivePlayerActivity.StartReliveReviewActivity((ChannelDetailsActivity) holder.itemView.getContext(), relive.asset.stream.url, relive.experienceGuid, relive.guid, holder.getAdapterPosition()); 
     } 
    }); 
} 

@Override 
public final int getItemCount() { 
    return data.size(); 
} 

final class ViewHolder extends RecyclerView.ViewHolder { 

    private final CircleImageView profilePicture; 
    private final ImageView eyeIcon; 
    private final ImageView backgroundImage; 
    private final TextView reliveDescription; 
    private final ProgressBar imageLoading; 

    ViewHolder(View itemView) { 
     super(itemView); 

     profilePicture = (CircleImageView) itemView.findViewById(R.id.profile_circle_image); 
     eyeIcon = (ImageView) itemView.findViewById(R.id.icon_circle_image); 
     backgroundImage = (ImageView) itemView.findViewById(R.id.thumbnail_image); 
     reliveDescription = (TextView) itemView.findViewById(R.id.description_textview); 
     imageLoading = (ProgressBar) itemView.findViewById(R.id.image_loading); 
    } 
} 

}

這是我的佈局:

activity_channel_details

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar_details" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:background="#017789" 
    android:textAlignment="center"> 

    <TextView 
     android:id="@+id/txt_toolbar_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textColor="@android:color/white" 
     android:textStyle="bold" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:layout_marginEnd="10dp" 
     android:background="@color/white_trans" 
     android:src="@drawable/zeality" /> 

</android.support.v7.widget.Toolbar> 

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff" 
    android:orientation="vertical"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#ffff" 
     android:orientation="vertical"> 


     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#ffff" 
      android:orientation="vertical"> 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:id="@+id/image_cover_details" 
        android:layout_width="match_parent" 
        android:layout_height="120dp" 
        android:adjustViewBounds="true" 
        android:scaleType="fitXY" /> 

       <FrameLayout 
        android:id="@+id/frame" 
        android:layout_width="100dp" 
        android:layout_height="110dp" 
        android:layout_centerInParent="true"> 

        <co.zeality.vrplayer.views.HexagonImageView 
         android:id="@+id/img_hex" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentTop="true" 
         android:layout_centerHorizontal="true" 
         android:scaleType="fitXY" /> 

       </FrameLayout> 

      </RelativeLayout> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp"> 

       <android.support.v7.widget.RecyclerView 
        android:id="@+id/rv_videos" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:nestedScrollingEnabled="false" /> 
      </RelativeLayout> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/rv_relive_details" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

     </LinearLayout> 
    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

item_video_recycle_tile

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

<ImageView 
    android:id="@+id/img_thumbnail_background_video" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:adjustViewBounds="true" /> 

<FrameLayout 
    android:id="@+id/frame_image" 
    android:layout_width="150dp" 
    android:layout_height="100dp" 
    android:layout_centerInParent="true"> 

    <ProgressBar 
     android:id="@+id/pb_video_loading" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:foregroundGravity="center" 
     android:visibility="gone" /> 

    <ImageView 
     android:layout_width="80dp" 
     android:layout_height="50dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginBottom="40dp" 
     android:src="@drawable/glasses" /> 
</FrameLayout> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/img_play_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_marginStart="10dp" 
      android:src="@drawable/play_no_circle" /> 

     <TextView 
      android:id="@+id/txt_video_name" 
      android:layout_width="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_height="match_parent" 
      android:layout_below="@id/img_play_button" 
      android:layout_marginStart="5dp" 
      android:textColor="#FFF" /> 
    </RelativeLayout> 
</FrameLayout> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="5dp" 
    android:layout_alignParentBottom="true" 
    android:background="#660c7582"></View> 

+0

使用帶有linearlayout管理器及其項目的一個recyclerview作爲具有gridlayout管理器的另一個recyclerview。 –

+0

你需要在你的適配器中處理這個。您將確定您想要哪個位置具有水平的RecyclerView,並且您將爲那個特定的適配器位置創建該視圖。 在這個位置上,您將創建您的視圖爲一個回收站 – Booger

回答

3

應該使用一個recyclerView(垂直)作爲母體,並在位置1處的在適配器綁定視圖的一次返回一個包含recyclerView(水平)並加載其他廣告的視圖對於那個recyclerView來說。請參考圖正確理解。

Main RecyclerView (vertical): 
    --------------------------- 
    + Item 1 
    --------------------------- 
    + Second RecyclerView (Horizontal) 
    --------------------------- 
    + Item 2 
    --------------------------- 

父RecyclerView適配器代碼:

@Override 
     public int getItemViewType(int position) { 

      if (position == 1) 
       return 0; 
      else 
       return 1; 
     } 
    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     if (viewType == 0) { 
      View v = LayoutInflater.from(context).inflate(R.layout.your_second_recylerView_layout, parent, false); 
      return new ViewHolder1(v); 
     } 
     else{ 
      View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, parent, false); 
      return new ViewHolder2(v); 
     } 

    } 

現在你需要實現水平recycleView第二適配器。

+0

謝謝你的回答。我試圖搞砸它,它仍然太複雜。你可能有某種參考或樣本? – hyrulelink16

相關問題