Android基础-ExpandableListView二级菜单使用

101 阅读1分钟

ExpandableListView二级菜单的使用:

View:

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gameassist.plugin.input.R;
import com.gameassist.plugin.nativeutils.Logger;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

 @SuppressLint("ViewConstructor")
public class CheatView extends LinearLayout implements View.OnClickListener {
private final Context context;
private DraSticJNI draSticJNI;
private boolean d;
private boolean e;


public CheatView(Context context, DraSticJNI draSticJNI) {
    super(context);
    this.context = context;
    this.draSticJNI = draSticJNI;
    initView();

}

private void initView() {
    inflate(context, R.layout.cheats, this);
    TypeFaceUtils.a(TypeFaceUtils.a(context), this);
    this.d = false;
    this.e = false;
    final ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.cheatpicker);
    CheatListAdapter cVar = new CheatListAdapter(context, expandableListView, getCheats());
    expandableListView.setAdapter(cVar);
    expandableListView.setGroupIndicator(null);
    cVar.a();
    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long j) {
            try {
                CheatFolder bVar = (CheatFolder) expandableListView.getExpandableListAdapter().getGroup(i);
                bVar.cheatFolderExpanded = !expandableListView.isGroupExpanded(i);
                if (!bVar.f) {

                    draSticJNI.setCheatFolderExpanded(bVar.index, bVar.cheatFolderExpanded);

                }
                e = true;
                ImageView imageView = (ImageView) view.findViewById(R.id.iv_expandicon);
                int i2 = bVar.cheatFolderExpanded ? R.drawable.ic_expand_less_white_18dp : R.drawable.ic_expand_less_white_18dp;
                imageView.setBackgroundResource(i2);
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
            return false;
        }
    });
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long j) {
            try {
                CheatListAdapter cVar = (CheatListAdapter) expandableListView.getExpandableListAdapter();
                CheatM aVar = (CheatM) cVar.getChild(i, i2);
                CheatFolder bVar = (CheatFolder) cVar.getGroup(i);
                if (!bVar.cheatFolderMultiSelect) {
                    int i3 = 0;
                    for (CheatM aVar2 : bVar.cheatMList) {
                        if (!aVar2.equals(aVar) && aVar2.cheatEnabled) {
                            aVar2.cheatEnabled = false;
                            draSticJNI.setCheatEnabled(aVar2.index, false);
                            i3++;
                        }
                    }
                    if (i3 > 1) {
                        toast("这个文件夹中的金手指只能同时开启一个。请勿再开启其他的", 1);
                    }
                }
                aVar.cheatEnabled = !aVar.cheatEnabled;

                draSticJNI.setCheatEnabled(aVar.index, aVar.cheatEnabled);

                d = true;
                ((ImageView) view.findViewById(R.id.iv_cheatenabled)).setBackgroundResource(aVar.cheatEnabled ? R.drawable.cheat_1 : R.drawable.cheat_0);
                cVar.notifyDataSetChanged();
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
            return true;
        }
    });
//        ((LinearLayout)   findViewById(R.id.btn_cheats_back)).setOnClickListener(this);
    if (!this.f) {
        ((ImageView) findViewById(R.id.btn_cheats_deselect_all)).setOnClickListener(this);
    }
//        this.b = Controller.getInstance(getApplicationContext());
//        if (this.b != null && f.a(this.b, getApplicationContext())) {
//            this.b.setListener(this, new Handler());
//        }
//        this.c = false;
//        this.a = h.a();
}

@Override
public void onClick(View v) {
    int i = v.getId();
    if (i == R.id.btn_cheats_deselect_all) {
        closeAll();
    }

}

private class CheatFolder {
    public String cheatFolderName;
    public String cheatFolderNote;
    int index;
    public boolean cheatFolderExpanded;
    public boolean cheatFolderMultiSelect;
    public boolean f;
    public List<CheatM> cheatMList;


}


private class CheatM {
    String cheatName;
    String cheatNote;
    int index;
    boolean cheatEnabled;

}


