Android : GridView 未标记它加载为空 Activity

Android : GridView was Not Marked insit of that it load to empty Activity

我是 android 的新手,我知道如何创建 GridView,但我不知道如何将第一个标记为默认的 Selected 当 Fragmant 打开时.. .

当用户 Select 我想在 gridview 中标记图像时我不传递图像

gridview.xml

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/r1"
        >
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scroll"
            ><GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/gridview"
            android:columnWidth="90dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:choiceMode="singleChoice"
            android:listSelector="@android:color/transparent"
            />
        </ScrollView>
    </RelativeLayout>

图片-list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_height="60dp"
        android:layout_width="60dp"
        android:scaleType="fitCenter"
        android:focusable="false"
        android:clickable="false" />
</RelativeLayout>

网格类

public class NewgridCreate extends BottomSheetDialogFragment {



    Integer[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
    GridView gridView;
    ArrayList<imageModel> arrayList;
    public NewgridCreate() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.new_list_create, container, false);

        ImageButton done = view.findViewById(R.id.done);
        final EditText listname = (EditText) view.findViewById(R.id.listname);
        final GridView gridView = (GridView) view.findViewById(R.id.gridview);
        arrayList = new ArrayList<imageModel>();
        for (int i = 0; i < images.length; i++) {
            imageModel imagemodel = new imageModel();
            imagemodel.setmThumbIds(images[i]);
            //add in array list
            arrayList.add(imagemodel);
        }
        final ImageAdapterGridView adapterGridView = new ImageAdapterGridView(getContext(), arrayList);

        gridView.setAdapter(adapterGridView);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              //  String selectedItem = (String) adapterView.getItemAtPosition(i);
                adapterGridView.setSelectedPosition(i);
                adapterGridView.notifyDataSetChanged();
                int imageRes = images[i];
               Intent intent = new Intent(getContext(),CheckslateHome.class);
              intent.putExtra("IMAGE_RES", imageRes);
               startActivity(intent);
            }
        });

适配器类

 private int selectedPosition = -1;

 public View getView(int i, View view, ViewGroup viewGroup) {

        if (view==null)
        {
           view = LayoutInflater.from(context).inflate(R.layout.image_list, viewGroup, false);
           // gridView.setDrawSelectorOnTop(false);

        }

        ImageView imageView;
        imageView = (ImageView) view.findViewById(R.id.image);
        imageView.setImageResource(arrayList.get(i).getmThumbIds());

        if (i == selectedPosition) {

            view.setBackgroundColor(Color.WHITE);
        } else {
            view.setBackgroundColor(Color.TRANSPARENT);
        }
        return view;


    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

例子

复制并粘贴这个

public class NewListCreate extends BottomSheetDialogFragment {



    int[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
    int imageRes;

    public NewListCreate() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

         View view = inflater.inflate(R.layout.new_list_create, container, false);

        ImageButton done = view.findViewById(R.id.done);
        final EditText listname = (EditText) view.findViewById(R.id.listname);
        final GridView gridView = (GridView) view.findViewById(R.id.gridview);

        final CustomAdpter customAdpter = new CustomAdpter(images,getContext());
        gridView.setAdapter(customAdpter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                customAdpter.selectedImage = i;
                customAdpter.notifyDataSetChanged();
                imageRes = images[i];

            }
        });




        done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String itemname = listname.getText().toString();
                if (!TextUtils.isEmpty(listname.getText().toString())) {

                    startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image",imageRes));
                    dismiss();
                } else {
                    Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
                }

            }

       });

        
        return view;


    }

自定义适配器

    public class CustomAdpter extends BaseAdapter{

        private int[] icons;
        private Context context;
        private LayoutInflater layoutInflater;
        public int selectedImage = 0;

        public CustomAdpter(int[] icons, Context context) {
            this.icons = icons;
            this.context = context;
            this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
    public int getCount() {
        return icons.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

            if (view == null)
            {
               view =  layoutInflater .inflate(R.layout.image_list,viewGroup,false);

            }

            ImageView imageicons = view.findViewById(R.id.image);
        if (i < icons.length) {

            imageicons.setImageResource(icons[i]);

            if (i != selectedImage) {
               imageicons.setImageAlpha(50);
            }
            imageicons.setScaleType(ImageView.ScaleType.CENTER_CROP);
           // imageicons.setLayoutParams(new GridView.LayoutParams(150, 150));
            if (i ==  selectedImage) {

                view.setBackgroundColor(Color.WHITE);
            } else {
                view.setBackgroundColor(Color.TRANSPARENT);
            }
        };


        return view;
    }
}
}