简体   繁体   English

如何更改 ExpandableListView 子项中按钮的可见性?

[英]How to change the visibility of a button in subitem in ExpandableListView?

My code has an ExpandableListView and each child (schedule) can be deleted via a delete button in the child.我的代码有一个 ExpandableListView,每个子项(计划)都可以通过子项中的删除按钮删除。 I want to use a functionality of "Edit" TextView so that if the user clicks on "Edit" the Delete Buttons will be visible.我想使用“编辑”TextView 的功能,这样如果用户单击“编辑”,删除按钮就会可见。 I tried it on my custom adapter MainAdapter but gives me crashes.我在我的自定义适配器 MainAdapter 上尝试了它,但让我崩溃了。 Could anyone help me?谁能帮帮我?

MainAdapter.java

package com.example.easyplan;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

public class MainAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<String> listPlan; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> listScheduleMap;
    ImageButton scheduleDeleteBtn;

    public MainAdapter(Context context, List<String> listDataHeader,
                       HashMap<String, List<String>> listChildData) {
        this._context = context;
        this.listPlan = listDataHeader;
        this.listScheduleMap = listChildData;
    }
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.listScheduleMap.get(this.listPlan.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.schedule_group, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.scheduleGroup);
        scheduleDeleteBtn = convertView.findViewById(R.id.scheduleDeleteBtn);
        scheduleDeleteBtn.setVisibility(View.INVISIBLE);
        scheduleDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> plan = listScheduleMap.get(listPlan.get(groupPosition));
                plan.remove(childPosition);
                notifyDataSetChanged();
            }
        });
        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.listScheduleMap.get(this.listPlan.get(groupPosition))
                .size();
    }
    @Override
    public Object getGroup(int groupPosition) {
        return this.listPlan.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.listPlan.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.plan_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.planGroup);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

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

    public void setDeleteVisibility(boolean visible){
        for(int j = 0; j < getGroupCount(); j ++){
            for(int i = 0; i<getChildrenCount(i); i++ ){
                if(visible){

                    scheduleDeleteBtn.setVisibility(View.VISIBLE);
                }else{
                    scheduleDeleteBtn.setVisibility(View.INVISIBLE);
                }
            }
        }
    }
}

PlansFragment.java

package com.example.easyplan;


import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 * A simple {@link Fragment} subclass.
 */
public class PlansFragment extends Fragment {
    TextView editText;
    MainAdapter mainAdapter;
    ExpandableListView planExplandable;
    List<String> listPlanName;
    HashMap<String, List<String>> listSchedules;
    TextView textView;
    static int count = 0;
    View view;

    public PlansFragment() {

        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_plans_fragment, container, false);
        listPlanName = new ArrayList<String>();
        listSchedules = new HashMap<String, List<String>>();

        // get the listview
        planExplandable = (ExpandableListView) view.findViewById(R.id.plansExpandable);
        textView = view.findViewById(R.id.scheduleGroup);
        editText = view.findViewById(R.id.planEditText);


        // preparing list data
       // prepareListData();
        createAPlan(3);
        createAPlan(1);
        createAPlan(3);
        createAPlan(4);

        mainAdapter = new MainAdapter(getActivity(), listPlanName, listSchedules);

        // setting list adapter
        planExplandable.setAdapter(mainAdapter);
        //mainAdapter.setDeleteVisibility(false);
 /*       editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainAdapter.setDeleteVisibility(true);
            }
        });*/
        return view;
    }

    public HashMap<String, String> getData(){

        return null;
    }

    private void createAPlan(int sch){
        listPlanName.add("Plan#"+(count+1));
        List<String> schedules = new ArrayList<String>();
        if(sch > 0 ){
            for (int i = 1; i <= sch; i++){
                schedules.add("Schedule#" + i);
            }
        }
        listSchedules.put(listPlanName.get(count),schedules);
        count++;

    }
}

The child's xml has孩子的xml有

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1.8">

    <TextView
        android:id="@+id/scheduleGroup"
        android:layout_width="355dp"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textColor="@android:color/black" />
</LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.2">

        <ImageButton
            android:id="@+id/scheduleDeleteBtn"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/delete_icon"
            android:visibility="visible"
            android:clickable="true"
            ></ImageButton>
    </LinearLayout>

Inside your fragment use setOnItemClickListener() method on your listView and in overriden method make use of getFirstVisiblePosition() to subtract from the position where you will find the position of that view.在您的片段中,在您的 listView 上使用 setOnItemClickListener() 方法,并在覆盖方法中使用 getFirstVisiblePosition() 从 position 中减去,您将在其中找到该视图的 position。 then set the visibility.然后设置可见性。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listPosition = position - planExplandable.getFirstVisiblePosition();
if (planExplandable.getChildAt(listPosition).findViewById(R.id.yourview).getVisibility() == View.INVISIBLE) {
    //Write your logic here
    planExplandable.getChildAt(listPosition).findViewById(R.id.yourview).setVisibility(View.VISIBLE);

}

} }

If this won't help then please paste your crash log.如果这没有帮助,请粘贴您的崩溃日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM