草庐IT

android - 列表没有第二次展开/折叠

coder 2023-12-22 原文

问题:当我点击 Custom/Random 时,什么也没有发生。 (请参阅下面的屏幕截图)。

我使用 this 成功实现了 ExpandListView例如,但在我的例子中,数据来自数据库,这只是区别。我调试我的代码,它进入 expandAll() 并且当我运行应用程序时,它默认带有扩展列表。

我当前代码的屏幕截图,实际上,我在对话框中使用 expandeListview 并使用 expandlistadapter 扩展行

我的代码: ExpandableCategoryAdapter:

public class ExpandableCategoryAdapter extends BaseExpandableListAdapter {
    private static final String TAG = ExpandableCategoryAdapter.class.getSimpleName();
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private Context context;
    private List<CategoryHeader> originalList;
    private List<CategoryHeader> headerList;
    private HamburgerMenuListener menuInterface;

    public ExpandableCategoryAdapter(Context context, List<CategoryHeader> generalList, HamburgerMenuListener menuInterface) {
        this.context = context;
        this.headerList = generalList;
        this.originalList = generalList;
        this.menuInterface = menuInterface;
    }

    private boolean isPositionHeader(int position) {
        return position == 0 || position == 6;
    }


    private CategoryHeader getItem(int position) {
        return originalList.get(position);
    }

    private void showLog(String msg) {
        Log.d(TAG, msg);
    }

    @Override
    public int getGroupCount() {
        return headerList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<CustomCategory> countryList = headerList.get(groupPosition).getCategoryList();
        return countryList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return headerList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<CustomCategory> countryList = headerList.get(groupPosition).getCategoryList();
        return countryList.get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
        CategoryHeader categoryHeader = (CategoryHeader) getGroup(groupPosition);
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.row_custom_category_list, null);
        }

        TextView heading = view.findViewById(R.id.header_view);
        heading.setText(categoryHeader.getHeaderName().trim());

        return view;
    }

    public void filterData(String query) {

        query = query.toLowerCase();
        headerList.clear();

        if (query.isEmpty()) {
            headerList.addAll(originalList);
        } else {

            for (CategoryHeader categoryHeader : originalList) {

                List<CustomCategory> countryList = categoryHeader.getCategoryList();
                List<CustomCategory> newList = new ArrayList<CustomCategory>();
                for (CustomCategory customCategory : countryList) {
                    if (customCategory.getName().toLowerCase().contains(query)) {
                        newList.add(customCategory);
                    }
                }
                if (newList.size() > 0) {
                    CategoryHeader nContinent = new CategoryHeader(categoryHeader.getHeaderName(), newList);
                    headerList.add(nContinent);
                }
            }
        }

        notifyDataSetChanged();

    }

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


        CustomCategory customCategory = (CustomCategory) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.row_general_list, null);
        }

        TextView name = convertView.findViewById(R.id.tv_category_item);


        if (customCategory != null && customCategory.getName() != null) {
            name.setText(customCategory.getName().trim());
        }


        return convertView;
    }

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

    public class MyViewHolderItem extends RecyclerView.ViewHolder {
        private TextView textViewItem;
        private ImageView imageViewIcon;
        private ImageView hamburgerMenu;
        private Button customImageViewIcon;

        public MyViewHolderItem(View itemView) {
            super(itemView);
            textViewItem = itemView.findViewById(R.id.tv_category_item);
            imageViewIcon = itemView.findViewById(R.id.iv_category_icon);
            customImageViewIcon = itemView.findViewById(R.id.iv_custom_category_icon);
            hamburgerMenu = itemView.findViewById(R.id.hamburger_menu);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //          menuInterface.onClickListItem(originalList.get(getAdapterPosition()).getCustCategoryId());
                }
            });

            hamburgerMenu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (menuInterface != null) {
                        //          menuInterface.onClickHamburger(originalList.get(getAdapterPosition()).getCustCategoryId());
                    }
                }
            });
        }

    }

    public class MyViewHolderHeader extends RecyclerView.ViewHolder {
        private TextView headerView;

        public MyViewHolderHeader(View itemView) {
            super(itemView);
            headerView = itemView.findViewById(R.id.header_view);
        }
    }
}

类别对话框:

public class CategoryDialog extends BaseClass implements View.OnClickListener, Callback<String>, HamburgerMenuListener, ResultListener, SearchView.OnQueryTextListener, SearchView.OnCloseListener {
    private static final String TAG = CategoryDialog.class.getSimpleName();
    int i = 0;
    private String landlineName, getProviderName, defaultName = "", phoneNum, customCategoryName, selected = "", getConsumerNum, getAccountNum, getOwnerName;
    private long tempId = 0, categoryId, getCustomCategoryId, providerId, customCategoryId = 0, subProviderId;
    private boolean stop = true, insurance, isEditPayment = false, isDeletedSQLite = false, isDeletedServer = false, isClicked = false;
    private ExpandableCategoryAdapter expandableCategoryAdapter;
    private List<CustomCategory> categories, customCategories;
    private RecyclerView dialogRecyclerView;
    private ExpandableListView expandableListView;
    private DatabaseAdapter dbAdapter;
    private Context context;
    private SharedPreferences profilePreference;
    private View promptsView;
    private FloatingActionButton fab;
    private CustomCategory customCategory;
    private List<Reminder> dialogListItems;
    private ImageView info;
    private DialogListAdapter dialogListAdapter;
    private Activity activity;
    private ProviderDialog providerDialog;
    private TextView inputInsuranceProvider, textViewError, inputBillProvider, errorView, information, subProviderError, providerError, customProviderError, consumerError, ownerError;
    private EditText userInput, inputConsumerNumber, name, inputAccountNumber, inputCustomProvider;
    private ProvidersInfo providersInfo;
    private AlertDialog informationDialog, mDialog;
    private CategoryListener categoryListener;
    private General provider, subProvider;
    private RelativeLayout relativeProvider, subProviderLayout, accountLayout, customLayout;
    private LinearLayout spinnerLayout;
    private List<General> mainInsuranceList = new ArrayList<>();
    private CustomSpinnerAdapter spinnerAdapter;
    private CustomSpinnerClass spinInsuranceList;
    private ArrayList<CategoryHeader> headerArrayList = new ArrayList<CategoryHeader>();
    private CategoryHeader categoryHeader;

    public CategoryDialog(Context context, Activity activity) {
        super(context, activity);
        this.activity = activity;
        this.context = context;
    }

    private void init() {
        categories = new ArrayList<>();
        customCategories = new ArrayList<>();
        dbAdapter = RemindMe.getInstance().adapter;
        dialogListItems = new ArrayList<>();
        profilePreference = context.getSharedPreferences(PROFILE, MODE_PRIVATE);
        providerDialog = new ProviderDialog(context);
        providerDialog.setResultListener(this);
        getDataFromSharedPref();
    }

    private void loadSomeData() {
        categoryHeader = new CategoryHeader("Custom", customCategories);
        headerArrayList.add(categoryHeader);

        categoryHeader = new CategoryHeader("Random", categories);
        headerArrayList.add(categoryHeader);

        categoryHeader = new CategoryHeader("General", categories);
        headerArrayList.add(categoryHeader);
    }

    public void setCategoryListener(CategoryListener listener) {
        this.categoryListener = listener;
    }

    private void setClickListener() {
        fab.setOnClickListener(this);
    }

    public void showCategoryDialog() {
        LayoutInflater li = LayoutInflater.from(context);
        promptsView = li.inflate(R.layout.row_category_dialog_layout, null);
        init();
        findViewById();
        setClickListener();
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setView(promptsView);

        alertDialogBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        recyclerView();
        mDialog = alertDialogBuilder.create();
        mDialog.setCancelable(false);

        mDialog.getWindow().setBackgroundDrawableResource(R.color.colorWhite);
        mDialog.show();
    }

    private void findViewById() {
        expandableListView = promptsView.findViewById(R.id.expandableList);
        fab = promptsView.findViewById(R.id.fab);
    }

