【发布时间】:2020-02-03 17:46:38
【问题描述】:
在网格视图中选择屏幕上存在的视图但向下滚动时我无法选择它
我正在使用gridview,在选择项目时,我将所选项目背景设置为绿色,取消选择透明,但如果我向下滚动以选择更多项目,它会随机选择并取消选择项目,请帮助修复它。
studentGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
desiredBackgroundColor = Color.rgb(0, 255, 0);
try {
Student student = (Student) parent.getItemAtPosition(position);
String stuName = student.getStudent_name();
String stuId = student.getStudent_nid();
view = studentGridView.getChildAt(position);
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
clickCount++;
if (viewColor == null) {
view.setBackgroundColor(desiredBackgroundColor);
mainclickCount = clickCount;
//clickCount++;
//mainclickCount = clickCount;
// Toast.makeText(getApplicationContext(), "firstclick: " + clickCount, Toast.LENGTH_SHORT).show();
Log.d("clickCounttt", "" + mainclickCount);
studentselList.add(stuName);
studentselnidList.add(stuId);
Log.d("viewColor", "" + viewColor);
Log.d("studentselListdee", studentselList.toString());
if (mainclickCount > 0) {
doneIv.setVisibility(View.VISIBLE);
} else {
doneIv.setVisibility(View.GONE);
}
String studentselListStr = studentselList.toString().replace("[", "").replace("]", "");
showstudentTv.setText(studentselListStr);
if (isStudentClick == true) {
showstudentTv.setText(studentselListStr);
} else if (isStaffClick == true) {
showstaffTv.setText(studentselListStr);
}
return;
}
int currentColorId = viewColor.getColor();
Log.d("currentColorId", "" + currentColorId);
if (currentColorId == desiredBackgroundColor) {
view.setBackgroundColor(Color.TRANSPARENT);
clickCount--;
mainclickCount = clickCount;
studentselList.remove(stuName);
studentselnidList.remove(stuId);
String studentselListStr = studentselList.toString().replace("[", "").replace("]", "");
showstudentTv.setText(studentselListStr);
if (isStudentClick == true) {
showstudentTv.setText(studentselListStr);
} else if (isStaffClick == true) {
showstaffTv.setText(studentselListStr);
}
} else {
view.setBackgroundColor(desiredBackgroundColor);
clickCount++;
mainclickCount = clickCount;
studentselList.add(stuName);
String studentselListStr = studentselList.toString().toString().replace("[", "").replace("]", "");
showstudentTv.setText(studentselListStr);
}
Log.d("clickCountmainn", "" + mainclickCount);
Log.d("studentselListtt", studentselList.toString());
if (mainclickCount > 0) {
doneIv.setVisibility(View.VISIBLE);
} else {
doneIv.setVisibility(View.GONE);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
xml 文件
<GridView
android:id="@+id/studentGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:descendantFocusability="afterDescendants"
android:scrollingCache="false"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"
android:layout_marginBottom="@dimen/margin_1"
android:layout_below="@+id/classselLayout"/>
<RelativeLayout
android:id="@+id/doneLayout"
android:layout_width="@dimen/margin_150"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/done"
android:layout_width="@dimen/margin_40"
android:layout_height="@dimen/margin_40"
android:src="@drawable/done1"
android:visibility="visible"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="@+id/cancel"
android:layout_width="@dimen/margin_40"
android:layout_height="@dimen/margin_40"
android:src="@drawable/cross"
android:layout_alignParentRight="true"/>
</RelativeLayout>
【问题讨论】:
标签: android