2017-06-14 90 views
0

我已经成功制作一个RecyclerView,并且我想在顶部添加新的Recyclerview Horizo​​ntal。我将在我的代码解释:如何在一个布局中制作两个Recyclerview

<android.support.v7.widget.RecyclerView 
      android:id="@+id/arrayListUser" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:divider="@color/white"> 


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

<android.support.v7.widget.RecyclerView 
      android:id="@+id/arrayList" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentTop="true" 
      android:divider="@color/white"> 


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

ID:ArrayList的是我第一次Recyclerview 有名字的XML feeds_listview

ID:arrayListUser是我的新Recyclerview,我想使这个Recyclerview水平

xml for new Recylerview is feeds_listviewUser

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <de.hdodenhof.circleimageview.CircleImageView 
      android:id="@+id/profil" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_marginBottom="5dp" 
      android:layout_marginTop="30dp" 
      android:layout_gravity="center" 
      android:src="@drawable/cthprofil" /> 
     <TextView 
      android:id="@+id/fullName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="20" 
      android:text="Megi Fernanda" 
      android:textSize="17sp" 
      android:textColor="@color/colordefault" 
      android:textStyle="bold" /> 


    </LinearLayout> 

,这是我的课适配器

public class FeedsCustomAdapter extends RecyclerView.Adapter<FeedsCustomAdapter.ViewHolder> { 

private Context context; 
private List<FeedsAdapter> feeds_list; 
private ArrayList<Feeds> mFeedsList = new ArrayList<Feeds>(); 
private OnItemClickListener mListener; 
private OnItemClickListener mListener2; 

public FeedsCustomAdapter(Context context, ArrayList<Feeds> mFeedsList) { 
    this.context = context; 
    this.mFeedsList = mFeedsList; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.feeds_listview, parent, false); 

    return new ViewHolder(itemView); 
} 


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

    Feeds feed = getFeeds().get(position); 

    int textColor = context.getResources().getColor(R.color.btn_next); 
    int textColor2 = context.getResources().getColor(R.color.text_color_black); 


    holder.fullName.setText(feed.user.fullName); 
    holder.location.setText(feed.user.location); 
    holder.topic.setText(Html.fromHtml("Menyelesaikan Tantangan " + " <font color = '" + String.valueOf(textColor2) + "'>" + feed.topic + "</font>")); 

    Picasso.with(context) 
      .load(feed.user.avatar) 
      .into(holder.profile); 

    PrettyTime prettyTime = new PrettyTime(); 

    String times = prettyTime.format(DateUtil.timeMilisTodate(feed.timestamp * 1000)); 
    holder.times.setText(times); 

} 

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

public ArrayList<Feeds> getFeeds() { 
    return mFeedsList; 
} 

public void setComplete(int position) { 
    mFeedsList.get(position).isComplete = 1; 
} 

public boolean last() { 
    boolean result = false; 
    int total = mFeedsList.size(); 

    for (int i = 0; i < mFeedsList.size(); i++) { 
     if (mFeedsList.get(i).isComplete == 1) { 
      total--; 
     } 
    } 

    if (total == 1) { 
     result = true; 
    } 

    return result; 
} 




class ViewHolder extends RecyclerView.ViewHolder { 

    public TextView fullName; 
    public TextView location; 
    public TextView topic; 
    public ImageView profile; 
    public TextView times; 

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

     fullName = (TextView) itemView.findViewById(R.id.fullName); 
     location = (TextView) itemView.findViewById(R.id.location); 
     topic = (TextView) itemView.findViewById(R.id.topic); 
     profile = (ImageView) itemView.findViewById(R.id.profil); 
     times = (TextView) itemView.findViewById(R.id.times); 

     profile.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(mListener2 != null){ 
        mListener2.onItemClick2(v ,getPosition()); 
       } 

      } 
     }); 

     topic.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(mListener != null){ 
        mListener.onItemClick(v ,getPosition()); 
       } 

      } 
     }); 
    } 

} 

public void setClickListener(OnItemClickListener clickListener) { 
    this.mListener = clickListener; 

} 

public void setClickListenerProfile(OnItemClickListener clickListener2){ 
    this.mListener2 = clickListener2; 
} 


public interface OnItemClickListener { 
    public abstract void onItemClick(View view, int position); 
    public abstract void onItemClick2(View view, int position); 
} 

所以,在我的代码我的成功与我的第一个XML和显示第一recylerview我要添加新的recylerview水平与新的XML feeds_listviewUser

+0

,你在第一回收做的一切定义 –

回答

0

您可以使用LinearLayout来封装recyclerView。

<android.support.v7.widget.RecyclerView 
    android:id="@+id/arrayListUser" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:divider="@color/white"> 
</android.support.v7.widget.RecyclerView> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/arrayList" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:divider="@color/white"> 
</android.support.v7.widget.RecyclerView> 

,并分配水平布局管理一个recyclerview和垂直布局管理器等

LinearLayoutManager userManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); 
arrayListUser.setLayoutManager(userManager); 

LinearLayoutManager listManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 
arrayList.setLayoutManager(listManager); 
1

将您的recyclerviews放置在相对布局中。

首先将水平recyclerView添加到alignParentTop true并根据feeds_listviewUser的可见性修复高度next使用layout_below horizo​​ntal recyclerview id添加垂直recyclerView。

相关问题