亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

android - 如果通過點擊更換Gridview 的指定Item 背景顏色

瀏覽:102日期:2024-08-30 16:45:55

問題描述

我的情況是這樣的。 主界面是用Gridview展示出餐廳里的餐桌情況。

默認情況下,每個桌子都是綠色背景。 如果點擊該item, 會出現一個Dialog 窗口;提示是否開桌子。 如果點擊是,該item的背景顏色更換為紅色。

以下是我的代碼,請大神們指點迷津!

public class table extends AppCompatActivity implements AdapterView.OnItemClickListener{GridView gridView;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_table); gridView=(GridView)findViewById(R.id.gridview); String wtf[]={'1a','1b','1c','1d','2a','2b','2c','2d','3a','3b','3c','3d'}; gridView.setAdapter(new my_adapter(this,wtf)); gridView.setOnItemClickListener(this);}@Overridepublic void onItemClick(final AdapterView<?> adapterView, View view, final int i, long l) { new AlertDialog.Builder(this) .setTitle('臺座號 '+adapterView.getItemAtPosition(i).toString()) .setMessage('確定開桌?') .setPositiveButton('是', new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) { Toast.makeText(table.this,adapterView.getItemAtPosition(i).toString()+' 已開桌,請下單。',Toast.LENGTH_SHORT).show();} }) .setNegativeButton('否', null) .show();}}class my_adapter extends BaseAdapter{LayoutInflater inflater=null;Context ctx;String table_names[];ArrayList store_table_no;my_adapter(Context ctx, String table_names[]){ this.ctx=ctx; this.table_names=table_names; store_table_no=new ArrayList<Integer>(); for (int i=0;i<table_names.length;i++){store_table_no.add(table_names[i]); }}@Overridepublic int getCount() { return store_table_no.size();}@Overridepublic Object getItem(int i) { return store_table_no.get(i);}@Overridepublic long getItemId(int i) { return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) { View row=view; if(row==null){inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);row=inflater.inflate(R.layout.single,null); } TextView tv_table_no=(TextView)row.findViewById(R.id.table_no); tv_table_no.setText(''+store_table_no.get(i)); return row;}}// 這個是 row.xml <?xml version='1.0' encoding='utf-8'?><RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android'android:orientation='vertical' android:layout_width='match_parent'android:layout_height='match_parent'><RelativeLayout android:layout_width='170dp' android:layout_height='100dp' android:background='@android:color/holo_green_dark' android:layout_centerHorizontal='true' android:layout_centerVertical='true'> <TextViewandroid:text='101'android:layout_width='wrap_content'android:layout_height='wrap_content'android: android:layout_centerVertical='true'android:layout_centerHorizontal='true' /></RelativeLayout></RelativeLayout>

android - 如果通過點擊更換Gridview 的指定Item 背景顏色

如果再次點擊同樣item, 窗口再次出現,點擊 ok 就返回默認顏色。

問題解答

回答1:

用selector文件做item的背景顏色例如

selector_item_bg.xml放到資源文件夾res/color/中

<?xml version='1.0' encoding='utf-8'?><selector xmlns:android='http://schemas.android.com/apk/res/android'> <item android:color='@color/red' android:state_selected='true'/> <item android:color='@color/green' android:state_selected='false'/></selector>

其中@color/red和@color/green自己添加

然后在item的layout中添加背景色, 就是你的row.xml

<?xml version='1.0' encoding='utf-8'?><RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android'android:orientation='vertical' android:layout_width='match_parent'android:layout_height='match_parent'android:background='@color/selector_item_bg'>...省略中間的內容</RelativeLayout>

關鍵代碼就是android:background='@color/selector_item_bg'把上面的selector設置成item的背景色.

當你點擊開桌的時候把對應的item的ViewsetSelected(true)就可以變成紅色了.

在代碼里面大概是修改你的onItemClick()

@Overridepublic void onItemClick(final AdapterView<?> adapterView, final View view, final int i, long l) { new AlertDialog.Builder(this) .setTitle('臺座號 '+adapterView.getItemAtPosition(i).toString()) .setMessage('確定開桌?') .setPositiveButton('是', new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) { Toast.makeText(table.this,adapterView.getItemAtPosition(i).toString()+' 已開桌,請下單。',Toast.LENGTH_SHORT).show(); // 這個view就是被點擊的item, 也就是row.xml渲染出來的view // 把它設置為selected, selector就會使其變色 // 根據selector_item_bg.xml, true對應red, false對應green view.setSelected(true);} }) .setNegativeButton('否', null) .show(); }}

關鍵點就這些, 其他細節百度吧

相關文章:
主站蜘蛛池模板: 国产亚洲福利 | 天堂素人搭讪系列嫩模在线观看 | www.一级毛片 | 国产不卡的一区二区三区四区 | 亚洲五月婷婷 | 中国日本欧美韩国18 | 黄色录像免费看 | 国产免费无遮挡精品视频 | 精品免费久久久久国产一区 | 国产成人高清亚洲一区91 | 久久国产免费观看精品1 | 亚洲精品一区二区三区 | 欧美在线一级毛片观看 | 欧美gogo高清bbw | 亚洲国产最新在线一区二区 | 精品一区二区久久 | 免费欧洲毛片a级视频 | 欧美嘿咻视频 | 日本黄色录像 | 免费观看色 | 香蕉视频在线观看网址 | 中国黄色三级 | 老子影院午夜伦不卡亚洲 | 成人看的午夜免费毛片 | 欧美日韩 国产区 在线观看 | 国产亚洲精品久久久久久久 | 亚洲狠狠婷婷综合久久久图片 | 国产伊人久久 | 国产免费jizz在线播放视频 | 天天草综合网 | 亚洲天堂久久精品成人 | 免费观看国产 | 一级aaaaaa毛片免费 | 免费在线观看一级毛片 | 亚洲一区二区三区四区在线观看 | 爱爱爱爱的视频在线观看 | 免费碰碰视频 | 在线免费看网站 | 日韩毛片在线免费观看 | 又粗又大又爽 真人一级毛片 | 妞干网在线播放 |