Android单击更改图像,自定义可扩展列表适配器 [英] Android Change image on click, custom expandable list adapter

查看:78
本文介绍了Android单击更改图像,自定义可扩展列表适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用自定义可扩展列表适配器实现可扩展列表视图.
并且在组展开和组折叠时图像也会改变.

I am going to implement an expandable listview with custom expandable list adapter.
And also image change on group expand and group collapse.

我的可扩展列表"视图运行良好,但是当我在这些事件上设置图像更改时,出现了一些问题.也就是说,当我展开一个图像时,另一个图像也会自动更改.我想我不能保存该分组项目的正确地址.

My Expandable list view works well but when I set image change on those events there is some problem. i.e when I expand one, automatically another one's image also changes. I think I can not hold the proper address of the group item.

这是我的代码.

public class HomeFrame extends Fragment {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    ImageView img_selection;
    HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
     public static int[] GalImages = new int[] {
                R.drawable.banner_one,
                R.drawable.banner,
                R.drawable.banner_three,
                R.drawable.banner_two,};
    public HomeFrame() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.home_frame, container, false);

        ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.banner);
        ImageAdapter adapter = new ImageAdapter(getActivity());
        viewPager.setAdapter(adapter);

        //img_selection=(ImageView)rootView.findViewById(R.id.img_selection);

        expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpListAdapter(getActivity(), listDataHeader,
                listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        // Listview Group click listener
        expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                Toast.makeText(getActivity(),
                 "Group Clicked " + listDataHeader.get(groupPosition),
                 Toast.LENGTH_SHORT).show();
                return false;

            }
        });

        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {

                  Toast.makeText(getActivity(),
                  groupPosition + " Expanded",
                  Toast.LENGTH_SHORT).show();

             img_selection=(ImageView)expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
                img_selection.setImageResource(R.drawable.arrow_se);
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {

                 Toast.makeText(getActivity(),
                 groupPosition + " Collapsed",
                 Toast.LENGTH_SHORT).show();

                 img_selection=(ImageView) expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
                //ImageView img_selection=(ImageView) get.findViewById(R.id.img_selection);

                img_selection.setImageResource(R.drawable.arrow);
            }
        });

        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                /*
                 * Toast.makeText( getActivity(),
                 * listDataHeader.get(groupPosition) + " : " +
                 * listDataChild.get( listDataHeader.get(groupPosition)).get(
                 * childPosition), Toast.LENGTH_SHORT) .show();
                 */
                return false;
            }
        });

        return rootView;
    }

而Adapter类是..

And Adapter class is..

public class ExpListAdapter 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 ExpListAdapter(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.item_home, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.heading);
        //ImageView img_selection=(ImageView)convertView.findViewById(R.id.img_selection);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        //img_selection.setImageResource(R.drawable.arrow);

        return convertView;
    }

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

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

}

推荐答案

getGroupView()中的自定义适配器中,执行以下操作:

In your custom adapter in the getGroupView() do this:

@Override
 public View getGroupView(int groupPosition, boolean isExpanded, View view,
                ViewGroup parent) {

    ImageView img_selection=(ImageView) view.findViewById(R.id.img_selection);
    int imageResourceId = isExpanded ? R.drawable.group_closed
                        : R.drawable.group_open;
     img_selection.setImageResource(imageResourceId);
}

这篇关于Android单击更改图像,自定义可扩展列表适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