Android-GridView中带有复选框的项目选择 [英] Item-Selection with Checkbox in Android-GridView
问题描述
我遇到了Android GridView的问题.我已经尝试过Stackoverflow提供的各种解决方案,但实际上都没有.
I've come to a problem with the Android GridView. I already tried various solutions from Stackoverflow but none of them actually worked.
我正在借助ArrayAdapter填充GridView.适配器会放大仅包含图像和复选框的布局.我正在使用该"convertView"功能,以防止在添加图像时出现闪烁(将近500张图像添加到GridView中).
I'm populating a GridView with the help of an ArrayAdapter. The Adapter inflates a layout containing only an image and a checkbox. I'm using that "convertView"-thing in order to prevent flickering when the images get added (nearly 500 images are getting added to the GridView).
问题:当我选中一个复选框并滚动时,选中状态消失了.另外,例如,当我选中多个GridView项的复选框时,以下所有元素的选择模式都会无休止地持续下去.
The Problem: When I check a checkbox and scroll, the checked-state disappears. Also, when I for instance check multiple GridView item's checkboxes, the pattern of selection endlessly continues for all the following elements.
我的GridView是RelativeLayout的子元素. RelativeLayout用作ViewPager中使用的片段的根元素:
My GridView is the child-element of a RelativeLayout. The RelativeLayout is used as the root element of a fragment being used in a ViewPager:
<GridView
android:id="@+id/MainGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/appBackgroundColor"
android:drawSelectorOnTop="false"
android:listSelector="#00000000"
android:numColumns="2"
android:paddingLeft="6dp"
android:paddingTop="5dp"
android:paddingRight="6dp"
android:translationZ="2dp" />
我正在借助线程来填充GridView,该线程正在从网站下载图像数据.我认为这应该没有问题,但是为了完整起见,下面是代码:
I'm populating the GridView with the help of a thread, which is downloading image-data from a website. I think there shouldn't be a problem with this, but for completeness, here is the code:
//Declaring the Array
ArrayList<GridImage> ImageArray = new ArrayList<>();
//And the adapter
Adapter_GridAdapter GridViewAdapter = new Adapter_GridAdapter (getActivity().getApplicationContext(), ImageArray );
//The following is executed 500 times in a thread
ImageArray.add(new GridImage(URL));
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
GridViewAdapter.notifyDataSetChanged();
}
});
GridView适配器的代码:
The code of the GridView adapter:
public class Adapter_GridAdapter extends ArrayAdapter<GridImage> {
Context context;
public Adapter_GridAdapter (Context context, ArrayList<GridImage> SearchResults) {
super(context, 0, SearchResults);
this.context = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.griditem_ig_post, null);
final ImageView ThisViewIMG = view.findViewById(R.id.GridImageView);
//Calculating Image Height (Square Image)
ThisViewIMG.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
ThisViewIMG.removeOnLayoutChangeListener(this);
ThisViewIMG.getLayoutParams().height = ThisViewIMG.getWidth();
ThisViewIMG.requestLayout();
}
});
}
final GridImage ThisImg = getItem(position);
final ImageView ThumbnailImage = view.findViewById(R.id.GridImageView);
//THIS IS THE CUSTOM CHECKBOX!
final CustomCheckBox scb = (CustomCheckBox) view.findViewById(R.id.MyCheckBox);
//I AM SAVING THE VALUE IN THE OBJECT (TRY #38181 THAT DIDN'T WORK)
scb.setChecked(ThisImg.IsChecked);
scb.setOnCheckedChangeListener(new CustomCheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CustomCheckBox checkBox, boolean isChecked) {
Log.d("CustomCheckBox", String.valueOf(isChecked));
ThisImg.setChecked(isChecked);
}
});
Glide.with(context).load(Post.IMG_ThumbnailURL).fitCenter().into(ThumbnailImage);
return view;
}
}
对象本身非常简单,问题(我认为)也不应该隐藏在这里:
The Object itself is quite simple, the problem (i think) also shouldn't hide in here:
public class GridImage{
public String PostURL;
public Boolean IsChecked = false;
GridImage(String URL){
PostURL = URL;
}
public void setChecked(Boolean checked) {
IsChecked = checked;
}
}
我已经尝试过的:
What I already tried:
- https://stackoverflow.com/a/24016687/7850133 -这是我的解决方案,仅使用标签而不是标签直接存入物体中,对吗?
- https://stackoverflow.com/a/17188179/7850133 -SparseBooleanArray,但我不是100 %确保我以正确的方式完成了操作
- https://stackoverflow.com/a/24016687/7850133 - Which is my solution just with tags instead of saving straight into the object, am I right?
- https://stackoverflow.com/a/17188179/7850133 - SparseBooleanArray, but I'm not a 100% sure I did this the right way
推荐答案
在使用ListView/GridView适配器时,我总是避免使用 final 和 onCheckedChangeListener ,因为两者都可能有时甚至逻辑似乎也有问题,因此请尝试以下代码:
When doing with ListView/GridView adapters, I always avoid to have final and onCheckedChangeListener because both may have problem sometimes even the logic seems right, so try the code below:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.griditem_ig_post, null);
ImageView ThisViewIMG = view.findViewById(R.id.GridImageView);
//Calculating Image Height (Square Image)
ThisViewIMG.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
v.getLayoutParams().height = v.getWidth();
v.requestLayout();
}
});
}
GridImage ThisImg = getItem(position);
ImageView ThumbnailImage = view.findViewById(R.id.GridImageView);
//THIS IS THE CUSTOM CHECKBOX!
CustomCheckBox scb = (CustomCheckBox) view.findViewById(R.id.MyCheckBox);
scb.setChecked(ThisImg.IsChecked);
scb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomCheckBox cb = (CustomCheckBox)view;
int position = (int)cb.getTag();
GridImage ThisImg = getItem(position);
Log.d("CustomCheckBox", String.valueOf(!ThisImg.IsChecked));
ThisImg.setChecked(!ThisImg.IsChecked);
cb.setChecked(ThisImg.IsChecked);
}
});
Glide.with(context).load(Post.IMG_ThumbnailURL).fitCenter().into(ThumbnailImage);
scb.setTag(position);
return view;
}
希望有帮助!
这篇关于Android-GridView中带有复选框的项目选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!