【问题标题】:Vertical recyclerview (which includes horizontal recyclerview) is not working properly垂直 recyclerview(包括水平 recyclerview)无法正常工作
【发布时间】:2017-06-17 11:15:55
【问题描述】:

我有一个片段,其中包括两个回收器视图(垂直和水平)。

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

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)

<?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):

<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中显示每一项的内容):

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

【问题讨论】:

    标签: java android xml android-recyclerview nested


    【解决方案1】:

    看看这段代码: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 中:

    <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.LayoutManager layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false)...,是的,我试过了,但是不行。
    • 不幸的是,它仍然无法正常工作,无论如何谢谢
    • 尝试在不同的小部件上设置彩色背景,看看问题是来自行还是回收器?
    • 看起来行被渲染了,但它们之间的距离是无限的。这意味着当我无限向下滚动屏幕时,会出现第二/第三/...行,而当我向上滚动一点时,它们会消失。
    • 去掉setNestedScrollingEnable试试,还有,在行的布局中,把layout_heigt改成150dp
    【解决方案2】:

    几个小时后,我终于克服了这个问题。 Adapter、Fragment 和 RecyclerViews 背后的逻辑是正确且功能齐全的。甚至可以用作实现嵌套回收器视图的模板(水平回收器视图在垂直视图中)。然而,水平回收器视图设计上的一个小错误(applet_category_item.xml) 几乎会导致应用程序出现巨大的错误。

    长话短说,似乎RelativeLayout 不应该在这种情况下使用。当我删除它时,一切都顺利,生活变得更加甜蜜:)

    <LinearLayout 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="right"
        android:orientation="vertical"
        android:layout_margin="10dp">
    
        <TextView
            android:id="@+id/applet_category_item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:textSize="17sp" />
    
        <android.support.v7.widget.RecyclerView
                android:id="@+id/applet_items_horizontal_list"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"/>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 2015-11-30
      • 2015-12-17
      • 1970-01-01
      • 2016-10-26
      相关资源
      最近更新 更多