在可展开的列表视图中添加可单击的图像视图



我在 android 中使用 expandableListView 时遇到了问题(我使用 android studio)。我在网上找到了一个关于 expandableListVIew 的教程,一切正常,但现在我想在每个 ListGroup 中添加一个可点击的图像。

在每个组中都添加了一个小修改图标,但现在当用户单击它时,我真的不知道如何在主程序中执行某些操作。

主活动.java

package com.luca.mattia.password;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
Boolean aperto[] = new Boolean[500];
int img_cont = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    for(int i = 0; i < 500; i ++)
    {
        aperto[i] = false;
    }
    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);
    // preparing list data
    prepareListData();
    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
    // setting list adapter
    expListView.setAdapter(listAdapter);
    //listener for child row click
    expListView.setOnChildClickListener(myListItemClicked);
    expListView.setOnGroupClickListener(myListGroupClicked);
}
/*
 * Preparing the list data
 */
private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();
    // Adding child data
    listDataHeader.add("uno");
    listDataHeader.add("due");
    listDataHeader.add("tre");
    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("E-mail: e-mail");
    top250.add("Username: un");
    top250.add("Password: QWERTY");
    top250.add("Chiave: __________");
    add_img();
    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("The Conjuring");
    nowShowing.add("Despicable Me 2");
    nowShowing.add("Turbo");
    nowShowing.add("Grown Ups 2");
    nowShowing.add("Red 2");
    nowShowing.add("The Wolverine");
    add_img();
    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");
    comingSoon.add("The Smurfs 2");
    comingSoon.add("The Spectacular Now");
    comingSoon.add("The Canyons");
    comingSoon.add("Europa Report");
    add_img();
    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);
}
//our child listener
private OnChildClickListener myListItemClicked =  new OnChildClickListener() {
    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id) {
        Toast.makeText(getBaseContext(), "Child clicked",
               Toast.LENGTH_LONG).show();
        return false;
    }
};
private OnGroupClickListener myListGroupClicked =  new OnGroupClickListener() {
    public boolean onGroupClick(ExpandableListView parent, View v,
                                int groupPosition, long id) {

        ImageView img = (ImageView)  findViewById(R.id.icona_modifica);
        if(aperto[(int)id] == false)
        {
            img.setVisibility(View.VISIBLE);
            aperto[(int)id] = true;
        }
        else
        {
            img.setVisibility(View.INVISIBLE);
            aperto[(int)id] = false;
        }
        return false;
    }
  };
}

可扩展列表适配器.java

package com.luca.mattia.password;
/**
  * Created by Mattia on 29/11/2014.
*/
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
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);
    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);
    }
    ImageView lblListHeader = (ImageView) convertView
            .findViewById(R.id.lblListHeader);
    return convertView;
}
@Override
public boolean hasStableIds() {
    return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
     <TextView
       android:id="@+id/lblListItem"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:textSize="17dip"
       android:paddingTop="5dp"
       android:paddingBottom="5dp"
       android:singleLine="true"
       android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
  </LinearLayout>

list_group.xml

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
      <!-- <TextView
              android:id="@+id/lblListHeader"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
              android:textSize="17dp"
              android:textColor="#009933" /> -->
      <ImageView
         android:id="@+id/lblListHeader"
         android:layout_width="match_parent"
         android:layout_height="95dp"
         android:background="@drawable/striscia_ignoto"/>
      <ImageView
         android:id="@+id/icona_modifica"
         android:layout_width="22dp"
         android:layout_height="22dp"
         android:layout_alignParentRight="true"
         android:layout_alignParentBottom="true"
         android:visibility="visible"
         android:background="@drawable/icona_modifica"/>
  </RelativeLayout>

谢谢大家。

附言对不起我的英语

您可以在 getGroupView() 方法中添加图像 clickListener:

@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);
    }
    ImageView lblListHeader = (ImageView) convertView.findViewById(R.id.lblListHeader);
    ImageView iconaModifica= (ImageView) convertView.findViewById(R.id.icona_modifica);
    iconaModifica.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               // do your stuff here
              Toast.makeText(_context, "Group position: "+groupPosition, Toast.LENGTH_LONG).show();
        }
    });
    return convertView;
}

更新

如果你想在你的活动中获取 clickListener,你可以通过实现一个接口来实现。

为此,您需要:

1) 创建接口类

public interface IExpandableListInterface {
    public void onClickIconaModifica(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent); // you can change the method parameters if you want
}

2) 在活动中实现接口并添加方法:

    public class MainActivity extends Activity implements IExpandableListInterface{
        public void onClickIconaModifica(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent){
             // do your stuff here
              Toast.makeText(MainActivity.this, "Group position: "+groupPosition, Toast.LENGTH_LONG).show();

        }
   }

3) 将接口传递给适配器:

   listAdapter = new ExpandableListAdapter(this, this, listDataHeader, listDataChild);
//PS: the first "this" is for the context and the second "this" is for the interface

4) 更改适配器构造函数:

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private IExpandableListInterface mMyInterface;
public ExpandableListAdapter(Context context, IExpandableListInterface myInterface, List<String> listDataHeader,
                             HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
    this.mMyInterface = myInterface;
}
5) 在

适配器内部使用您的接口(在您的情况下,您想在 getGroupView() 中调用它):

@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);
        }
        ImageView lblListHeader = (ImageView) convertView.findViewById(R.id.lblListHeader);
        ImageView iconaModifica= (ImageView) convertView.findViewById(R.id.icona_modifica);
        iconaModifica.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMyInterface.onClickIconaModifica(groupPosition, isExpanded, convertView, parent)
            }
        });
        return convertView;
    }

6)好吧,没有第六步。只需运行您的应用程序,我希望对您有所帮助!

最新更新