无法确定android ViewGroup中单击的项目

时间:2016-01-22 17:24:11

标签: android android-viewgroup

我正在使用ViewGroup。我想对组的项目进行单击效果,以便用户可以检测当前单击的项目。有人可以帮帮我吗?

private View getSubCategoryListItemView(final SubCategoryItem si, double dwPercentage, final int cat_id)
    {
        LayoutInflater li = LayoutInflater.from(this);
        View vv = li.inflate(R.layout.sub_cat_list_item, llCatListHolder, false);
        ImageView ivIcon = (ImageView) vv.findViewById(R.id.iv_sub_cat_icon);
        final TextView tvName = (TextView) vv.findViewById(R.id.tv_sub_cat_name);
        ivIcon.setImageResource(AppConstants.ALL_CAT_MARKER_ICONS[cat_id-1]);
        ViewGroup.LayoutParams lpIv = ivIcon.getLayoutParams();
        lpIv.width = (int) (primaryIconWidth * dwPercentage);
        ivIcon.setLayoutParams(lpIv);
        tvName.setText(si.getSubcatHeader());
        tvName.setTextSize((float) (VIEW_WIDTH * .10 * dwPercentage));

/**************************
 *
 * This OnClickListener will be called for clicking subcategory items from the top list
 *
 * Toast.makeText(getApplicationContext(), "Entertainment ", Toast.LENGTH_SHORT).show();
 * ************************/
      //  tvName.setTextColor(Color.WHITE);

        Toast.makeText(getApplicationContext(), "BackTrack ", Toast.LENGTH_SHORT).show();

        vv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                ArrayList<SubCategoryItem> subCategoryItems;
                subCategoryItems = getSubCategoryList(cat_id);


                for(SubCategoryItem si : subCategoryItems)
                {
                //    tvName.setTextColor(Color.WHITE);
                }
                tvName.setTextColor(Color.RED);
                /*code for category*/
                /*following code will be different for each category*/
                /*category id 1 means education.
                * category id 2 means health
                * category id 3 means entertainment
                * category id 4 means government
                * category id 5 means legal
                * category id 6 means financial
                * category id 7 means job*/
            //    tvName.setTextColor(Color.WHITE);
                switch (currentCategoryID) {
                    case AppConstants.EDUCATION:
                        Toast.makeText(getApplicationContext(), "Education Entrance ", Toast.LENGTH_SHORT).show();
                        ArrayList<EducationServiceProviderItem> eduItem;
                        eduItem = constructEducationListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithEducationInfo(si.getSubcatHeader(), cat_id, eduItem);
                        break;
                    case AppConstants.HEALTH:
                        Toast.makeText(getApplicationContext(), "Helath Entrance ", Toast.LENGTH_SHORT).show();
                        //TODO write necessary codes for health
                        ArrayList<HealthServiceProviderItem> healthItem;
                        healthItem = constructHealthListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithHealthInfo(si.getSubcatHeader(), cat_id, healthItem);

                        break;


                    case AppConstants.ENTERTAINMENT:
                        tvName.setTextColor(Color.GREEN);
                        Toast.makeText(getApplicationContext(), "Entertainment ", Toast.LENGTH_SHORT).show();
                        ArrayList<EntertainmentServiceProviderItem> entItem;
                        entItem = constructEntertainmentListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithEntertainmentInfo(si.getSubcatHeader(), cat_id, entItem);
                        break;
                    //TODO write necessary codes for entertainment

                    case AppConstants.GOVERNMENT:
                        //TODO write necessary codes for government
                        break;
                    case AppConstants.LEGAL:
                        Toast.makeText(getApplicationContext(), "Legal ", Toast.LENGTH_SHORT).show();

                        ArrayList<LegalAidServiceProviderItem>legalItem;
                        legalItem = constructlegalaidListItemForHeader(cat_id,si.getSubcatHeader());
                        callMapFragmentWithLegalAidInfo(si.getSubcatHeader(),cat_id,legalItem);
                        break;
                    case AppConstants.FINANCIAL:
                        ArrayList<FinancialServiceProviderItem>financialItem;
                        financialItem = constructfinancialListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithFinancialInfo(si.getSubcatHeader(), cat_id, financialItem);
                        break;
                    case AppConstants.JOB:
                        ArrayList<JobServiceProviderItem>jobItem;
                        jobItem = constructjobListItemForHeader(cat_id, si.getSubcatHeader());
                        callMapFragmentWithJobInfo(si.getSubcatHeader(), cat_id, jobItem);
                        break;
                    default:
                        break;
                }
                /*code for all*/
            }
        });
        return vv;
    }


flag

我已经设置了一个OnclickListener。但我无法检测到现在单击哪个TextView?我试图改变文本颜色。假设我将Text color设置为橙色并将commmon颜色设置为黑色。单击第一个项目后,文本颜色为橙色。点击另一个项目时,它也变为橙色。然后两个项目是橙色。但是点击下一个项目之前的项目应该是黑色的,只有当前项目应该是橙色。

0 个答案:

没有答案