ExpandableListView

xiaoxiao2021-02-28  95

package com.ycfy.myapplication; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); final ExpandableListAdapter adapter = new BaseExpandableListAdapter() { //设置组视图的图片 int[] logos = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; //设置组视图的显示文字 private String[] generalsTypes = new String[]{"数字", "字母"}; //子视图显示文字 private String[][] generals = new String[][]{ {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}, {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} }; //子视图图片 public int[][] generallogos = new int[][]{ { R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}, { R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}}; //自己定义一个获得文字信息的方法 TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; } //重写ExpandableListAdapter中的各个方法 @Override public int getGroupCount() { // TODO Auto-generated method stub return generalsTypes.length; } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return generalsTypes[groupPosition]; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return generals[groupPosition].length; } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return generals[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LinearLayout ll = new LinearLayout( MainActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); ImageView logo = new ImageView(MainActivity.this); logo.setImageResource(logos[groupPosition]); logo.setPadding(50, 0, 0, 0); ll.addView(logo); TextView textView = getTextView(); textView.setTextColor(Color.BLACK); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LinearLayout ll = new LinearLayout( MainActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); ImageView generallogo = new ImageView( MainActivity.this); generallogo.setImageResource(generallogos[groupPosition][childPosition]); ll.addView(generallogo); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition) .toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } }; ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list); expandableListView.setGroupIndicator(null); expandableListView.setAdapter(adapter); //设置item点击的监听器 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText( MainActivity.this, "你点击了" + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_SHORT).show(); return false; } }); } } <?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="fill_parent" android:orientation="vertical" > <ExpandableListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:cacheColorHint="#00000000" android:listSelector="#00000000" > </ExpandableListView> </LinearLayout>

转载请注明原文地址: https://www.6miu.com/read-52640.html

最新回复(0)