【问题标题】:how to change textcolor of selected list item both header and child in expandable listview?如何在可扩展列表视图中更改所选列表项的标题和子项的文本颜色?
【发布时间】:2017-07-07 03:19:25
【问题描述】:

我需要什么: 上面的屏幕是我的抽屉。在那个可扩展的列表视图中,如果我按下“仪表板”标题列表,我将重定向到仪表板活动。在这里,我需要“仪表板”文本为蓝色,其他文本为黑色。当我点击“今日跟进”子视图我希望“CRM”和“今日跟进”文本为蓝色,其他为黑色。 我的导航抽屉活动:

    public class BaseActivity extends AppCompatActivity {

    protected DrawerLayout drawer;
    ExpandableListView expListView;
    ExpandableListAdapter listAdapter;
    HashMap<String, List<String>> listDataChild,listDatachild2;
    List<String> listDataHeader;
    List<String> listDataHeader2;
    public List<Integer> groupImages;
    private NavigationView navigationView;
    ViewStub viewStub;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testingnavi);

    }

    public void preparedrawer(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        navigationView = (NavigationView)findViewById(R.id.nav_view);
        expListView = (ExpandableListView)findViewById(R.id.navList);
         drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        setprofileimage();
        prepareListData();

        listAdapter = new ExpandableListAdapter(
                this, listDataHeader,groupImages,listDataChild,drawer);
        expListView.setAdapter(listAdapter);

        expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return false;
            }
        });

        expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {



                //  mDrawerLayout.closeDrawers();
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });

        expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {

                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();

            }
        });

        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            View _lastColored;

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                drawer.closeDrawer(GravityCompat.START);
                // TODO Auto-generated method stub



                Toast.makeText(
                        getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                        .show();


                return false;
            }
        });
    }
    public void setviewstub(int layout_id){
        viewStub=(ViewStub)findViewById(R.id.view_stub);
        viewStub.setLayoutResource(layout_id);
        viewStub.inflate();


    }
    public void setprofileimage(){

        CircleImageView profilePictureView = (CircleImageView) navigationView.getHeaderView(0).findViewById(R.id.profile_image);
        TextView Usernamenav=(TextView)navigationView.getHeaderView(0).findViewById(R.id.Usernamenav);

        final Userloginsession userloginsession = new Userloginsession(getApplicationContext());
        final HashMap<String, String> userlist = userloginsession. isGetuserDetails();
        String first_namee=userlist.get(Userloginsession.IS_FIRST_NAME);
        String last_namee=userlist.get(Userloginsession.IS_LAST_NAME);
        Picasso.with(BaseActivity.this)
                .load(Url.imgurl + userlist.get(Userloginsession.IS_IMAGE))
                .fit().centerCrop()
                .into(profilePictureView);
        Usernamenav.setText(first_namee+" "+last_namee);
        Log.i("imgurl",Url.imgurl + userlist.get(Userloginsession.IS_IMAGE));


    }
    private void prepareListData() {

        groupImages= new ArrayList<Integer>();
        groupImages.add(R.drawable.dashboard_icon);
        groupImages.add(R.drawable.crm_icon);
        groupImages.add(R.drawable.accouns_icon);
        groupImages.add(R.drawable.settings_icon);
        groupImages.add(R.drawable.logout_icon);



        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Dashboard");
        listDataHeader.add("CRM");
        listDataHeader.add("Accounts");
        listDataHeader.add("Settings");
        listDataHeader.add("LogOut");

        // Adding child data

        List<String> Dashboard = new ArrayList<String>();

        List<String> CRM = new ArrayList<String>();
        CRM.add("Today Followup's");
        CRM.add("Unhandled Followup(s)");
        CRM.add("Today Appointments");
        CRM.add("Unhandled Appointment(s)");
        CRM.add("Cold Call(s)");


        List<String> Accounts = new ArrayList<String>();
        Accounts.add("Quotations");
        Accounts.add("Orders");
        Accounts.add("Payments");


        List<String> Settings = new ArrayList<String>();

        List<String> Logout = new ArrayList<String>();
        listDataChild.put(listDataHeader.get(0), Dashboard);
        listDataChild.put(listDataHeader.get(1), CRM); // Header, Child data
        listDataChild.put(listDataHeader.get(2), Accounts);
        listDataChild.put(listDataHeader.get(3), Settings);
        listDataChild.put(listDataHeader.get(4), Logout);

    }
public static void dointent(Context A1, Class A2){

    Intent i = new Intent().setClass(A1, A2);
    i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
    A1.startActivity(i);


}
    public  static Typeface settypefacess(Context _context,String fonts){

        Typeface roboto = Typeface.createFromAsset(_context.getAssets(),
                fonts);
        return roboto;
    }

}

