如何扩展ExpandableListView?

时间:2012-11-07 09:12:56

标签: android expandablelistview

我使用自定义适配器继承了一些具有可扩展列表视图的代码。我试图找出扩展事件的注册位置,以便我可以使用其他组件触发它。我在expandablelistview中没有看到该组的任何听众。从我在日志中使用print语句可以看出,当单击我的组时,会调用getGroupView()。但是,我不知道调用方法的位置或调用方法。

这是自定义适配器类..

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

        private String[][] teams;
        private String[] league;
        private final Context context;
        private ImageView imageViewUser;

        public MyExpandableListAdapter(String[] groups, String[][] children, Context context) {
            this.context = context;
            this.teams = children;
            this.league = groups;
        }

        public Object getChild(int groupPosition, int childPosition) {
            return teams[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

         public int getChildrenCount(int groupPosition) {
         return teams[groupPosition].length;
         }

         public TextView getGenericView() {
             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
             ViewGroup.LayoutParams.FILL_PARENT, 64);

             TextView textView = new TextView(context);
             textView.setLayoutParams(lp);
             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

             textView.setPadding(56, 0, 0, 0);
             return textView;
         }
         public boolean areAllItemsEnabled() {
                return false;
            }
         public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
             if (view == null) {
                    LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                    view = inf.inflate(R.layout.child_row, null);
                }
             view.setFocusable(false);
             view.setClickable(false);
             TextView textView = (TextView) view.findViewById(R.id.grp_child);
             textView.setFocusable(false);
             textView.setClickable(false);
             textView.setText(getChild(groupPosition, childPosition).toString());
             //final ImageView imageView;
             //imageView = (ImageView) view.findViewById(R.id.imageViewChild);
            // imageView.setImageResource(android.R.drawable.ic_menu_gallery);
             //imageView.setTag(new ChildPosition(groupPosition,childPosition));
             //imageView.setOnClickListener(new OnClickListener(){

                    /*public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        ChildPosition realPosition = (ChildPosition) imageView.getTag();
                        Log.d("Shimmer","Click" + "position: " + realPosition.mGroupPosition + " " + realPosition.mChildPosition);
                    }

                 });*/
             return view;
         }

         public Object getGroup(int groupPosition) {
             System.out.println("Group Clicked!S");

         return league[groupPosition];
         }

         public int getGroupCount() {
         return league.length;
         }

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

         public View getGroupView(int groupPosition, boolean isExpanded,View view, ViewGroup parent) {
             //TextView textView = getGenericView();

             System.out.println("I AM HERE!!!!");

             if (view == null) {
                    LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                    view = inf.inflate(R.layout.group_row, null);
                }
             TextView textView = (TextView) view.findViewById(R.id.row_name);
             textView.setText(getGroup(groupPosition).toString());
             final ImageView imageView;
             imageView = (ImageView) view.findViewById(R.id.imageView1);
             imageView.setImageResource(R.drawable.expand);
             imageView.setFocusable(false);
             imageView.setClickable(true);
             imageView.setTag(groupPosition);

             imageViewUser = (ImageView) view.findViewById(R.id.usericon);
             imageViewUser.setImageResource(R.drawable.user_one);

        /*   if (view == null) {
                    LayoutInflater inf = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
                    view = inf.inflate(R.layout.expandlist_group_item, null);
                }*/


return view;
         }

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


    }

1 个答案:

答案 0 :(得分:0)

您是否尝试模拟群组上的点击,如下所示:

groupView.performClick();

编辑:

public View getGroupView(int groupPosition, boolean isExpanded,View view, ViewGroup parent) {
    System.out.println("I AM HERE!!!!");

    ...

    if (groupPosition == thePositionIWantToAutomaticallyExpend) {
        view.post(new Runnable() {
             @Override
             public void run() {
                view.performClick(); // This will simulate a user clicking on the view
             }
        });
    }
}