我正在我的应用程序中实现一个可扩展的recyclerview适配器,除了我的可扩展适配器中有一个gridview之外,一切都正常工作,在gridview我正在放置图像,但似乎可扩展适配器没有给出高度,当我预定义高度一切正常,但我不想预定义它,我希望它包装内容在这里是我的代码
我的适配器XML
<FrameLayout
style="@style/commonListItemStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_swipe_item_neutral">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:foreground="?attr/selectableItemBackground">
<GridView
android:id="@+id/expanded_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:drawSelectorOnTop="true"
android:background="#ecf0f1"
android:numColumns="4"
android:horizontalSpacing="3dp"
android:verticalSpacing="3dp"
android:listSelector="?android:attr/selectableItemBackground"
/>
</FrameLayout>
我的适配器代码
public class ExpandableCategoryAdapter extends ExpandableRecyclerAdapter<CategoryParentViewHolder, CategoryChildViewHolder> {
/**
* Primary constructor. Sets up {@link #mParentItemList} and {@link #mItemList}.
* <p>
* Changes to {@link #mParentItemList} should be made through add/remove methods in
* {@link ExpandableRecyclerAdapter}
*
* @param parentItemList List of all {@link ParentListItem} objects to be
* displayed in the RecyclerView that this
* adapter is linked to
*/
private LayoutInflater mInflater;
private ImageInternalFetcher mImageFetcher;
private Context context;
public ExpandableCategoryAdapter(Context context, ArrayList<ParentListItem> parentItemList) {
super(parentItemList);
this.context = context;
mInflater = LayoutInflater.from(context);
mImageFetcher = new ImageInternalFetcher(context, 300);
}
@Override
public CategoryParentViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
View view = mInflater.inflate(R.layout.parent_category, parentViewGroup, false);
return new CategoryParentViewHolder(view);
}
@Override
public CategoryChildViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
View view = mInflater.inflate(R.layout.preview_child, childViewGroup, false);
return new CategoryChildViewHolder(view);
}
@Override
public void onBindParentViewHolder(CategoryParentViewHolder parentViewHolder, int position, ParentListItem parentListItem) {
ExpandableCategoryItem item = (ExpandableCategoryItem) parentListItem;
parentViewHolder.mCrimeTitleTextView.setText(item.getmDescription());
}
@Override
public void onBindChildViewHolder(CategoryChildViewHolder childViewHolder, int position, Object childListItem) {
ChildItem data = (ChildItem) childListItem;
ImageGalleryAdapter adapter = new ImageGalleryAdapter(context);
if(data.getImgUri() != null) {
for (String item : data.getImgUri()) {
try {
adapter.add(new ImageObject(Uri.parse(item)));
}catch (Exception e)
{
e.printStackTrace();
}
}
childViewHolder.gridView.setAdapter(adapter);
}
}
class ViewHolder {
ImageObject mImage;
ImageView mThumbnail;
}
public class ImageGalleryAdapter extends ArrayAdapter<ImageObject> {
public ImageGalleryAdapter(Context context) {
super(context, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.adapter_grid_expanded, null);
holder = new ViewHolder();
holder.mThumbnail = (ImageView) convertView.findViewById(R.id.pp__thumbnail_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ImageObject image = getItem(position);
if (holder.mImage == null || !holder.mImage.equals(image)) {
mImageFetcher.loadImage(image.getImagePath(), holder.mThumbnail);
holder.mImage = image;
}
return convertView;
}
}
包含Recycler的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/parentCord"
android:background="@drawable/repeatinggiftly"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:contentScrim="@color/google_lightblue"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/clothes"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|enterAlways"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/actionbar_gradient_dark" />
</FrameLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/anim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerParent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@android:color/transparent">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
android:id="@+id/relativeLayou">
<android.support.v7.widget.RecyclerView
android:id="@+id/scrollableview"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_height="match_parent"
/>
</android.support.v7.widget.CardView>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"
android:id="@+id/fabAddItem"
android:src="@drawable/ic_action_add"
app:backgroundTint="@color/accent"
app:layout_anchor="@+id/appbar"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>