1

enter image description here

What I'm trying to do is when I click on any item of the list that will change the color of text and the drawable this is my getview method

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
        LayoutInflater mInflater = (LayoutInflater)
                mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        convertView = mInflater.inflate(R.layout.nav_drawer_item, null);
        convertView.setFocusable(false);

        final TextView mItemTitle = (TextView) convertView.findViewById(R.id.item_title_tv);
        mItemTitle.setText(mNavDrawerItems.get(position).getmItemTitle());

        final ImageView mTitleIcon = (ImageView) convertView.findViewById(R.id.item_icon_iv);
        mTitleIcon.setImageDrawable(
                mContext.getResources().getDrawable(mNavDrawerItems.get(position).getmItemIcon()));

        if (position == 0)
        {
            convertView = mInflater.inflate(R.layout.nav_drawer_header, null);
        }
        else if (position == 1)
        {
            /*
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    setColorAndDrawble(mItemTitle, mTitleIcon,
                            mContext.getResources().getColor(R.color.last_news_click_color),
                            mContext.getResources().getDrawable(R.drawable.ic_class_news_click));
                }
            });
            */
        }
        else if (position == 2)
        {

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    setColorAndDrawble(mItemTitle, mTitleIcon,
                            mContext.getResources().getColor(R.color.public_chat_click_color),
                            mContext.getResources().getDrawable(R.drawable.ic_public_chat_click));
                }
            });
        }
        else if (position == 3)
        {
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    setColorAndDrawble(mItemTitle, mTitleIcon,
                            mContext.getResources().getColor(R.color.messages_click_color),
                            mContext.getResources().getDrawable(R.drawable.ic_messages_click));
                }
            });
        }
        else if (position == 4)
        {
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    setColorAndDrawble(mItemTitle, mTitleIcon,
                            mContext.getResources().getColor(R.color.results_click_color),
                            mContext.getResources().getDrawable(R.drawable.ic_results_click));
                }
            });
        }

        return convertView;
}

private void setColorAndDrawble(TextView textView, ImageView imageView, int color, Drawable drawble)
{
    imageView.setImageDrawable(drawble);
    textView.setTextColor(color);
}

the problem is now the listener of listview doesn't work. Why?

mDrawerList.setOnItemClickListener(new SlideMenuClickListener());    

private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

            Log.e("SlideMenuClickListener", position + "");
            TextView textView = (TextView) view.findViewById(R.id.item_title_tv);
            textView.setTextColor(getResources().getColor(R.color.last_news_click_color));
            Log.e("textView", textView.getText() + "");
            // display view for selected nav drawer item
            displayView(position);
        }
    }
Howli
  • 12,291
  • 19
  • 47
  • 72
user3871129
  • 261
  • 1
  • 3
  • 9

3 Answers3

0

Your approach can be seen as wrong, You should instead create a class and a Custom Adapter for each listview item, also you shouldn't use setOnClickListener for each element.

Using of a ViewHolder is advised too.

For further details also check this link or the official documentation.

Amedeo Baragiola
  • 314
  • 4
  • 14
0

When you set a click listener for a row, this blocks the normal click processing for the ListView. You can fix this by moving the entire logic for updating the row into your onItemClick method. (You already have the text color change logic duplicated there.)

private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(ListView parent, View view, int position, long id) {

        Log.e("SlideMenuClickListener", position + "");
        TextView textView = (TextView) view.findViewById(R.id.item_title_tv);

        if (position > 0) {
            textView.setTextColor(getTextColor(position),
                textView.getTextColors().getDefaultColor());
            final ImageView titleIcon = (ImageView) view.findViewById(R.id.item_icon_iv);
            titleIcon.setImageDrawable(getImage(position));
            parent.getAdapter().getView(i, view, list);
        }
        Log.e("textView", textView.getText() + "");
        // display view for selected nav drawer item
        displayView(position);
    }

    private int getTextColor(int position, int defaultColor) {
        final Resources res = getContext().getResources();
        int color = defaultColor;
        switch (position) {
            case 2: color = res.getColor(R.color.public_chat_click_color); break;
            case 3: color = res.getColor(R.color.messages_click_color);    break;
            case 4: color = res.getColor(R.color.results_click_color);     break;
        }
        return color;
    }

    private Drawable getImage(int position) {
        int resId = mNavDrawerItems.get(position).getmItemIcon()); // default
        switch (position) {
            case 2: resId = R.drawable.ic_public_chat_click; break;
            case 3: resId = R.drawable.ic_messages_click;    break;
            case 4: resId = R.drawable.ic_results_click;     break;
        }
        return getContext().getDrawable(resId);
    }
}

You can improve the overall performance of this approach by using the view holder pattern (as recommended in this tutorial). This involves setting the tag for each row to a small object that has references to the text and image views for that row.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    @user3871129 - I don't know. I tried to imitate your code, but maybe I got it wrong. You should be able to fix the logic. The important idea is the approach: make the changes in `onItemClick`. – Ted Hopp Dec 25 '14 at 21:07
  • still the same problem the color of text view and icon didn't change – user3871129 Dec 25 '14 at 21:10
  • @user3871129 - Hm. You may need to add a line of code to force the row to refresh (as described in [this thread](http://stackoverflow.com/questions/2123083/android-listview-refresh-single-row).) I updated my sample code to show how that might work, and fixed a couple of other obvious problems with it. – Ted Hopp Dec 25 '14 at 22:58
0

I solved this problem by add setOnTouchListener to convertView in my adpater class :) and it's work fine right now

user3871129
  • 261
  • 1
  • 3
  • 9