[嗨,我需要帮助以找到为什么我的Android gridview中的选择不起作用的原因。我正在尝试在我的应用程序中添加功能,以允许用户突出显示所选项目。它正在工作(即,选中的项目突出显示),但是一旦我滚动,所选内容就会消失。另外,如果我选择6个单元格,则突出显示第8个单元格。
下面是代码:XML
<TableRow
android:layout_width="wrap_content"
android:layout_height="@dimen/student_activity_first_row"
android:layout_marginTop="10dp">
<RelativeLayout
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="30dp">
<!--grid view for students goes here-->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_marginTop="90dip"
android:layout_width="fill_parent"
android:layout_height="330dp"
android:numColumns="auto_fit"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_marginBottom="20dp"
>
</GridView>
</RelativeLayout>
</TableRow>
下面是GridView的绑定和选择:
private void BindData(String filter) {
GridView gridView = findViewById(R.id.grid_view);
CommonUtils utils = new CommonUtils();
String dropdate = utils.getTodaysDate();
final Booking h = new Booking();
bookingsList = new ArrayList<Booking>();
bookingsList = h.getAllBookingsForClassroom(ASCActivity.this, room_id, dropdate,filter);
adapter = new BookingGridViewAdapter(ASCActivity.this, bookingsList,true);
gridView.setAdapter(adapter);
final SharedPreferences pref = this.getSharedPreferences(SETTINGS_PREFS_NAME, Context.MODE_PRIVATE);
// OnClick
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
GridView gridView = ASCActivity.this.findViewById(R.id.grid_view);
final int size = gridView.getChildCount();
CommonUtils utils = new CommonUtils();
for(int i = 0; i < size; i++) {
ViewGroup gridChild = (ViewGroup) gridView.getChildAt(position);
if (gridChild != null) {
LinearLayout tv = (LinearLayout) gridChild.findViewById(R.id.parentPanel);
int color = ((ColorDrawable)tv.getBackground()).getColor();
if(color == -4591){
tv.setBackgroundColor(Color.parseColor("#ffee12"));
}else{
tv.setBackgroundColor(Color.parseColor("#ffee11"));
}
break;
}
}
}
}
[BindData()
将在您滚动到顶部或底部时被调用,并且视图将重置为初始状态,因此在Booking模型中维护状态数组或状态布尔值,并比较布尔值以检查是否选中]