Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a multilevel recyclerview, and I have already set different colors for every item on the first level of the recyclerview.
I need for every subitem on every list, to have the same color as the parent list, but different text.
How can I accomplish this?
Thanks.

What I have tried:

<pre>
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity
{
    ExpandableListView explvlist;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
        explvlist.setAdapter(new ParentLevel());

    }

    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(MainActivity.this);
            SecondLevelexplv.setAdapter(new SecondLevelAdapter());
            SecondLevelexplv.setGroupIndicator(null);
            return SecondLevelexplv;
        }

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

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

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

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent)
        {
            TextView tv = new TextView(MainActivity.this);
            switch(groupPosition % 7) {
                case 0:
                    tv.setText(getResources().getStringArray(R.array.colores)[groupPosition%7]);
                    tv.setBackgroundColor(Color.WHITE);
                    tv.setPadding(10, 7, 7, 7);
                    break;
                case 1:
                    tv.setText(getResources().getStringArray(R.array.colores)[groupPosition%7]);
                    tv.setBackgroundColor(getResources().getColor(R.color.c2));
                    tv.setPadding(10, 7, 7, 7);
                    break;
                case 2:
                    tv.setText(getResources().getStringArray(R.array.colores)[groupPosition%7]);
                    tv.setBackgroundColor(getResources().getColor(R.color.c3));
                    tv.setPadding(10, 7, 7, 7);
                    break;
                case 3:
                    tv.setText(getResources().getStringArray(R.array.colores)[groupPosition%7]);
                    tv.setBackgroundColor(getResources().getColor(R.color.c4));
                    tv.setPadding(10, 7, 7, 7);
                    break;
                case 4:
                    tv.setText(getResources().getStringArray(R.array.colores)[groupPosition%7]);
                    tv.setBackgroundColor(getResources().getColor(R.color.c5));
                    tv.setPadding(10, 7, 7, 7);
                    break;
                case 5:
                    tv.setText(getResources().getStringArray(R.array.colores)[groupPosition%7]);
                    tv.setBackgroundColor(getResources().getColor(R.color.c6));
                    tv.setPadding(10, 7, 7, 7);
                    break;
                case 6:
                    tv.setText(getResources().getStringArray(R.array.colores)[groupPosition%7]);
                    tv.setBackgroundColor(getResources().getColor(R.color.c7));
                    tv.setPadding(10, 7, 7, 7);
                    break;
            }
            /*TextView tv = new TextView(MainActivity.this);
            tv.setText("->FirstLevel");
            tv.setBackgroundColor(Color.WHITE);
            tv.setPadding(10, 7, 7, 7);*/
            tv.setTextSize(20);
            return tv;
        }

        @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(MainActivity.this);
            tv.setText("child");
            tv.setPadding(15, 5, 5, 5);
            tv.setBackgroundColor(Color.YELLOW);
            tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            return tv;
        }

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

        @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)
        {
            //Second Level
            TextView tv = new TextView(MainActivity.this);
            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;
        }

    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900