    private void recyclerView() {
        addToCategories();
        loadSomeData();
        showLog("headerArrayList: " + headerArrayList.size());
        //expandableCategoryAdapter = new ExpandableCategoryAdapter(context, categories, this);
        expandableCategoryAdapter = new ExpandableCategoryAdapter(context, headerArrayList, this);
        //       expandableListView.setHasFixedSize(true);
       /* final LinearLayoutManager mLayoutManager;
        mLayoutManager = new LinearLayoutManager(context);
        expandableListView.setLayoutManager(mLayoutManager);
        expandableListView.setItemAnimator(new DefaultItemAnimator());*/
        try {
            expandableListView.setAdapter(expandableCategoryAdapter);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        showLog("1");
        expandAll();
      listener();
    //      expandableCategoryAdapter.notifyDataSetChanged();
    //     expandableCategoryAdapter.refresh(headerArrayList);
}

private void listener() {
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            for (int i = 0; i < headerArrayList.size(); i++) {
                if (i != groupPosition) {
                    expandableListView.expandGroup(i);
                }

            }
        }
    });
}private void expandAll() {
        showLog("2");
        int count = expandableCategoryAdapter.getGroupCount();
        showLog("3 :"+ count);
        for (int i = 0; i < count; i++) {
            showLog("4");
            expandableListView.expandGroup(i);
        }
    }

最佳答案

public class Class******Adapter extends BaseExpandableListAdapter
{
private Context context;
private List<ModelClassObj> ModelClassObjs;

public ExpandableVocherDetailsAdapter(Context context,List<ModelClassObj> 
ModelClassObjs)
{
this.context = context;
this.ModelClassObjs = ModelClassObjs;
}

public void refresh(List<ModelClassObj> ModelClassObjs)
{
    this.ModelClassObjs   = ModelClassObjs;
    notifyDataSetChanged();
}


@Override
public boolean isChildSelectable(int i, int i1) {
    return false;
}

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

@Override
public Object getChild(int i, int i1) {
    return null;
}

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

@Override
public Object getGroup(int i) {
    return null;
}

@Override
public long getChildId(int i, int i1) {
    return 0;
}

@Override
public int getChildrenCount(int i) {
    return 1;
}

@Override
public int getGroupCount() {
    if(ModelClassObjs!=null && ModelClassObjs.size()>0)
        return ModelClassObjs.size();
    return 0;
}

@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup)
{
    ModelClassObj  obj  = ModelClassObjs.get(i);

    EditText tvCreatedBy,tvCreatedOn,tvDescription,tvVoucherNumber,tvcancelReason,tvcancelBy,tvCancelApproveBy,tvCancelApproveOn,tvdocph,tvdocemail;
    LinearLayout tvCancelReasonHed,lldocph_email,llcancel_appro;

    view  =  LayoutInflater.from(context).inflate(R.layout.exp_vocher_child,null);

    tvCreatedBy     = view.findViewById(R.id.tvCreatedBy);

    tvCreatedBy.setText(obj.getCreatedBy());

    return view;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
    final ModelClassObj  obj  = ModelClassObjs.get(i);

    TextView tvDoctorName,tvvpAmnt,tv_vp_payment,tv_vp_ReceipientType;
    ImageView ivIndicator;
    LinearLayout llParent;

    view = 
LayoutInflater.from(context).inflate(R.layout.exp_vocher_parent_cell,null);

    tvDoctorName             = view.findViewById(R.id.tvDoctorName);


    tvDoctorName.setText(obj.getDoctorName());

    if(b==true)
        ivIndicator.setImageResource(R.drawable.up);
    else
        ivIndicator.setImageResource(R.drawable.down);

    return view;
}
}

Activity 类

expandableLisView.setOnGroupExpandListener(new 
ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            for (int i = 0; i < voucherDetailsObjs.size(); i++) {
                if (i != groupPosition) {
                    exlvVocherDetail.collapseGroup(i);
                }
            }
        }
    });

关于android - 列表没有第二次展开/折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56881534/

有关android - 列表没有第二次展开/折叠的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  3. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  6. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  7. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  8. ruby - 没有类方法获取 Ruby 类名 - 2

    如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象

  9. ruby - 没有轨道的 ActiveRecord 时区 - 2

    我在非Rails项目中使用ActiveRecord。在Rails中,我可以这样做:config.time_zone='EasternTime(US&Canada)'config.active_record.default_timezone='EasternTime(US&Canada)'但如果我不使用rails,我该如何设置时区? 最佳答案 ActiveRecord::Base.default_timezone='EasternTime(US&Canada)' 关于ruby-没有轨道的A

  10. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

随机推荐