如何在一个布局中制作两个Recyclerview

时间:2017-06-14 02:51:04

标签: android android-recyclerview

我已经成功制作了一个Recyclerview,我想在顶部添加新的Recyclerview Horizo​​ntal。我将在我的代码中解释:

<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水平

新的Recylerview的

xml是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,并且我希望使用新的xml feeds_listviewUser

添加新的recylerview水平

2 个答案:

答案 0 :(得分:1)

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

首先将horizo​​ntal recycviewView添加到alignParentTop为true并根据feeds_listviewUser的可见性修复高度,然后使用layout_below horizo​​ntal recyclerview id添加vertical recyclelerView。

答案 1 :(得分: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);