多个RecyclerViews

时间:2015-07-17 08:39:25

标签: android android-recyclerview

我有一个要求,我在一个应用程序中有超过1个水平滚动。是否可以通过新的RecyclerView实现这一目标? 我尝试在同一个XML中实现两个RecyclerViews,一个在另一个下面,但只有一个出现,另一个是空的。
如何获得多个水平滚动?

XML代码:     

<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" />

这是代码:

    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;

2 个答案:

答案 0 :(得分:1)

类BannerSmallAdapter扩展了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);
            }
        });
    }
}

}

答案 1 :(得分:0)

制作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);

    }


}

将它放在ScrollView内的LinearLayout中:

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