gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:06:14 34 4
gpt4 key购买 nike

我是 android 新手,我知道如何创建一个 GridView,但我不知道如何将第一个标记为默认的,当 Fragmant 打开时...
当用户选择我想在 gridview 中标记图像的一个时,我不传递图像
网格 View .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>
图像列表.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;
}
例子
enter image description here

最佳答案

复制并粘贴这个

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;
}
}
}

关于安卓 : GridView was Not Marked insit of that it load to empty Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63439054/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com