Android 中 RecyclerListView 内的可扩展 ListView

Expandable ListView inside a RecyclerListView in Android

如何在 RecyclerView ListView 中放置一个 Expandable ListView 如何做到这一点? 像这样

不展开ExpandingListView

随着扩展ExpandableListView

在 RecyclerView 中使用了可扩展列表视图。

一切正常,但在扩展时,当给定高度作为 Recycler 视图项目高度中的包装内容时,我们无法查看子项目。

在给定 Recyclerview 项目高度的固定高度的情况下查看相同的内容,但效率不高。

还有其他方法吗? 提前致谢

主要 RecyclerView 适配器

 public class MySnatchitsListAdapterNew extends RecyclerView.Adapter<MySnatchitsListAdapterNew.SnatchitViewHolder> {

            Context context;
            ArrayList<Promotion> snatchitslist = new ArrayList<>();
            LayoutInflater inflater;
            AdapterInterface adapterInterface;
            boolean isworkinginbackground = false;
            SimpleTooltip tooltip;

            public void setIsworkinginbackground(boolean isworkinginbackground) {
                this.isworkinginbackground = isworkinginbackground;
            }

            public MySnatchitsListAdapterNew(Context context, ArrayList<Promotion> snatchitslist, AdapterInterface adapterinterface) {

                this.context = context;
                this.snatchitslist = snatchitslist;
                this.inflater = LayoutInflater.from(context);
                this.adapterInterface = adapterinterface;
            }

            @Override
            public SnatchitViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                View view = inflater.inflate(R.layout.my_snatchit_list_item_new, null, false);
                return new SnatchitViewHolder(view);
            }

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

                Promotion item = snatchitslist.get(position);

                CommonUtils.loadProductImage(context, item.getPromotionImage(), R.drawable.no_product_image, R.drawable.no_product_image, holder.img_product);

                holder.swtch_continue.setOnCheckedChangeListener(null);
                holder.swtch_continue.setChecked((item.getIsDiscontinued() == 0));

                holder.txt_view_count.setText(item.getViewcount() + "");
                holder.txt_purchase_count.setText(item.getPurchasecount() + "");

               holder.adapter = new ProductExpandableListAdapter(context,snatchitslist.get(position));
                holder.exp_products.setAdapter(holder.adapter);


                if (item.isdiscontinueinprogress()) {

                    holder.pb_discontinue.setVisibility(View.VISIBLE);
                    holder.swtch_continue.setVisibility(View.INVISIBLE);


                } else {

                    holder.pb_discontinue.setVisibility(View.INVISIBLE);
                    holder.swtch_continue.setVisibility(View.VISIBLE);
                    holder.swtch_continue.setTag(position);

                    if (item.isdeleting() || (isworkinginbackground)) {

                        holder.swtch_continue.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                                buttonView.setChecked(!isChecked);
                                CommonUtils.showToast(context, context.getString(R.string.operation_in_progress));

                            }
                        });

                    } else {

                        holder.swtch_continue.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                                int pos = (int) buttonView.getTag();

                                adapterInterface.doAction(MySnatchitsFragment.DISCONTINUE_PROMOTION, pos, isChecked);

                            }
                        });

                    }
                }

                holder.llyt_my_snatchit.setTag(position);
                holder.llyt_my_snatchit.setTag(R.id.key_holder, holder);
                holder.llyt_my_snatchit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (!isworkinginbackground) {

                            int pos = (int) v.getTag();
                            SnatchitViewHolder holder = (SnatchitViewHolder) v.getTag(R.id.key_holder);
                            getQRCodeDetails(holder, pos);

                        } else {
                            CommonUtils.showToast(context, context.getString(R.string.operation_in_progress));
                        }

                    }
                });


                holder.btn_post.setTag(position);
                holder.btn_post.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int pos = (int) v.getTag();
                        Promotion item = snatchitslist.get(pos);

                        if ((isworkinginbackground) || (item.isdeleting()) || (item.isdiscontinueinprogress())) {

                            CommonUtils.showToast(context, context.getString(R.string.operation_in_progress));

                        } else {

                            adapterInterface.doAction(MySnatchitsFragment.POST_PROMOTION, pos);

                        }
                    }
                });





            public void hideToolTip() {

                if ((tooltip != null) && (tooltip.isShowing())) {

                    tooltip.dismiss();

                }
            }

            public boolean istooltipshowing() {

                boolean isshowing = false;

                if ((tooltip != null) && (tooltip.isShowing())) {

                    isshowing = true;

                }

                return isshowing;
            }



            public class SnatchitViewHolder extends RecyclerView.ViewHolder {


                ImageView img_product;
                Switch swtch_continue;
                Button btn_post;
                LinearLayout llyt_my_snatchit;
                ProgressBar pb_discontinue;
                TextView txt_view_count, txt_purchase_count;
                ImageView img_view_count, img_purchase_count;
                ExpandableListView exp_products;
                ProductExpandableListAdapter adapter ;

                public SnatchitViewHolder(View itemView) {

                    super(itemView);
                    img_product = (ImageView) itemView.findViewById(R.id.img_product);
                    swtch_continue = (Switch) itemView.findViewById(R.id.swtch_continue);
                    btn_post = (Button) itemView.findViewById(R.id.btn_post);
                    llyt_my_snatchit = (LinearLayout) itemView.findViewById(R.id.llyt_my_snatchit);
                    pb_discontinue = (ProgressBar) itemView.findViewById(R.id.pb_discontinue);
                    txt_view_count = itemView.findViewById(R.id.txt_view_count);
                    txt_purchase_count = itemView.findViewById(R.id.txt_purchase_count);
                    img_view_count = itemView.findViewById(R.id.img_view_count);
                    img_purchase_count = itemView.findViewById(R.id.img_purchase_count);
                    exp_products = itemView.findViewById(R.id.exp_products);


                }
            }
        }

