更改自定义列表视图单击项目运行时的textcolor

时间:2016-06-08 05:15:01

标签: android listview baseadapter

我正在使用基本适配器使用自定义列表视图。我想更改仅单击项目文本视图的文本颜色。

单击列表项对话框将打开,单击确定按钮想要更改列表视图的单击项目的文本颜色。并非所有列表视图的项目。

我已经说过我不想改变整个列表查看项目的颜色。

我只想更改点击项目文字视图的颜色。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    DataBaseAdapter baseAdapter = new DataBaseAdapter(context);

    if (convertView == null) {

        Log.d("tag123", "in getview");
        convertView = inflater.inflate(R.layout.list_item_expense, null);
        viewHolder = new ViewHolder();
        viewHolder.tvDate = (TextView) convertView
                .findViewById(R.id.tvDateExpense);
        viewHolder.ivCatExp = (ImageView) convertView
                .findViewById(R.id.ivCatExpense);
        viewHolder.tvPayee = (TextView) convertView
                .findViewById(R.id.tvPayeeExpense);
        // viewHolder.tvAmt = (TextView) convertView
        // .findViewById(R.id.tvAmtExpense);
        viewHolder.tvBy = (TextView) convertView
                .findViewById(R.id.tvByExpense);

        viewHolder.tvListItemExpense = (com.rey.material.widget.TextView) convertView
                .findViewById(R.id.tvListItemExpense);

        convertView.setTag(viewHolder);

    } else {
        Log.d("tag123", "in getview else condition");
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.tvDate.setText(installments.get(position));

    viewHolder.tvPayee.setText(String.valueOf(amount));

    viewHolder.tvBy.setText(String.valueOf(simpleInterest));

    viewHolder.tvListItemExpense.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ActionItem editItem_for_def_user = new ActionItem(ID_EDIT,
                    "Want to Pay?", context.getResources().getDrawable(
                            R.drawable.fbem_ptp32_popup));
            final QuickAction mQuickAction_for_def_user = new QuickAction(
                    context);

            mQuickAction_for_def_user.addActionItem(editItem_for_def_user);

            mQuickAction_for_def_user
                    .setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
                        @SuppressLint("ResourceAsColor")
                        @Override
                        public void onItemClick(QuickAction quickAction,
                                int pos, int actionId) {
                            ActionItem actionItem = quickAction
                                    .getActionItem(pos);

                            if (actionItem != null) {

                                if (viewHolder.tvListItemExpense.isSelected()) {

                                    viewHolder.tvDate.setTextColor(context
                                            .getResources().getColor(
                                                    R.color.light_green));
                                }
                            }
                        }
                    });
            mQuickAction_for_def_user
                    .setOnDismissListener(new QuickAction.OnDismissListener() {
                        @Override
                        public void onDismiss() {

                        }
                    });
            mQuickAction_for_def_user.show(v);
        }
    });
    total = amount + simpleInterest;

    // viewHolder.tvAmt.setText(String.valueOf(total));
    baseAdapter.close();
    return convertView;
}

我想这样做

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试在getView()的开头添加:

final ListView listview = (ListView) parent;

然后在您的操作中,请勿更改文字颜色,但请检查所选项目:

listview.setItemChecked(position, true);

在返回convertView之前,相应地更改文本颜色:

if(listview.isItemChecked(position)){
// set text color for checked items.
}else{
// set text color for normal items.
}