适配器不会调用getChildView android

时间:2016-03-03 11:10:01

标签: android expandablelistview

我正在尝试开发3级向下扩展列表视图,但在第一级项目无法看到第一级别的孩子后,卡住了。

仅显示父视图未显示第二级视图,甚至不显示任何点击事件。

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

import com.ofd.R;
import com.ofd.base.BaseFragment;
import com.ofd.utils.Utils;

import static com.ofd.utils.Constants.LEFT_ICON_PREVIOUS;
import static com.ofd.utils.Constants.NONE;

/*
 * This fragment is used to show the Agencies expanable list
 * 
 * @author girish.sharma
 *
 */

public class AgenciesFragment extends BaseFragment implements OnItemClickListener {
    private View view;
    private Button mSubmit;
    public static int Service = -1;
    private Context mContext;

    ExpandableListView explvlist;
    private ViewHolder mViewHolder;
    private LayoutInflater mInflater;

    /**
     * This is the function used to initialize the view of the fragment
     ***/
    @Override
    protected View initUI(LayoutInflater inflater, ViewGroup container) {
        if (view == null) {
            view = inflater.inflate(R.layout.agencies_fragment, null);
            mContext = getActivity();
            mInflater = LayoutInflater.from(mContext);
            explvlist = (ExpandableListView) view.findViewById(R.id.ParentLevel);
            explvlist.setAdapter(new ParentLevel());
            explvlist.setOnItemClickListener(this);
        }
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
        super.onItemClick(adapterView, arg1, position, arg3);
        Utils.showToast(mContext, "Click");
    }

    public class ParentLevel extends BaseExpandableListAdapter {

        @Override
        public Object getChild(int arg0, int arg1) {

            return arg1;
        }

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

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            CustExpListview SecondLevelexplv = new CustExpListview(mContext);
            SecondLevelexplv.setAdapter(new SecondLevelAdapter());
            SecondLevelexplv.setGroupIndicator(null);
            return SecondLevelexplv;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 3;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupPosition;
        }

        @Override
        public int getGroupCount() {
            return 5;
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                mViewHolder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService
                        (Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.row_first, parent, false);
                // Inflate the layout that you want for each row
                /*convertView = mInflater.inflate(R.layout.row_first, parent, false);*/
                /*mViewHolder.txtViewTitle = (TextView) view.findViewById(R.id.txtViewTitleMain);*/

                // Add a reference in ViewHolder for your Button, find it like the TextViews and define its OnClickListener here too, eventually...
                /*view.setTag(mViewHolder);*/
            } /*else
                mViewHolder = (ViewHolder) view.getTag();*/
            mViewHolder.txtViewTitle = (TextView) view.findViewById(R.id.txtViewTitleMain);

            mViewHolder.txtViewTitle.setText("FirstLevel");
            /*TextView tv = new TextView(mContext);
            tv.setText("->FirstLevel");
            tv.setBackgroundColor(Color.WHITE);
            tv.setPadding(10, 7, 7, 7);*/

            return view;
        }

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

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

    public class CustExpListview extends ExpandableListView {

        int intGroupPosition, intChildPosition, intGroupid;

        public CustExpListview(Context context) {
            super(context);
        }

        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public class SecondLevelAdapter extends BaseExpandableListAdapter {

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childPosition;
        }

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

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            TextView tv = new TextView(mContext);
            tv.setText("child");
            tv.setPadding(15, 5, 5, 5);
            tv.setBackgroundColor(Color.GRAY);
            tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            return tv;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 5;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupPosition;
        }

        @Override
        public int getGroupCount() {
            return 1;
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            TextView tv = new TextView(mContext);
            tv.setText("-->Second Level");
            tv.setPadding(12, 7, 7, 7);
            tv.setBackgroundColor(Color.RED);

            return tv;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }
    }

    /**
     * Here change the header text
     ***/
    @Override
    public void onResume() {
        TitleBarFragment fragment = (TitleBarFragment) getFragment(R.id.activity_fragment_launcher_header);
        if (fragment != null) {
            fragment.setTitleBarVisibility(true);
            fragment.setHeaderTitleAndSideIcon(getActivity().getResources()
                            .getString(R.string.fragment_agencies_header),
                    LEFT_ICON_PREVIOUS, NONE);
        }

        super.onResume();
    }

    private class ViewHolder {
        private TextView txtViewTitle;
    }
}

1 个答案:

答案 0 :(得分:0)

我花了好几个小时试图找到同样的问题,只是意识到这对我来说很简单:

  1. 出于某种原因(我现在将进行调试) - 触摸时groupview没有展开。
  2. 由于组视图未展开,因此没有理由浪费CPU周期来获取不会显示的视图。
  3. 不调用适配器中与子项相关的所有方法。
  4. 尝试调用explvlist.expandGroup(0 ... n)手动展开它,看看它是否在适配器中调用了与子项相关的方法。