Megi Fernanda
Megi Fernanda

Reputation: 273

How to make two Recyclerview in one Layout

i already success to make one Recyclerview and i want to add new Recyclerview Horizontal on top. i will explain in my code :

<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 is my first Recyclerview have name xml feeds_listview

id:arrayListUser is my new Recyclerview, i want make this Recyclerview Horizontal

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>

and this is my class adapter

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);
}

so, in my code i success to display first recylerview with my first xml and i want add new recylerview horizontal with new xml feeds_listviewUser

Upvotes: 2

Views: 4982

Answers (2)

Patrick R
Patrick R

Reputation: 6857

You can use LinearLayout to wrap both 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>

And assign horizontal layout manager to one recyclerview and vertical layout manager to other

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

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

Upvotes: 1

Dhanumjay
Dhanumjay

Reputation: 525

Put your recyclerviews in Relative layout.

First add horizontal recyclerView to alignParentTop true and fix height according to visibility of feeds_listviewUser next add vertical recyclerView with layout_below horizontal recyclerview id.

Upvotes: 1

Related Questions