在适配器外更改gridview项

时间:2016-05-25 18:35:37

标签: android gridview

我使用了创建了一个水平gridview https://developer.android.com/reference/android/support/v17/leanback/widget/HorizontalGridView.html

我的gridview包含一个imageview。我想当用户点击屏幕上的按钮(按钮不在griview中)时,gridview中的特定图像会发生变化。 我试过了

adapter.notifyDataSetChanged()

但它不起作用。这是我的代码:

      public class GridElementAdapter extends RecyclerView.Adapter<GridElementAdapter.SimpleViewHolder>{

            private Context context;


    public static ArrayList<File> elements;
        public static int mPosition=0;



            public GridElementAdapter(Context context, ArrayList<File> file){
                this.context = context;
                this.elements = file;


            }

            public static class SimpleViewHolder extends RecyclerView.ViewHolder {
               ImageView button;
                ImageView cancel;

                public SimpleViewHolder(View view) {
                    super(view);
                    button = (ImageView) view.findViewById(R.id.imageView2);
                    cancel = (ImageView) view.findViewById(R.id.check);
                }
            }

            @Override
            public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                final View view = LayoutInflater.from(this.context).inflate(R.layout.grid_element, parent, false);
                return new SimpleViewHolder(view);
            }

            @Override
            public void onBindViewHolder(final SimpleViewHolder holder, final int position) {
             Glide.with(context).load(elements.get(position).toString()).into(holder.button);
                holder.cancel.setImageResource(R.drawable.cancell);
                holder.cancel.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v) {
                        elements.remove(position);
                       notifyDataSetChanged();
                    }
                });



                holder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
mPosition=position;
                        Glide.with(context).load(elements.get(position).toString()).into(holder.button);

                    }
                });
            }


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

            @Override
            public int getItemCount() {
                return this.elements.size();
            }
        }

这是mainActivity中的函数,我必须通知适配器。

 public void save_image(View v) throws IOException {

       File file = new File(new_name);


        GridElementAdapter.elements.remove(GridElementAdapter.mPosition);
        GridElementAdapter.elements.add(GridElementAdapter.mPosition, file);
       View v1=new View(context);
         v1 = horizontalGridView.getChildAt(GridElementAdapter.mPosition);


        ImageView someText = (ImageView) v1.findViewById(R.id.imageView2);
        Glide.with(this).load(GridElementAdapter.elements.get(GridElementAdapter.mPosition)).into(someText);
        adapter.notifyDataSetChanged();
     }

我尝试过这个解决方案,但它没有用。 Changing gridVIew's imageView outside of the adapter

1 个答案:

答案 0 :(得分:0)

适配器的elements属性应该是实例变量。

因此,public static ArrayList<File> elements;应该是public ArrayList<File> elements;

而不是save_image

然后在 adapter.elements.remove(GridElementAdapter.mPosition); adapter.elements.add(GridElementAdapter.mPosition, file); 方法中,您将使用

GridElementAdapter.elements.remove(GridElementAdapter.mPosition);
GridElementAdapter.elements.add(GridElementAdapter.mPosition, file);

而不是:

{{1}}