布局 - my_snatchit_list_item_new

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <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:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
        android:animateLayoutChanges="true"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="8dp"
        app:cardElevation="4dp">


        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/llyt_my_snatchit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:orientation="horizontal"
            android:padding="8dp">

            <ImageView
                android:id="@+id/img_product"
                android:layout_width="@dimen/product_image_width"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/no_product_image" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:animateLayoutChanges="true"
                android:orientation="vertical">

                <ExpandableListView
                    android:id="@+id/exp_products"
                    android:layout_width="match_parent"
                    android:groupIndicator="@null"
                    android:layout_height="wrap_content">

                </ExpandableListView>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:layout_marginTop="4dp"
                    android:paddingTop="4dp"
                    android:paddingBottom="4dp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:gravity="center_vertical"
                        android:layout_weight="1">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:gravity="center_vertical"
                            android:orientation="horizontal">

                            <ImageView
                                android:id="@+id/img_view_count"
                                android:layout_width="24dp"
                                android:layout_height="24dp"
                                android:src="@drawable/camera" />

                            <com.integral.app.snatchit.ui.FontTextView

                                android:id="@+id/txt_view_count"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="8dp"
                                android:textColor="@color/form_text_color"
                                android:textAppearance="@style/TextAppearance.AppCompat.Medium" />


                        </LinearLayout>


                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="8dp"
                            android:gravity="center_vertical"
                            android:orientation="horizontal">

                            <ImageView
                                android:id="@+id/img_purchase_count"
                                android:layout_width="24dp"
                                android:layout_height="24dp"
                                android:src="@drawable/bag" />

                            <com.integral.app.snatchit.ui.FontTextView

                                android:id="@+id/txt_purchase_count"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="8dp"
                                android:textColor="@color/form_text_color"
                                android:textAppearance="@style/TextAppearance.AppCompat.Medium" />


                        </LinearLayout>


                    </LinearLayout>

                    <com.integral.app.snatchit.ui.FontButton
                        android:id="@+id/btn_post"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/button_height_small"
                        android:background="@drawable/post_background"
                        android:elevation="2dp"
                        android:minWidth="0dp"
                        android:paddingBottom="4dp"
                        android:paddingLeft="16dp"
                        android:paddingRight="16dp"
                        android:paddingTop="4dp"
                        android:text="@string/post"
                        android:textColor="@color/clickable_white_text_color" />


                    <FrameLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right|center_vertical"
                        android:visibility="gone"
                        android:animateLayoutChanges="true">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:gravity="center_vertical"
                            android:animateLayoutChanges="true"

                            >

                            <Switch
                                android:id="@+id/swtch_continue"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:animateLayoutChanges="true"
                                android:checked="false"
                                android:gravity="center_vertical"
                                android:textOff="no"
                                android:textOn="yes"
                                android:thumb="@drawable/swith_thumb"
                                android:track="@drawable/swith_track"
                                android:visibility="visible" />

                        </LinearLayout>

                        <ProgressBar
                            android:id="@+id/pb_discontinue"
                            style="?android:attr/progressBarStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:animateLayoutChanges="true"
                            android:padding="2dp"
                            android:visibility="invisible" />

                    </FrameLayout>

                </LinearLayout>


            </LinearLayout>


        </LinearLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

内部可扩展 ListView 适配器 - ProductExpandableListAdapter

package com.integral.app.snatchit.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.integral.app.snatchit.R;
import com.integral.app.snatchit.model.Product;
import com.integral.app.snatchit.model.Promotion;
import com.integral.app.snatchit.util.CommonUtils;