private ArrayList<CheatFolder> getCheats() {
    int i;
    ArrayList arrayList = new ArrayList();
    ArrayList arrayList2 = new ArrayList();
    try {
        int cheatFolderCount = draSticJNI.getCheatFolderCount();
        for (i = 0; i < cheatFolderCount; i++) {
            CheatFolder bVar = new CheatFolder();
            bVar.index = i;
            bVar.cheatMList = new ArrayList();
            bVar.cheatFolderExpanded = draSticJNI.getCheatFolderExpanded(i);
            bVar.f = false;
            bVar.cheatFolderMultiSelect = draSticJNI.getCheatFolderMultiSelect(i);
            bVar.cheatFolderName = draSticJNI.getCheatFolderName(i);
            bVar.cheatFolderNote = draSticJNI.getCheatFolderNote(i);
            if (!bVar.cheatFolderMultiSelect) {
                if (bVar.cheatFolderNote == null) {
                    bVar.cheatFolderNote = "";
                }
                bVar.cheatFolderNote = bVar.cheatFolderNote +
                        " - 只能选择一个";
            }
            arrayList.add(i, bVar);
        }
        i = draSticJNI.getCheatCount();
        for (int i2 = 0; i2 < i; i2++) {
            CheatM aVar = new CheatM();
            aVar.cheatEnabled = draSticJNI.getCheatEnabled(i2);
            aVar.cheatName = draSticJNI.getCheatName(i2);
            aVar.cheatNote = draSticJNI.getCheatNote(i2);
            aVar.index = i2;
            Logger.e("cheatName:" + aVar.cheatName);
            int cheatFolderId = draSticJNI.getCheatFolderId(i2);
            if (cheatFolderId < 0 || cheatFolderId >= cheatFolderCount) {
                arrayList2.add(aVar);
            } else {
                ((CheatFolder) arrayList.get(cheatFolderId)).cheatMList.add(aVar);
            }
        }
        if (arrayList2.size() > 0) {
            CheatFolder bVar2 = new CheatFolder();
            bVar2.index = cheatFolderCount;
            bVar2.cheatMList = arrayList2;
            bVar2.cheatFolderExpanded = false;
            bVar2.cheatFolderMultiSelect = true;
            bVar2.f = true;
            bVar2.cheatFolderName = "匹配的";
            bVar2.cheatFolderNote = null;
            arrayList.add(cheatFolderCount, bVar2);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return arrayList;
}


private void toast(final String str, final int i) {

    Toast.makeText(context, str, i).show();

}

private boolean f;


private final class CheatListAdapter extends BaseExpandableListAdapter {

    private final LayoutInflater layoutInflater;
    private Context context;
    private List<CheatFolder> cheatFolderList;
    private ExpandableListView expandableListView;


    private class Holder {
        public TextView textView1;
        public TextView textView2;
        public ImageView imageView;
    }

    public CheatListAdapter(Context context, ExpandableListView expandableListView, List<CheatFolder> list) {
        this.layoutInflater = LayoutInflater.from(context);
        this.cheatFolderList = list;
        this.expandableListView = expandableListView;
        this.context = context;
    }


    public void a() {
        for (int i = 0; i < getGroupCount(); i++) {
            if (((CheatFolder) this.cheatFolderList.get(i)).cheatFolderExpanded) {
                this.expandableListView.expandGroup(i);
            } else {
                this.expandableListView.collapseGroup(i);
            }
        }
    }

    public Object getChild(int i, int i2) {
        return ((CheatFolder) this.cheatFolderList.get(i)).cheatMList.get(i2);
    }

    public long getChildId(int i, int i2) {
        return (long) i2;
    }

    public View getChildView(int i, int i2, boolean z, View view, ViewGroup viewGroup) {
        Holder aVar;
        CheatM aVar2 = this.cheatFolderList.get(i).cheatMList.get(i2);
        if (view == null) {
            view = layoutInflater.inflate(R.layout.cheat_item, null);
            aVar = new Holder();
            aVar.textView1 = (TextView) view.findViewById(R.id.tw_cheatname);
            aVar.textView2 = (TextView) view.findViewById(R.id.tw_cheatnote);
            aVar.textView1.setTypeface(TypeFaceUtils.a(this.context));
            aVar.textView2.setTypeface(TypeFaceUtils.a(this.context));
            aVar.imageView = (ImageView) view.findViewById(R.id.iv_cheatenabled);
            view.setTag(aVar);
        } else {
            aVar = (Holder) view.getTag();
        }
        aVar.textView1.setText(aVar2.cheatName);
        aVar.textView2.setText(aVar2.cheatNote);
        aVar.imageView.setBackgroundResource(aVar2.cheatEnabled ? R.drawable.cheat_1 : R.drawable.cheat_0);
        return view;
    }

    public int getChildrenCount(int i) {
        return ((CheatFolder) this.cheatFolderList.get(i)).cheatMList.size();
    }

    public Object getGroup(int i) {
        return this.cheatFolderList.get(i);
    }

    public int getGroupCount() {
        return this.cheatFolderList.size();
    }

    public long getGroupId(int i) {
        return (long) i;
    }

    @SuppressLint("InflateParams")
    public View getGroupView(int i, boolean z, View view, ViewGroup viewGroup) {
        View inflate;
        Holder aVar;
        CheatFolder bVar = this.cheatFolderList.get(i);
        if (view == null) {
            inflate = layoutInflater.inflate(R.layout.cheat_folder, null);
            aVar = new Holder();
            aVar.textView1 = (TextView) inflate.findViewById(R.id.tw_cheatfolder);
            aVar.textView2 = (TextView) inflate.findViewById(R.id.tw_cheatfoldernote);
            aVar.textView1.setTypeface(TypeFaceUtils.a(this.context));
            aVar.textView2.setTypeface(TypeFaceUtils.a(this.context));
            aVar.imageView = (ImageView) inflate.findViewById(R.id.iv_expandicon);
            inflate.setTag(aVar);
        } else {
            aVar = (Holder) view.getTag();
            inflate = view;
        }
        aVar.textView1.setText(bVar.cheatFolderName);
        aVar.textView2.setText("");
        if (bVar.cheatFolderNote != null && bVar.cheatFolderNote.length() > 1) {
            aVar.textView2.setText(bVar.cheatFolderNote);
        }
        i = bVar.cheatFolderExpanded ? R.drawable.ic_expand_less_white_18dp : R.drawable.ic_expand_more_white_18dp;
        aVar.imageView.setBackgroundResource(i);
        return inflate;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int i, int i2) {
        return true;
    }
}


public void closeAll() {

    int i;
    int cheatCount = 0;
    try {
        cheatCount = draSticJNI.getCheatCount();

        for (i = 0; i < cheatCount; i++) {
            if (draSticJNI.getCheatEnabled(i)) {
                draSticJNI.setCheatEnabled(i, false);
                this.d = true;
            }
        }
        if (this.d) {
            cheatCount = draSticJNI.getCheatFolderCount();
            for (i = 0; i < cheatCount; i++) {
                draSticJNI.setCheatFolderExpanded(i, false);
            }
//                ExpandableListView expandableListView = (ExpandableListView) findViewById(2131230927);
//                c cVar = new c(this, expandableListView, a());
//                expandableListView.setAdapter(cVar);
//                expandableListView.setGroupIndicator(null);
//                cVar.a();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

布局文件:cheats.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cheats_root"
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="@color/menu_bg_center"
android:orientation="vertical">

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:background="@color/menu_bg_header">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:text="@string/str_ui_setcheats"
            android:textColor="#ffffffee"
            android:textSize="16sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/btn_cheats_deselect_all"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="6dp"
            android:background="@drawable/btn_back_bg"
            android:contentDescription="@null"
            android:visibility="visible"
            android:src="@drawable/ic_select_all_white_36dp" />
    </TableRow>

    <View
        android:layout_height="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:background="#ff666666" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:text="@string/str_gm_cheats_warning"
        android:textColor="#ff000000"
        android:textSize="8sp" />

    <View
        android:layout_height="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:background="#ff666666" />
</TableLayout>

<ExpandableListView
    android:id="@+id/cheatpicker"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/menu_bg_center"
    android:scrollbars="vertical" />
</LinearLayout>

一个cheat_item.xml

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/state_list_bg"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_cheatenabled"
        android:layout_width="16.0dip"
        android:layout_height="16.0dip"
        android:layout_gravity="center_vertical"
        android:layout_margin="6.0dip"
        android:background="@drawable/cheat_0"
        android:contentDescription="@string/empty" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tw_cheatname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1.0dip"
            android:layout_marginRight="6.0dip"
            android:text="@string/empty"
            android:textColor="@color/item_big"
            android:textSize="14.0sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tw_cheatnote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="6.0dip"
            android:layout_marginBottom="1.0dip"
            android:text="@string/empty"
            android:textColor="#ff111111"
            android:textSize="12.0sp"
            android:textStyle="normal" />
    </LinearLayout>
</LinearLayout>

<View
    android:layout_width="fill_parent"
    android:layout_height="1.0dip"
    android:background="#ff666666" />
</LinearLayout>