Android Gridview滚动后未保存选择



嗨,我需要帮助找出为什么我的android网格视图中的选择不起作用。我正在尝试在我的应用程序中添加功能,允许用户突出显示所选项目。它正在工作(即所选项目高亮显示(,但我一滚动,选择就消失了。此外,如果我选择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()将在您滚动顶部或底部时被调用,视图将重置为初始视图,因此在预订模型中维护状态数组或状态布尔值,并比较布尔值以检查是否选择

最新更新