简体   繁体   中英

Get child view from Expandable list View

I am using expandable list view in my app.

I am setting Background color for my selected items in my expandablelistview.

When the fragment is started,my expandablelistview first child will be selected by default.But I want to get the first child view from it, so that i can change the background color.

I am getting null in getChild()

Please help in this

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.mypage_list_fragment_f, container,
                false);

        initView(v);


        View child = mLeftMenuListView.getChildAt(0);
        child.setBackgroundResource(R.drawable.list_select);
        mAdapter.notifyDataSetChanged();
        mLeftMenuListView.invalidateViews();

        return v;
    }

private void initView(View v) {

        mLeftMenuListView = (ExpandableListView) v
                .findViewById(R.id.mypage_list_fragment_expandablelistview);
        mAdapter = new MyPageListAdapter(getActivity(),
                R.layout.mypage_left_menu_layout_group_i,
                R.layout.mypage_left_menu_layout_group_child_i);
        mAdapter.setmIsSelected(mIsSelected);

        mLeftMenuListView.setAdapter(mAdapter);




        mLeftMenuListView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView arg0, View arg1,
                    int arg2, int arg3, long arg4) {

                TextView category = (TextView) arg1
                        .findViewById(R.id.mypage_category);
                mAdapter.setSelected(category.getText().toString());
                mOnCategorySelectedListener.onCategorySelected(category
                        .getText().toString());
                mAdapter.notifyDataSetChanged();
                if (isKitkat) {
                    mChild = (RelativeLayout) arg1
                            .findViewById(R.id.child_relative);
                    if ( mChildLastColored != null) {
                        mChildLastColored.setBackgroundResource(Color.TRANSPARENT);
                        mChildLastColored.invalidate();
                    }
                    if (mGroupLastColored != null ) {
                        mGroupLastColored.setBackgroundResource(Color.TRANSPARENT);
                    }
                    mChildLastColored = arg1;
                    arg1.setBackgroundResource(R.drawable.list_select);

                }
                return false;
            }

        });

        mLeftMenuListView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView arg0, View arg1,
                    int arg2, long arg3) {
                if (isKitkat) {
                    mGroup = (RelativeLayout) arg1
                            .findViewById(R.id.group_relative);
                    if (mGroupLastColored != null ) {
                        mGroupLastColored.setBackgroundResource(Color.TRANSPARENT);
                        mGroupLastColored.invalidate();
                    }
                    if ( mChildLastColored != null) {
                        mChildLastColored.setBackgroundResource(Color.TRANSPARENT);
                    }
                    mGroupLastColored = arg1;
                    arg1.setBackgroundResource(R.drawable.list_select);
                }
                TextView category = (TextView) arg1
                        .findViewById(R.id.mypage_category);
                mAdapter.setSelected(category.getText().toString());
                mOnCategorySelectedListener.onCategorySelected(category
                        .getText().toString());
                mAdapter.notifyDataSetChanged();
                return false;
            }

        });

        mAdapter.setSelected(0);

}

You can do that, by getting the Adapter and the View inside it. Here's what you can try. Inside the onCreateView(), after you inflate,

View defaultSelectedListView = yourExpListView.getAdapter().getView(0, v, container);
defaultSelectedListView.setBackgroundColor(Color.RED);

Hope this helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM