android-自定义ExpandableListview在子级中包含另一个扩展按钮

android-自定义ExpandableListview在子级中包含另一个扩展按钮,第1张

概述我想实现这种类型的列表视图.问题是当我滚动列表时,视图已刷新.任务是显示默认情况下最多可扩展到3个子组的组,当单击”(加号)按钮时,该组的子项将被扩展,新按钮将显示在该组下,以将该组折叠为默认布局.使用plus(”)按钮显示的3个子项目.当任何组的子项大于3时,将显示加号按钮,如果

我想实现这种类型的列表视图.问题是当我滚动列表时,视图已刷新.任务是显示默认情况下最多可扩展到3个子组的组,当单击”(加号)按钮时,该组的子项将被扩展,新按钮将显示在该组下,以将该组折叠为默认布局.使用plus(”)按钮显示的3个子项目.当任何组的子项大于3时,将显示加号按钮,如果子项小于3或等于3,则所有子项将不带加号按钮显示,但如果子项大于3,则显示加号按钮将显示.这里的“ DIAPERS”和“ LAUNDRY DETERGENT”是组名.

当前方案:-如果子位置> 3,然后将文本视图的可见性设置为“ GONE”,将按钮的可见性设置为“ VISIBLE”.但是问题在于,如果子项大于4,并且我单击加号按钮以展开group,则仅显示4个子项5或不显示下一个子项.

如果您需要此代码,请在评论中提出,我将为您提供代码.

解决方法:

首先,您需要为可扩展列表视图的子视图设置一个最小限制.像2或3个孩子一样,第一次会从您的数据库或Web服务中下载或获取.
然后,在该限制变量上,您可以将可扩展列表视图限制为仅显示前2和3个子项.

然后,使用子限制变量,您还需要一个标志变量,其中包含以下信息:“如果列表包含3个以上的孩子,则值为1;如果列表包含3个或少于3个孩子,则值为0”.

在flag变量的值上,您可以在Android中设置加号按钮的隐藏值和可见值.

对于加号按钮,我认为下面给出的代码会有所帮助.单击后,只需再次调用数据库并获取所有子项并显示它们,然后刷新可扩展列表视图即可.