/**
 * Created by sanoop on 23-01-2018.
 */

public class ProductExpandableListAdapter extends BaseExpandableListAdapter {


    Context context;
    Promotion promotion;


    public ProductExpandableListAdapter(Context context, Promotion promotion) {

        this.context = context;
        this.promotion = promotion;

    }

    @Override
    public int getGroupCount() {
        return 1;
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        int childcount = (promotion.getProducts().size() - 2);

            return (childcount<0)? 0 :  childcount;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return promotion;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return promotion.getProducts().get(childPosition + 2);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {


        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.my_snatchit_product_item_header, null, false);
            ProductHeaderViewHolder holder = new ProductHeaderViewHolder(convertView);
            convertView.setTag(holder);
        }

        ProductHeaderViewHolder holder = (ProductHeaderViewHolder) convertView.getTag();
        if (promotion.getProducts().size() > 0) {

            Product firstproduct = promotion.getProducts().get(0);
            holder.txt_product_name_1.setText(firstproduct.getProductName());
            String currencysymbol = CommonUtils.getCurrencySymbol(promotion.getCurrencyCode());
            holder.txt_local_price_1.setText(currencysymbol + firstproduct.getProductPriceLocal());
            holder.txt_international_price_1.setText(currencysymbol + firstproduct.getProductPriceInternational());
        }

        if (promotion.getProducts().size() > 1) {

            holder.llyt_product_2.setVisibility(View.VISIBLE);

            Product firstproduct = promotion.getProducts().get(1);
            holder.txt_product_name_2.setText(firstproduct.getProductName());
            String currencysymbol = CommonUtils.getCurrencySymbol(promotion.getCurrencyCode());
            holder.txt_local_price_2.setText(currencysymbol + firstproduct.getProductPriceLocal());
            holder.txt_international_price_2.setText(currencysymbol + firstproduct.getProductPriceInternational());


        } else {

            holder.llyt_product_2.setVisibility(View.GONE);
        }

        if (promotion.getProducts().size() > 2) {

            holder.img_show_more.setVisibility(View.VISIBLE);
            holder.img_show_more.setImageResource(isExpanded ? R.drawable.drop_up : R.drawable.drop_down);

        } else {

            holder.img_show_more.setVisibility(View.GONE);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.my_snatchit_product_item_child, null, false);
            ProductChildViewHolder holder = new ProductChildViewHolder(convertView);
            convertView.setTag(holder);
        }

        ProductChildViewHolder holder = (ProductChildViewHolder) convertView.getTag();
        Product childproduct = promotion.getProducts().get(childPosition+2);

        holder.txt_product_name.setText(childproduct.getProductName());
        String currencysymbol = CommonUtils.getCurrencySymbol(promotion.getCurrencyCode());
        holder.txt_local_price.setText(currencysymbol + childproduct.getProductPriceLocal());
        holder.txt_international_price.setText(currencysymbol + childproduct.getProductPriceInternational());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    public class ProductHeaderViewHolder {

        TextView txt_product_name_1, txt_local_price_1, txt_international_price_1, txt_product_name_2, txt_local_price_2, txt_international_price_2;
        FrameLayout flyt_remove;
        ProgressBar pb_remove;
        LinearLayout llyt_product_2;
        ImageView img_show_more;


        public ProductHeaderViewHolder(View itemview) {

            txt_product_name_1 = itemview.findViewById(R.id.txt_product_name_1);
            txt_product_name_2 = itemview.findViewById(R.id.txt_product_name_2);
            txt_local_price_1 = itemview.findViewById(R.id.txt_local_price_1);
            txt_local_price_2 = itemview.findViewById(R.id.txt_local_price_2);
            txt_international_price_1 = itemview.findViewById(R.id.txt_international_price_1);
            txt_international_price_2 = itemview.findViewById(R.id.txt_international_price_2);
            flyt_remove = itemview.findViewById(R.id.flyt_remove);
            pb_remove = itemview.findViewById(R.id.pb_remove);
            llyt_product_2 = itemview.findViewById(R.id.llyt_product_2);
            img_show_more = itemview.findViewById(R.id.img_show_more);

        }
    }


    public class ProductChildViewHolder {

        TextView txt_product_name, txt_local_price, txt_international_price;


        public ProductChildViewHolder(View itemview) {

            txt_product_name = itemview.findViewById(R.id.txt_product_name);
            txt_local_price = itemview.findViewById(R.id.txt_local_price);
            txt_international_price = itemview.findViewById(R.id.txt_international_price);
        }
    }

}

ExpandableListAdapter 视图由于正文大小限制而无法包含。 但它是简单的文本视图,没有太多复杂的。

compile 'net.cachapa.expandablelayout:expandablelayout:2.9.1' 使用此库进行可扩展布局。