【问题标题】:Android Gridview not holding selection after scroll滚动后Android Gridview不保持选择
【发布时间】:2020-02-21 13:00:30
【问题描述】:

您好,我需要帮助来找出我的 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;
                }
            }

    }

}

【问题讨论】:

    标签: java android android-gridview


    【解决方案1】:

    BindData() 将在您滚动顶部或底部时调用,并且视图将重置为初始状态,因此请在 Booking 模型中维护状态数组或状态布尔值并比较布尔值以检查是否选中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多