public class ProductListingExpandableAdapter extends Baseexpandablelistadapter {    public String TAG = ProductListingExpandableAdapter.class.getSimplename();    private Context _context;int clickedposition;    private List<String> _ListDataheader; // header Titles  child data in format of header Title, child Title    private HashMap<String, ArrayList<String>> _ListDataChild;    ArrayList<String> categoryID;    String stateID,countryID;    ArrayList<ProductDataBean> ProductList; ArrayList<ProductListingdisplayCheck> checkArrayList;    int _ListSize;String user_ID;    ProductDataBean bean;    /* Variable to do lazy loading of images */    Handler handler;    Runnable runnable;    /* array List to hold data */    ArrayList<String> BrandList;         ImageLoader    imageLoader;         private        displayImageOptions options;         Activity a;    String RetailerImageUrl,BrandImageUrl;    public ProductListingExpandableAdapter(Context context, List<String> ListDataheader, HashMap<String, ArrayList<String>> ListChildData,            int size,ArrayList<ProductDataBean> ProductList,ArrayList<ProductListingdisplayCheck> checkArrayList,ArrayList<String> categoryID,  String user_ID,String stateID,String countryID)     {        this._context        =  context;        this._ListDataheader =  ListDataheader;        this._ListDataChild  =  ListChildData;                                       this.categoryID      =  categoryID;            this.checkArrayList  =  checkArrayList;          this._ListSize       =  size;        this.ProductList     =  ProductList;        this.checkArrayList  =  checkArrayList;        this.user_ID         =  user_ID;        this.countryID       =  countryID;        this.stateID         =  stateID;         options = new displayImageOptions.Builder()              .showImageForEmptyUrl(R.drawable.thumb_demo).cacheInMemory()              .cacheOndisc().build();              imageLoader = ImageLoader.getInstance();                    Log.d("....return the  the event image loader class...==", ""+imageLoader.getClass());    }    @OverrIDe    public Object getChild(int groupposition, int childPosititon)     {           //Log.i("Object getChild",String.valueOf(this._ListDataChild.get(this._ListDataheader.get(groupposition)).get(childPosititon)));            return this._ListDataChild.get(this._ListDataheader.get(groupposition)).get(childPosititon);            }    @OverrIDe    public long getChildID(int groupposition, int childposition)     {           return childposition;    }    @OverrIDe    public VIEw getChildVIEw(final int groupposition, final int childposition,boolean isLastChild, VIEw convertVIEw, VIEwGroup parent)     {                       final String childText = (String) getChild(groupposition, childposition);        if (convertVIEw == null)         {                       LayoutInflater infalinflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            convertVIEw = infalinflater.inflate(R.layout.custom_brand_retailer_layout_new, null);                           }        relativeLayout ParentLayout=  (relativeLayout) convertVIEw.findVIEwByID(R.ID.parentLayout);                 /* Retailer Image*/        ImageVIEw retailerImage    =  (ImageVIEw) convertVIEw.findVIEwByID(R.ID.retailerImage);             /* Brand Image */        ImageVIEw brandImage       =  (ImageVIEw) convertVIEw.findVIEwByID(R.ID.brandImage);                /* Product PricePerUnit */        TextVIEw pricePerUnit      =  (TextVIEw) convertVIEw.findVIEwByID(R.ID.pricePerItem);               /* Product PricePerUnit */        TextVIEw packetPrice       =  (TextVIEw) convertVIEw.findVIEwByID(R.ID.pricePerPacket);             /* Product name */        TextVIEw productname       =  (TextVIEw) convertVIEw.findVIEwByID(R.ID.Productname);        /* Group close Images */         relativeLayout addMore     =  (relativeLayout) convertVIEw.findVIEwByID(R.ID.addMore);        /* Minus button Image */        ImageVIEw minusItems       =  (ImageVIEw) convertVIEw.findVIEwByID(R.ID.minusItems);                /* Minus button Image */        ImageVIEw plusItems        =  (ImageVIEw) convertVIEw.findVIEwByID(R.ID.plusItems);          try   {              JsONObject jObject = new JsONObject(childText);                        // Log.i("jObject",String.valueOf(jObject));                pricePerUnit.setText ("$"+jObject.getString("pricePerItem"));                packetPrice .setText ("$"+jObject.getString("product_price"));                                  String itemnameString   =   "";                String Title            =   jObject.getString("product_name");                if (Title.length()  >  44)                 {                    itemnameString = Title.substring(0, 45)+"...";                }                 else                 {   itemnameString = Title;                                 }                productname.setText(itemnameString);                                RetailerImageUrl = jObject.getString("retailer_image_url_small");                BrandImageUrl    = jObject.getString("brand_image_url");                // String RetailerImageUrl = jObject.getString("pricePerItem");            } catch (JsONException e)             {                               e.printstacktrace();            }     //=========================================================================================    // Log.e("Pagination ArrayList size", String.valueOf(Constants.Paginationposition.size()));    String PaginationPos               =         Constants.Paginationposition.get(groupposition);    Log.e ("PaginationPos", String.valueOf(PaginationPos));    // Log.e("is last child", String.valueOf(isLastChild));      /*   HIDe or Show Group Close option   */          Log.e("Pagination ArrayList size", String.valueOf(Constants.Paginationposition.size()));        if(PaginationPos.equals("1") && childposition == 2 && isLastChild == true )         {             addMore.setVisibility(VIEw.VISIBLE); minusItems.setVisibility(VIEw.GONE ); plusItems.setVisibility(VIEw.VISIBLE);         }         else if(PaginationPos.equals("0") && childposition > 2 && isLastChild == true  )                                                            {             addMore.setVisibility(VIEw.GONE);                   }         else if(PaginationPos.equals("2") && childposition > 2 && isLastChild == true  )                                                      {              plusItems.setVisibility(VIEw.GONE);              addMore.setVisibility  (VIEw.VISIBLE);              minusItems.setVisibility(VIEw.VISIBLE);              minusItems.setVisibility(VIEw.VISIBLE);              minusItems.setimageResource(R.drawable.minus);         }         else          {            addMore.setVisibility(VIEw.GONE);         }         //==================================================================================        minusItems.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Log.e("Constants.ListDataheader.get(groupposition)", Constants.ListDataheader.get(groupposition));                String keyword = Constants.ListDataheader.get(groupposition);                    String alternate = ProductListingExpandableListVIEwActivity.demoJsonObjectTesting;                    // check whether the List for keyword is present                    ArrayList<String> alternateList = _ListDataChild.get(keyword);                    if(alternateList == null)                      {                         Log.i(TAG, "List is null");                        /* alternateList = new ArrayList<String>();                          _ListDataChild.put(keyword, alternateList);           */                              }                    else                    {                        Constants.Paginationposition.set(groupposition, "1");                        ArrayList<String> newList = new ArrayList<String>();                        int size = alternateList.size();                             Log.e("alternateList size", String.valueOf( alternateList.size()));                                for(int i=0;i<3;i++)                                {                                    newList.add(alternateList.get(i));                                                                  }                                alternateList.clear();                                for(int i=0;i<3;i++)                                {                                alternateList.add(newList.get(i));                                }                          Log.i("alternate List size",String.valueOf( alternateList.size()));                        ProductListingExpandableAdapter.this.notifyDataSetChanged();                        //ProductListingExpandableAdapter.this.notifyDataSetInvalIDated();                        /*Intent showSearchResult = new Intent(_context,ProductListingExpandableListVIEwActivity.class);                        showSearchResult.putExtra("ShowSeachResult", "2");                        _context.startActivity(showSearchResult);                        ((Activity)_context).finish();                           Apply our splash exit (fade out) and main entry (fade in) animation Transitions.                         ((Activity)_context). overrIDePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);*/                    }            }        });        addMore.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Log.i("addMore images List clicked", "addMore image clicked");                //Clicked postion of group                clickedposition = groupposition;                   String keyword = Constants.ListDataheader.get(groupposition);                Log.i("keyword", keyword);                for( int ii = 0;ii<Constants.ListDataheader.size();ii ++)                {                    String currentKeyword = Constants.ListDataheader.get(ii);                    if(currentKeyword.equals(keyword)==false)                    {                                      // check whether the List for keyword is present                        ArrayList<String> alternateList = _ListDataChild.get(currentKeyword);                        if(alternateList == null)                          {                             Log.i(TAG,Constants.ListDataheader.get(groupposition)+ " List is null");                             /*alternateList = new ArrayList<String>();                              _ListDataChild.put(keyword, alternateList);       */                                      }                        else                        {                               if(alternateList.size()>2)                            {                             Constants.Paginationposition.set(ii, "1");                             Log.i(TAG,Constants.ListDataheader.get(groupposition)+ "insIDe else");                            ArrayList<String> newList = new ArrayList<String>();                            int size = alternateList.size();                                 Log.e("alternateList size", String.valueOf( alternateList.size()));                                    for   (int i=0; i<3;i++)                                    {                                        newList.add(alternateList.get(i));                                                                      }                                                                          alternateList.clear();                                    for  (int j=0; j<3; j++)                                    {                                       alternateList.add(newList.get(j));                                    }                                    Log.i("alternate List size",String.valueOf( alternateList.size()));                                             }}                    }                }                /*  Calling Json webservices  */                new LoadProductData(_context,groupposition).execute();            }        });        /*   Lazy loading class method  for loading Retailer images  */             imageLoader.init(ImageLoaderConfiguration.createDefault(_context));             if(RetailerImageUrl.equals("no image"))             {                 retailerImage.setBackgroundResource(R.drawable.no_img);             }             else             {                 imageLoader.displayImage(RetailerImageUrl,retailerImage,                        options, new ImageLoadingListener() {                            @OverrIDe                            public voID onl oadingComplete() {}                            @OverrIDe                            public voID onl oadingFailed() {}                            @OverrIDe                            public voID onl oadingStarted() {}                        });             }                int SDK_INT = androID.os.Build.VERSION.SDK_INT;                 if (SDK_INT>=16)                 {                     if(BrandImageUrl.equals("no image")==false)                     {                         Drawable Branddrawable= Loadimage(BrandImageUrl);                         brandImage.setBackground(Branddrawable);                     }                 }                 else                 {                     if(BrandImageUrl.equals("no image")==false)                     {                         Drawable Branddrawable= Loadimage(BrandImageUrl);                         brandImage.setBackgroundDrawable(Branddrawable);                     }            }        return convertVIEw;    }    @OverrIDe    public int getChildrenCount(int groupposition)     {        return this._ListDataChild.get(this._ListDataheader.get(groupposition)).size();    }    @OverrIDe    public Object getGroup(int groupposition)     {        return this._ListDataheader.get(groupposition);    }    @OverrIDe    public int getGroupCount()     {        return this._ListDataheader.size();    }    @OverrIDe    public long getGroupID(int groupposition)     {        return groupposition;    }    @OverrIDe    public VIEw getGroupVIEw(int groupposition, boolean isExpanded,VIEw convertVIEw, VIEwGroup parent)     {        String headerTitle = (String) getGroup(groupposition);        if  (convertVIEw == null)       {            LayoutInflater infalinflater    =   (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            convertVIEw   = infalinflater.inflate (R.layout.List_group,  null);        }        TextVIEw lblListheader =     (TextVIEw) convertVIEw.findVIEwByID(R.ID.lblListheader);        lblListheader.setTypeface    (null, Typeface.BolD);        lblListheader.setText        (headerTitle);            ExpandableListVIEw mExpandableListVIEw = (ExpandableListVIEw) parent;            mExpandableListVIEw.expandGroup(groupposition);        return convertVIEw;    }    @OverrIDe    public boolean hasStableIDs()     {        return false;    }    @OverrIDe    public boolean isChildSelectable(int groupposition, int childposition) {        return true;    }    // Background async  task    /* State/Province List background thread */    class LoadProductData extends AsyncTask<VoID, VoID, VoID> {        private ProgressDialog dialog;String response;        Context context;int GroupPos;        private JsONArray Jsonarray, stateJsonArray;        public LoadProductData(Context context,int GroupPos) {            super();            this.GroupPos   = GroupPos;             this.context = context;        }        @OverrIDe        protected voID onPreExecute()  {            super.onPreExecute();            dialog = ProgressDialog.show(context, "","Please wait...", true, true);dialog.show();            Log.e("Adapter pre execute ", "in the pre-execute loop");        }        @OverrIDe        protected VoID doInBackground(VoID... params) {            try {                //Constants.ListDataChild.clear();Constants.ListDataheader.clear();Constants.productListing.clear();                Log.e(TAG  , "in the background-execute loop");                UserFunctions userFunctions = new UserFunctions();                String categoryID = String.valueOf(Constants.categoryID.get(GroupPos));     Log.e("categoryID"  , Constants.categoryID.get(GroupPos));                JsONObject categoryJson = userFunctions.SinglecategoryListRequest(categoryID,user_ID,stateID,countryID); // Log.i("Product lisiting Json Array",String.valueOf(categoryJson));                String result = categoryJson.getString("result");                 Log.i("result",result);                if(result.equals("no records found"))                {                    response = "no records found";                }                else                {                    response = "record found";                // SearchResult refers to the current element in the array                // "search_result"                JsONObject questionMark = categoryJson.getJsONObject("result");                Iterator keys = questionMark.keys();                ProductListingdisplayCheck addCheck;                 int i = 0;                while (keys.hasNext()) {                    // Loop to get the dynamic key                    String currentDynamicKey = (String) keys.next();    // Log.i("current Dynamic key",                                                                        // String.valueOf(currentDynamicKey));                    ArrayList<String>   BrandList = new ArrayList<String>();                    // Get the value of the dynamic key                    JsONObject currentDynamicValue = questionMark.getJsONObject(currentDynamicKey); // Log.i("current Dynamic Value"+String.valueOf(i),                    String product_List = currentDynamicValue.getString("product_Listing"); // Log.i("product_Listing",String.valueOf(product_List));                    addCheck = new ProductListingdisplayCheck();                    addCheck.setCheckStatus(0);                    checkArrayList . add(addCheck);                    Log.i("checkArrayList size",String.valueOf(checkArrayList.size()));                    JsONArray product_Listing = currentDynamicValue.getJsONArray    ("product_Listing");                     BrandList = Constants.ListDataChild.get(currentDynamicKey);    Log.i("BrandList size", String.valueOf(BrandList.size()));                     BrandList.clear();                    for (int ii = 0; ii < product_Listing.length(); ii++)                            {                        JsONObject Jsonobject = product_Listing.getJsONObject(ii);                        String JsonObjectString = String.valueOf(Jsonobject);                        if ( BrandList == null )                          {                            BrandList = new ArrayList<String>();                            Constants.ListDataChild.put(currentDynamicKey, BrandList);                        }                                               BrandList.add(JsonObjectString);                                            }                    //HashMap<String, ArrayList<String>> _ListDataChild = null;                     /* String keyword = "Wipes";                        String alternate = ProductListingExpandableListVIEwActivity.demoJsonObjectTesting;                        // check whether the List for keyword is present                        ArrayList<String> alternateList = _ListDataChild.get(keyword);                        if(alternateList == null)  {                             alternateList = new ArrayList<String>();                            _ListDataChild.put(keyword, alternateList);                        }                        alternateList.add(ProductListingExpandableListVIEwActivity.demoJsonObjectTesting);                        */                    Constants.Paginationposition.set(GroupPos, "2");                    Constants.ListDataChild.put(Constants.ListDataheader.get(clickedposition), BrandList);                    Log.i("hash map size", String.valueOf(Constants.ListDataChild.size()));                    /* Update the value of position */                    i++;                 }                }              }             catch (Exception e)             {                e.printstacktrace();            }            return null;        }        @OverrIDe        protected voID onPostExecute(VoID result) {            super.onPostExecute(result);            dialog.dismiss();        if(response.equals("no records found"))         {            Toast.makeText(_context, "No Record Found.", 500).show();        }        else        {            ProductListingExpandableAdapter.this.notifyDataSetChanged();            /*Intent showSearchResult = new Intent(_context,ProductListingExpandableListVIEwActivity.class);            showSearchResult.putExtra("ShowSeachResult", "2");            _context.startActivity(showSearchResult);            ((Activity)context).finish();               Apply our splash exit (fade out) and main entry (fade in) animation Transitions.             ((Activity)context). overrIDePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);*/        }    }        @OverrIDe        protected voID onProgressUpdate(VoID... values) {            super.onProgressUpdate(values);        }    }    private Drawable Loadimage(String url)    {      try    {        inputStream is = (inputStream) new URL(url).getContent();        Drawable d = Drawable.createFromStream(is, "src name");        return d;    }          catch (Exception e) {      // tv.setText("Exc="+e);       return null;    }    }    }
总结

以上是内存溢出为你收集整理的android-自定义ExpandableListview在子级中包含另一个扩展按钮全部内容,希望文章能够帮你解决android-自定义ExpandableListview在子级中包含另一个扩展按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1076963.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存