android - 如果通过点击更换Gridview 的指定Item 背景颜色
巴扎黑
巴扎黑 2017-04-17 17:48:00
0
1
700

我的情况是这样的。 主界面是用Gridview展示出餐厅里的餐桌情况。

默认情况下,每个桌子都是绿色背景。 如果点击该item, 会出现一个Dialog 窗口;提示是否开桌子。 如果点击是,该item的背景颜色更换为红色。

以下是我的代码,请大神们指点迷津!

public class table extends AppCompatActivity implements AdapterView.OnItemClickListener{

GridView gridView;

@Override
protected 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);
}

@Override
public 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() {
                @Override
                public 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]);
    }
}

@Override
public int getCount() {
    return store_table_no.size();
}

@Override
public Object getItem(int i) {
    return store_table_no.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public 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">

    <TextView
        android:text="101"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/table_no"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>

如果再次点击同样item, 窗口再次出现,点击 ok 就返回默认颜色。

巴扎黑
巴扎黑

reply all(1)
左手右手慢动作

Use the selector file to set the background color of the item
for example 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()

selector_item_bg.xml is placed in the resource folder res/color/

@Override
public 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() {
                @Override
                public 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();
    }
}
Among them, @color/red and @color/green add them yourself #🎜🎜# #🎜🎜#Then add a background color to the item’s layout, which is your row.xml#🎜🎜# rrreee #🎜🎜#The key code is android:background="@color/selector_item_bg" and set the above selector to the background color of the item.#🎜🎜# #🎜🎜#When you click to open the table, the ViewsetSelected(true) of the corresponding item will turn red.#🎜🎜# #🎜🎜#In the code, probably modify your onItemClick()#🎜🎜# rrreee #🎜🎜#These are the key points, other details can be found on Baidu#🎜🎜#
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!