我有一个片段,其中包含两个回收者视图(垂直和水平)。
我为这2个回收者视图编写了所有适配器(受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 >
答案 0 :(得分:0)
请看这段代码: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">
答案 1 :(得分:0)
几个小时后,我终于克服了这个问题。 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>