android - gridview 我想让它点击一项 那项变红 但是我的代码的效果很诡异,点击后那项会变红,但是有的没点的项也变红。
ringa_lee
ringa_lee 2017-04-17 17:36:54
0
2
399
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

全部回覆(2)
黄舟

這個和gridview的item復用有關,這個是很常見的吧,你點position 0,那麼你滑到第二頁第一個也會變紅,因為它復用了那個紅色的。復用是為了內存,題主百度下 listview item復用吧,這是一個很經典古老的東西了。最好的做法是,用java類別把每個item的紅色狀態記錄下來,這樣就不會有問題了

刘奇

你透過View child = parent.getChildAt(position);来获取点击视图,然后再调用child.setBackgroundColor(Color.RED);看效果如何。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板