我的适配器:`

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private List<Integer> _group;

    private DrawerLayout _drawer;

    private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
    private static final int[][] GROUP_STATE_SETS = {
            EMPTY_STATE_SET, // 0
            GROUP_EXPANDED_STATE_SET // 1
    };

    private HashMap<String, List<String>> _listDataChild;
    ExpandableListView expandList;
    public ExpandableListAdapter(Context context, List<String> listDataHeader, List<Integer> group,
                                 HashMap<String, List<String>> listChildData, DrawerLayout drawer) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
        this._group=group;
        this._drawer=drawer;

    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);
        int childpositionaa= (int) getChildId(groupPosition,childPosition);
        int gropuposi= (int) getGroupId(groupPosition);
        Log.i("groupposi", String.valueOf(+groupPosition));
        Log.i("groupposicc", String.valueOf(+childPosition));
        Log.i("groupposiccee", String.valueOf(+childpositionaa));
        int child3position=(int)getChildId(1,childPosition);
        Log.i("groupposiccee33", String.valueOf(+child3position));

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }
Log.i("childposition",""+childPosition);
        Typeface roboto = Typeface.createFromAsset(_context.getAssets(),
                "font/Roboto-Light.ttf");
        AppCompatTextView txtListChild = (AppCompatTextView) convertView
                .findViewById(R.id.lblListItem);
        txtListChild.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        _drawer.closeDrawer(GravityCompat.START);
                        switch (groupPosition) {
                            case 0:
                                break;
                            case 1:
                                switch (childPosition){
                                    case 0:
                                        BaseActivity.dointent(_context.getApplicationContext(),Handled_follow.class);

                                        break;
                                    case 1:
                                        BaseActivity.dointent(_context.getApplicationContext(), Unhanded_follow.class);
                                        break;
                                    case 2:
                                        BaseActivity.dointent(_context.getApplicationContext(), Navi_recyview.class);
                                                break;
                                    case 3:
                                        BaseActivity.dointent(_context.getApplicationContext(), Unhandled_appoint.class);
                                                                          break;
                                    case 4:
                                        BaseActivity.dointent(_context.getApplicationContext(), Cold_calls.class);
                                                                              break;
                                }
                                break;
                        }                        
                        break;
                }

                return false;
            }
        });
        AppCompatTextView txtchildcount= (AppCompatTextView) convertView
                .findViewById(R.id.lblistnavcount);
        txtListChild.setText(childText);
        txtListChild.setTypeface(roboto);


        txtchildcount.setText(myList.get(childpositionaa));
        if(gropuposi==2){
            if(childpositionaa==0){
                txtchildcount.setText(myList.get(5));

            }
            else{
                txtchildcount.setText(myList.get(4));
            }
            txtchildcount.setText(myList.get(childpositionaa+5));
        }
        else{
            txtchildcount.setText(myList.get(childpositionaa));
        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

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

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        int headerimg = _group.get(groupPosition);
        Log.i("groupposigroupview", String.valueOf(+groupPosition));
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }
        AppCompatTextView lblListHeader = (AppCompatTextView) convertView
                .findViewById(R.id.lblListHeader);
        View ind = convertView.findViewById( R.id.indicicon);

        if( ind != null ) {
            ImageView indicator = (ImageView)ind;
            if( getChildrenCount( groupPosition ) == 0 ) {
                indicator.setVisibility( View.INVISIBLE );
            } else {
                indicator.setVisibility(View.VISIBLE);
                if (isExpanded) {
                    lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorPrimary));
                    indicator.setImageResource(R.drawable.ic_expand_less_black_18dp);
                } else {
                    lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorlisthead));

                    indicator.setImageResource(R.drawable.ic_expand_more_black_18dp);
                }
            }
        }
        Typeface roboto = Typeface.createFromAsset(_context.getAssets(),
                "font/Roboto-Regular.ttf"); //use this.getAssets if you are calling from an Activity


        lblListHeader.setTypeface(roboto);
        lblListHeader.setText(headerTitle);
        ImageView imgsListHeader = (ImageView) convertView
                .findViewById(R.id.imagview);

        imgsListHeader.setImageResource(headerimg);
lblListHeader.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:

                switch (groupPosition) {
                    case 0:
                        _drawer.closeDrawer(GravityCompat.START);
                        BaseActivity.dointent(_context.getApplicationContext(),Dashboard.class);
                        break;
                    case 1:

                        break;
                    case 2:

                    case 3:
                        _drawer.closeDrawer(GravityCompat.START);

                        break;
                    case 4:
                        _drawer.closeDrawer(GravityCompat.START);

                        break;
                }
                break;
        }

        return false;
    }
});
        return convertView;
    }

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


        return true;
    }

}
`

【问题讨论】:

    标签: android android-studio expandablelistview


    【解决方案1】:

    您需要修改与您在层次结构中的当前位置关联的 GroupHeader 和 GroupItem。

    为了做到这一点,您的 ListAdapter 需要知道这个位置是什么。例如,您可以将其作为参数传递给构造函数。

    然后,当扩展组标题 (getGroupView) 和组项 (getChildView) 时,如果项目是“当前位置”,只需将您的 textview 装饰设置为粗体(或为选定项目扩展另一个特定布局)。

    header 和 child 的位置作为 getGroupView 和 getChildView 方法的参数传递。

    请查看that postthat one 对与您相关的问题的回答。

    【讨论】:

    • @thank you w00ly...如果我选择了仪表板,我希望该文本变为蓝色,直到我选择另一个列表。如果我选​​择冷呼叫,那么我想要 CRM 和冷呼叫文本为蓝色,其他为黑色
    • 我想你所有的活动都是从 BaseActivity 扩展而来的。好吧,当单击“冷电话”时,您正在启动一个意图,对吗?在这里,您将所选项目作为参数(您的类)传递。你也可以传递一个id,在刷新drawer的时候,通过这个参数知道你在哪个section,然后在listAdapter中做相应的修改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多