简体   繁体   中英

How can I remove setBackgroundResource when new View is clicked?

I have a Horizontal Listview with a textview. When I click on a textview in the view, that particular textview gets a border.

public View getView(int position, View convertView, ViewGroup parent) {
        View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.minute_listview, null);
        Typeface afBold = Typeface.createFromAsset(getAssets(), "fonts/AftenScreen-Bold.ttf");
        minuteText = (TextView) retval.findViewById(R.id.title);
        Button button = (Button) retval.findViewById(R.id.clickbutton);
        button.setOnClickListener(mOnButtonClicked);
        minuteText.setText(dataObjects[position]);
        minuteText.setTypeface(afBold);

//THIS IS WHERE THE BORDER GETS SET
        minuteText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setBackgroundResource(R.drawable.border);
            }
        });

        return retval;
    }

Now, how can I remove this border and set it on the next textview thats clicked?

Use this it will give you a white background or a translucent one, in other words it has been removed

WhatEverView.setBackground(new ColorDrawable(Color.TRANSPARENT));

okay full code

int pos -1;// default is -1, which means no one has altered it
// replicate this onclick listener logic
minuteText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(pos != -1){ // it is not -1 that means some1 has altered it                 
                parentView.findViewById(pos).
                      setBackground(new ColorDrawable(Color.TRANSPARENT));
            // the above line searched for the view and changed the background
            }
            pos = v.getId(); // the id of the new view, keep doing it
            v.setBackgroundResource(R.drawable.border);
        }
    });

so use this for all onclick listeners you want that effect on

Does it fit your requirement?

add this

TextView ClicledTv ;//to save the clicked tv id

then

 minuteText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(Clickedtv!=null)
                clickedtv.setBackground(R.drawable.anotherOne);
                v.setBackgroundResource(R.drawable.border);
                clickedTv=minuteText;
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM