Behrooz Payoon
Behrooz Payoon

Reputation: 1

Multiple RecyclerViews

I have a requirement where I have more than 1 horizontal scrolls in an app. Is it possible to achieve this via the new RecyclerView?
I tried to implement two RecyclerViews, one below the other in the same XML, but only one showed up and the other is empty.
How can I get more than one horizontal scroll?

XML Code:

<com.payoon.customviews.HorizontalRecycleView
    android:id="@+id/lst_LastBarbers"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rel_LastBarbers"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp" />

And This is Code:

    mFeaturedListView = (HorizontalRecycleView) root.findViewById(R.id.lst_FeaturedBarbers);
    mLatestListView = (HorizontalRecycleView) root.findViewById(R.id.lst_LastBarbers);

    mFeaturedAdapter = new Ad_FeaturedBarbers(mAct,lstFeaturedBarbers);
    mFeaturedListView.setAdapter(mFeaturedAdapter);

    mLatestAdapter = new Ad_LatestBarbers(mAct,lstFeaturedBarbers);
    mLatestListView.setAdapter(mLatestAdapter);

    getFeaturedBarbers();
    getLatestBarbers();

    return root;

Upvotes: 0

Views: 1273

Answers (2)

Ankit Tomer
Ankit Tomer

Reputation: 135

class BannerSmallAdapter extends RecyclerView.Adapter {

private final HomeScreenFragment context;
private final List<HomePageModel.ChildrenEntity> data;
public interface OnBannerSmallClicked {
    void onBannerSmallClicked(String static_page, String position);
}

OnBannerSmallClicked mCallback;

public BannerSmallAdapter(HomeScreenFragment context, List<HomePageModel.ChildrenEntity> children) {
    this.context = context;
    this.data = children;
    mCallback = (OnBannerSmallClicked) context;
}

@Override
public BannerSmallAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.banner_small_item_cell, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder viewholder = new ViewHolder(v);

    return viewholder;
}

@Override
public void onBindViewHolder(BannerSmallAdapter.ViewHolder holder, int position) {
    String image = data.get(position).getImage();
    if (holder.image != null) {
        if (image != null
                && !"null".equalsIgnoreCase(image)) {
            holder.image
                    .setDefaultImageResId(R.drawable.newcategorybanner_place_holderimage);
            holder.image
                    .setErrorImageResId(R.drawable.no_image_placeholder);
            holder.image.setImageUrl(
                    ConstantVariable.APPLICATION_IMAGE_URL
                            + image, imageLoader);
        } else {
            holder.image
                    .setImageResource(R.drawable.no_image_placeholder);
        }

    }


}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    public NetworkImageView image;

    public ViewHolder(View itemView) {
        super(itemView);
        image = (NetworkImageView) itemView.findViewById(R.id.img_banner_small);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String static_page = data.get(getAdapterPosition()).getStatic_page();
                String product_id = data.get(getAdapterPosition()).getProduct_id();
                mCallback.onBannerSmallClicked(static_page, product_id);
            }
        });
    }
}

}

Upvotes: 1

Mikelis Kaneps
Mikelis Kaneps

Reputation: 4584

Make a HoriznontalRecyclerView:

public class HorizontalRecycleView extends RecyclerView {
    private final LinearLayoutManager layoutManager;

    public HorizontalRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        layoutManager
                = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);

        this.setLayoutManager(layoutManager);

    }


}

Put it inside a LinearLayout that is inside ScrollView:

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">



<yourpackage.HorizontalRecycleView
                    android:id="@+id/deals_recycle_view"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/deals_row_height"
                     />
    <yourpackage.HorizontalRecycleView
                    android:id="@+id/deals_recycle_view"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/deals_row_height"
                    />
 </LinearLayout>
</ScrollView>

Upvotes: 0

Related Questions