android - gridview 我想让它点击一项 那项变红 但是我的代码的效果很诡异,点击后那项会变红,但是有的没点的项也变红。
ringa_lee
ringa_lee 2017-04-17 17:36:54
0
2
396
public class MainActivity extends Activity {

    GridView grid;
    ImageView imgview;
    int img=R.drawable.ic_launcher;
    int []imageids=new int []{img,R.drawable.sxq1,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,img,R.drawable.sxq2,img,R.drawable.sxq1,img,img,img,img,img,img,img,img};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
        for(int i=0;i<imageids.length;i++)
        {
            
            Map<String, Object> listItem = new HashMap<String, Object>();
            listItem.put("image", imageids[i]);
            listItems.add(listItem);
        }
     // 获取显示图片的ImageView
        imgview = (ImageView) findViewById(R.id.imageview);
             // 创建一个SimpleAdapter
             SimpleAdapter simpleAdapter = new SimpleAdapter(this,
                     listItems
                     // 使用/layout/cell.xml文件作为界面布局
                     , R.layout.cell, new String[] { "image" },
                     new int[] { R.id.image1 });
             grid = (GridView) findViewById(R.id.grid1);
             // 为GridView设置Adapter
             grid.setAdapter(simpleAdapter);
             // 添加列表项被选中的监听器
             grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
            {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id)
                {
                    // 显示当前被选中的图片
                    imgview.setImageResource(imageids[position]);
                    System.out.println("select");
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent)
                {
                }
            });
            // 添加列表项被单击的监听器
            grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id)
                {
                    // 显示被单击的图片
                    imgview.setImageResource(imageids[position]);
                    view.setBackgroundColor(Color.RED);
                    System.out.println("click");
                }
            });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
ringa_lee
ringa_lee

ringa_lee

reply all(2)
黄舟

This is related to the reuse of gridview items. This is very common. If you click position 0, then when you slide to the second page, the first one will also turn red, because it reuses the red one. Reuse is for memory. Let’s reuse listview items on Baidu. This is a very classic and ancient thing. The best way is to use a java class to record the red status of each item, so that there will be no problems

刘奇

You can see the effect through View child = parent.getChildAt(position);来获取点击视图,然后再调用child.setBackgroundColor(Color.RED);.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template