垂直recyclerview(包括水平recyclerview)无法正常工作 [英] Vertical recyclerview (which includes horizontal recyclerview) is not working properly

查看:65
本文介绍了垂直recyclerview(包括水平recyclerview)无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,其中包含两个回收站视图(垂直和水平).

I have a fragment which includes two recycler views (vertical and horizontal).

我为这两个回收站视图编写了所有适配器(受的启发这个答案),但问题是外部(垂直)recyclerview仅显示第一个项目,而没有显示其余项目.我尝试的代码如下:

I write all the adapters for these 2 recycler views (inspired by this answer) but the problem is that the outer (vertical) recyclerview just shows the first item and it doesn't show rest of the items. My attempted code is as follows:

aFragment.java

...
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new  LinearLayoutManager(getActivity()) {
    @Override
    public boolean canScrollVertically() {
        return false;
    }
};
recyclerView.setLayoutManager(layoutManager);
adapter = new AppletCategoryAdapter(applets, getActivity());
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter.notifyDataSetChanged();
...

AppletCategoryAdapter.java:

public class AppletCategoryAdapter  extends RecyclerView.Adapter<AppletCategoryAdapter.SimpleViewHolder> {

    private Context mContext;
    private List<AppletItemsCategory> mData;
    ...

    public class SimpleViewHolder extends RecyclerView.ViewHolder {
        public final TextView title;
        public RecyclerView horizontalList;

        public SimpleViewHolder(View view) {
            super(view);
            this.title = (TextView) view.findViewById(R.id.applet_category_item_name);
            this.horizontalList = (RecyclerView) itemView.findViewById(R.id.applet_items_horizontal_list);
            this.horizontalList.setLayoutManager(new LinearLayoutManager(horizontalList.getContext(), LinearLayoutManager.HORIZONTAL, false));
            this.horizontalList.setNestedScrollingEnabled(false);
            horizontalList.setAdapter(null);
        }
    }


    public AppletCategoryAdapter(List<AppletItemsCategory> data, Context context) {
        mData = data;
        mContext = context;
    }

     public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.applet_category_item, parent, false);
        return new SimpleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SimpleViewHolder holder, final int position) {
        List<Applet> applets = mData.get(position).getProductOffers();
        holder.title.setText(mData.get(position).getCategoryName());
        AppletCardAdapter appletCardAdapter = new AppletCardAdapter(applets,mContext);
        holder.horizontalList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        holder.horizontalList.setAdapter(appletCardAdapter);
        holder.horizontalList.setNestedScrollingEnabled(false);
        appletCardAdapter.notifyDataSetChanged();
    }

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

AppletCardAdapter.java:

public class AppletCardAdapter extends RecyclerView.Adapter<AppletCardAdapter.ViewHolder> {
    private ImageLoader imageLoader;
    private Context context;

    //List of channels
    List<Applet> applets;

    public AppletCardAdapter(List<Applet> applets, Context ctx) {
        this.applets = applets;
        this.context = ctx;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.applets_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Applet applet =  applets.get(position);
        holder.setIsRecyclable(true);
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(Config.BASE_URL + applet.getImage(), ImageLoader.getImageListener(holder.imageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
        holder.imageView.setImageUrl(Config.BASE_URL + applet.getImage(), imageLoader);
        holder.textViewName.setText(applet.getName());
        holder.textViewDescription.setText(applet.getDescription());
        try {
            holder.cardView.setCardBackgroundColor(Color.parseColor(applet.getColor()));
        }
        catch (Exception e) {
            Log.d("iotel", "Unknown color");
        }
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openAppletDetailFragment(applet);
            }
        });
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewDescription;
        public CardView cardView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.appletImage);
            textViewName = (TextView) itemView.findViewById(R.id.appletName);
            textViewDescription = (TextView) itemView.findViewById(R.id.appletDescription);
            cardView = (CardView) itemView.findViewById(R.id.appletCard);
        }
    }
}

a_fragment.xml:(包含垂直recyclerView)

a_fragment.xml: (containing vertical recyclerView)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleInverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="invisible"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:focusableInTouchMode="true"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

</android.support.v4.widget.NestedScrollView>

applet_category_item.xml (包含水平recyclerView):

applet_category_item.xml (containing horizontal recyclerView):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentRight="true">

        <TextView
            android:id="@+id/applet_category_item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/applet_items_horizontal_list"
            android:layout_width="match_parent"
            android:layout_marginTop="5dp"
            android:layout_height="160dp"
            android:layout_gravity="center"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </LinearLayout>

</RelativeLayout>

applet_list.xml (在卧式recyclerView中显示每个项目的内容):

applet_list.xml (shows the content of each item in horizontal recyclerView):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="5dp">

    <android.support.v7.widget.CardView
        android:layout_width="150dp"
        android:layout_height="150dp"
        style="@style/MyCardViewStyle"
        app:cardBackgroundColor="@color/cardDefaultBg"
        app:cardCornerRadius="15dp"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:id="@+id/appletCard">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.android.volley.toolbox.NetworkImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:id="@+id/appletImage" />
            <LinearLayout
                android:padding="8dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <ir.iotel.MyTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_gravity="end"
                    android:textSize="9pt"
                    android:textColor="@color/fontSecondary"
                    android:id="@+id/appletName" />
                <ir.iotel.MyTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:textColor="@color/fontSecondary"
                    android:id="@+id/appletDescription" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout >

推荐答案

查看以下代码:https://github.com/hardworker93/carousels/tree/master

您应该尝试以下方法:

在片段中

mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);      
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setHasFixedSize(true);

在适配器中:

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

        List<Item> RowItems = mRows.get(position);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false);     
        holder.mRecyclerViewRow.setLayoutManager(layoutManager);
        holder.mRecyclerViewRow.setHasFixedSize(true);
        RowRecyclerAdapter rowsRecyclerAdapter = new RowRecyclerAdapter(mContext,RowItems);         
        holder.mRecyclerViewRow.setAdapter(rowsRecyclerAdapter);

在布局applet_category_item中:

In layout applet_category_item :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp">

这篇关于垂直recyclerview(包括水平recyclerview)无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