簡體   English   中英

如何更改一次單擊時和再次單擊時列表視圖的背景色,我需要不使用選擇器來更改背景色

[英]how to change the background color of a listview when clicked once and when clicked again i need to change the background color without using selector

如何在一次單擊和再次單擊時更改列表視圖的背景顏色,我需要不使用選擇器來更改背景顏色。我已經在選擇每個項目時更改了背景。但是當我再次單擊該項目時將背景顏色更改為紅色。我如何給出條件。我將在此處發布我的代碼。請提供建議。請幫助...

公共類ProvierActivity擴展了Activity {

private String text[] = { "BroadStripe-Cable (Seattle)",
        "BroadStripe-Digital (Seattle)", "BroadStripe-Cable (Seattle)",
        "Comcast king county south)", "BroadStripe-Cable (Seattle)",
        "Comcast king county south", "BroadStripe-Digital (Seattle)",
        "BroadStripe-Digital (Seattle)", "BroadStripe-Cable (Seattle)",
        "Comcast king county south" };

ImageView icon;
public static int selectedRow;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ListView list = (ListView) findViewById(R.id.listview_id);

    list.setAdapter(new ArrayAdapter<String>(this, R.layout.list,
            R.id.title, text));
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view,
                int position, long arg3) {

            for (int i = 0; i < adapter.getChildCount(); i++) {
                if (i == position) {

                    adapter.getChildAt(i).setBackgroundColor(Color.BLUE);

                } else {
                    adapter.getChildAt(i).setBackgroundColor(Color.BLACK);

                }

            }

        }

    });
}

}

使用此編輯后的代碼。

public static int selectedRow;

/** Called when the activity is first created. */

private int prePos=-1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ListView list = (ListView) findViewById(R.id.listview_id);

    list.setAdapter(new ArrayAdapter<String>(this, R.layout.list,
            R.id.title, text));
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view,
                int position, long arg3) {

            for (int i = 0; i < adapter.getChildCount(); i++) {
                if (i == position) {   

                    if(position!=prePos){   
                       adapter.getChildAt(i).setBackgroundColor(Color.BLUE);

                      prePos=position;

                    }else{
                        adapter.getChildAt(i).setBackgroundColor(Color.BLACK);
                          prePos=-1;
                        }

                   }else{

                      adapter.getChildAt(i).setBackgroundColor(Color.BLACK);

                   }

                }


        }

    });
}

}

這將為您提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM