简体   繁体   English

可展开列表视图中的按钮 onclick

[英]Button onclick in expandablelistview

ExpandableListAdapter Hi i need to get onclick event to the button in List_item.xml whein i click on the button. ExpandableListAdapter嗨,我需要在单击按钮时将 onclick 事件发送到 List_item.xml 中的按钮。 im beginning with java and android development.我从 java 和 android 开发开始。 im searching for solution for 3 days我正在寻找解决方案 3 天

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List < String > _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap < String, List < String >> _listDataChild;

public ExpandableListAdapter(Context context, List < String > listDataHeader,
                             HashMap < String, List < String >> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int 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(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.list_item, null);
    }

    TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
    Button btnListChild = (Button) convertView.findViewById(R.id.btn);


    txtListChild.setText(childText);
    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);
    lblListHeader.setText(headerTitle);


    return convertView;
}

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

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

        

list_item.xml - there is the button list_item.xml - 有按钮

    <?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="55dip"
    android:orientation="vertical"
    android:background="@color/colorACD_white"
    >

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="144dp"
        android:layout_height="42dp"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="0dp"
        android:gravity="center"
        android:paddingLeft="25dp"
        android:background="@color/colorACD_white"
        android:textColor="@color/colorACD_black"
        android:textSize="18sp"
        android:fontFamily="@font/sourcesansproregular"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="115dp"
        android:layout_height="45dp"
        android:layout_marginLeft="250dp"
        android:fontFamily="@font/robotoregular"
        android:text="Edit">
    </Button>

    <LinearLayout
        android:id="@+id/line"
        android:layout_width="391dp"
        android:layout_height="10dp"
        android:orientation="horizontal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="popup"
        android:orientation="horizontal" />

</RelativeLayout>

Dialog.xml - the window i want to open when i click on the button from list_item Dialog.xml - 当我单击 list_item 中的按钮时,我想打开 window

    <?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="match_parent">

    <LinearLayout
        android:layout_width="156dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="133dp"
        android:layout_marginTop="297dp"
        android:layout_marginEnd="121dp"
        android:layout_marginBottom="312dp"
        android:background="@color/colorACD_orange" />

</RelativeLayout>

**My Dialog Java class**

    package com.example.application.example;
public class ExampleDialog extends AppCompatDialogFragment {
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Information")
                .setMessage("This is a Dialog")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
        return builder.create();
    }
}

I have added an interface to your adapter.我已经为您的适配器添加了一个接口。 Now from where you are creating this adapter you need to implement the interface ExpandableListAdapterInterFace and the buttonClicked method, you can change the arguments and their types as per your need.现在,您需要从创建此适配器的位置实现接口ExpandableListAdapterInterFacebuttonClicked方法,您可以根据需要更改 arguments 及其类型。

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private final ExpandableListAdapterInterFace _expandableListAdapterInterFace;

    interface ExpandableListAdapterInterFace{
        void buttonClicked(String dataHeader,int position);
    }

    //........ your code............

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData, ExpandableListAdapterInterFace expandableListAdapterInterFace) {
        this._context = context;
        this._expandableListAdapterInterFace=expandableListAdapterInterFace;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    //..............your code .........


    @Override
    public View getChildView(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.list_item, null);
        }

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
        Button btnListChild = (Button) convertView.findViewById(R.id.btn);
        btnListChild.setOnClickListener(v->{
            _expandableListAdapterInterFace.buttonClicked(childText,childPosition);
        });

        txtListChild.setText(childText);
        return convertView;
    }
   //..................your code............
}

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

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