【问题标题】:Get child view from Expandable list View从可扩展列表视图中获取子视图
【发布时间】:2013-11-30 08:50:54
【问题描述】:

我在我的应用程序中使用可展开的列表视图。

我正在为展开式列表视图中的选定项目设置背景颜色。

片段启动时,默认选择我的可扩展列表视图第一个子视图。但我想从中获取第一个子视图,以便我可以更改背景颜色。

我在 getChild() 中得到空值

请帮忙

@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);

}

【问题讨论】:

    标签: android listview expandablelistview expandable


    【解决方案1】:

    您可以通过在其中获取适配器和视图来做到这一点。这是您可以尝试的。 在 onCreateView() 内部,充气后,

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

    希望对你有帮助!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多