滚动的gridview生成随机点击事件

时间:2017-02-11 13:36:30

标签: java android gridview

//ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
    private Context context;
    String list_app_name[] = {"calendar","weather","news","youtube","time","gmail","gmap","twitter","facebook","hmb","uber","mirror_mode"};

    private final List<MyModel> items;
    public ImageAdapter(Context context, List<MyModel> items) {
        this.context = context;
        this.items = items;
    }

    public View getView(int position, final View convertView, ViewGroup parent) {


        View gridView;

        if (convertView == null) {  
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            gridView = inflater.inflate(R.layout.item, parent, false);
        } else {
            gridView = (View) convertView;
        }

        final MyModel item = items.get(position);

        // set value into imageview---------
        ImageView image = (ImageView) gridView.findViewById(item_image);
        ToggleButton button_ = (ToggleButton) gridView.findViewById(R.id.item_button);

//        Log.d("adapter crash",list_app_name[position]);
        SharedStorage st = new SharedStorage(context);
        try {
            this.isAppSynced = st.getSyncedApps();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if(isAppSynced.get(list_app_name[position])) {
            image.setImageResource(item.getItem_image());
        } else {
            image.setImageResource(item.getDown_image());
        }

        // set value into textview
        TextView text = (TextView) gridView.findViewById(item_text);
        text.setText(item.getItem_text());
        //------------------------------------------------------


        //the click listener for image and buttons are set here.    
        setClickListenerForImage(image,position);

        setClickListenerForButton(button_,position);

        return gridView;
    }
}



//MainActivity.java
//setting up the adapter for gridView
    protected void onCreate(Bundle savedInstanceState) {
        ...
        grid = (GridView)findViewById(R.id.simpleGrid);

        //get the mitems list to be initialised with the images, text and other items to be rendered by the adapter.
        generateData();

        ia = new ImageAdapter(this,mItems);
        grid.setAdapter(ia);
        ...
    }

我正在使用自定义gridview,gridview的每个项目都有一个ImageView,ToggleButton,TextView。

在这些视图中,单击ImageView和ToggleButton。所以我为ImageAdapter.java中的每个项目设置了两个点击监听器。

我面临的问题是某些特定项目的ToggleButton会在滚动时自行点击。

当我滚动gridview时,项目被随机点击,并且相应的事件发生与该特定项目的点击监听器相关联。

0 个答案:

没有答案