如何将CustomListView中的所有CheckBox从GONE更改为VISIBLE?

时间:2017-04-10 14:48:10

标签: android listview checkbox

我正在尝试创建一个小的FileExplorer。 在custom_list.xml中,我定义了一个CheckBox(Visible = GONE),一个图像和2个textViews。我想要的是,当我执行LongItemClick时,列表中的每个CheckBox都会将可见性更改为View.VISIBLE。 我已经用getChildCount()尝试过了。但问题是,它只适用于绘制,而不是当你向下滚动时。

那么如何通过对列表视图中的项目进行长按来显示所有CheckBox?

customAdapter

    adapter = new CustomListAdapter(arrCurrentDirFolders, subheadListFolders, imgList, getActivity().getApplicationContext());

    listView.setAdapter(adapter);

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                       int pos, long id) {
            // TODO Auto-generated method stub

            HERE I WANT TO SET VISIBILITY = VISIBLE FOR ALL CHECKBOXES 

            /*for(int i = 0; i < listView.getChildCount(); i++) {
                listView.getChildAt(i).findViewById(R.id.checkBox_checkDir).setVisibility(View.VISIBLE);
            }*/

            return true;
        }
    });

    // set the list item on click listener
    listView.setOnItemClickListener(newAdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

        Log.v("Position: ", position+" pressed");

        }
    });

custom_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/checkBox_checkDir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/img_customlist_icon"
        android:layout_marginLeft="5dp"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/checkBox_checkDir" />

    <TextView
        android:id="@+id/headingText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_alignTop="@+id/img_customlist_icon"
        android:layout_toEndOf="@+id/img_customlist_icon" />

    <TextView
        android:id="@+id/subHeadingText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_below="@+id/headingText"
        android:layout_alignStart="@+id/headingText" />

</RelativeLayout>

List all Elements from InternalStorage LongClick on an Element

1 个答案:

答案 0 :(得分:0)

您可以轻松地将一个名为isCheckBoxVisible的参数放在您传递给适配器的列表对象中,一旦您长按make for循环并将列表isCheckBoxVisible更新为true,该参数将为false然后在notifydatasetchanged();上的isCheckBoxVisible检查 public void ddlSO_SelectedIndexChanged(object sender, EventArgs e) { String strConnString = ""; String strQuery = "select strServiceOrderNo, strCustomerNo, strCustomerName, strCustomerAddress1, strCustomerAddress2, strCustomerAddress3, strServiceRequestDate, strServiceOrderDate, intStatusCodeID, intRepID from TServiceOrders where" + " intServiceOrderID = @intServiceOrderID"; SqlConnection con = new SqlConnection(strConnString); SqlCommand cmd = new SqlCommand(); cmd.Parameters.AddWithValue("@intServiceOrderID", ddlSO.SelectedItem.Value); cmd.CommandType = CommandType.Text; cmd.CommandText = strQuery; cmd.Connection = con; try { con.Open(); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { txtCUSTNO.Text = sdr["strCustomerNo"].ToString(); txtSONO.Text = sdr["strServiceOrderNo"].ToString(); txtAddress1.Text = sdr["strCustomerAddress1"].ToString(); txtAddress2.Text = sdr["strCustomerAddress2"].ToString(); txtAddress3.Text = sdr["strCustomerAddress3"].ToString(); txtDateCreated.Text = sdr["strServiceOrderDate"].ToString() ; txtDateDue.Text = sdr["strServiceRequestDate"].ToString(); txtCustName.Text = sdr["strCustomerName"].ToString(); } } catch (Exception ex) { throw ex; } finally { con.Close(); con.Dispose(); } } 如果true设置复选框为可见,如果false设置复选框为Gone