【问题标题】:Make an ExpandableListView and Textview scroll together使 ExpandableListView 和 Textview 一起滚动
【发布时间】:2016-05-29 12:35:15
【问题描述】:

我有这个代码 xml 布局,我试图在圆形 android 磨损手表中实现。我希望ExpandableListView 上方的TextView 与它一起滚动,而不是停留在顶部并占用空间。

我尝试使用ScrollView 而不是RelativeLayoutScrollViewRelativeLayout 中,反之亦然)但是这样做是LinearLayout 需要为ExpandableListView 设置一个高度显示由于列表动态变化而不可能的列表(因此它可以具有随机数量的组成员),如果我使用“wrap_content”,它一次只显示一个组。每个组也有 4 个子成员。

谁能告诉我如何使这个布局适合我,以便TextViewExpandableListAdapter 一起滚动,而无需为ExpandableListAdapter 设置高度?

round_activity_card_view_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jawad.wifihotspotfinder.CardViewLayout"
    tools:deviceIds="wear_round"
    android:background="#FF3C00"
    android:smoothScrollbar="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="20dp"
            android:paddingLeft="15dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cardViewHeader"
                android:textSize="17sp"
                android:textColor="@color/white"
                android:fontFamily="sans-serif"
                android:paddingLeft="20dp"
                android:paddingRight="5dp"/>

            <ExpandableListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/round_expandableListView"
                android:background="#FF3C00"
                android:choiceMode="multipleChoice"
                android:groupIndicator="@null"
                android:longClickable="true"/>

        </LinearLayout>

</ScrollView>

【问题讨论】:

    标签: android xml textview scrollview expandablelistview


    【解决方案1】:

    查看addHeaderView(View v)。以编程方式创建您的 TextView 或从 XML 扩展,然后使用该方法将其添加到您的 ExpandableListView

    TextView 应与您的 ExpandableListView 一起滚动。

    【讨论】:

      【解决方案2】:

      谢谢你告诉我该怎么做,亚历克斯。我必须想出一些方法来做这件事,这花了很长时间,但最终还是做对了。感谢您的指导,这对我帮助很大!使用代码如下:

      LinearLayout mLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.expandable_header, expListView, false);
      mLayout.setLayoutParams(new AbsListView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
      expListView.addHeaderView(mLayout);
      

      还有修改后的xml文件:

      expandable_header.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:background="#FF3C00">
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/expCardViewHeader"
              android:textSize="17sp"
              android:textColor="@color/white"
              android:fontFamily="sans-serif"
              android:paddingLeft="20dp"
              android:paddingRight="5dp"
              android:paddingBottom="2dp"
              android:background="#FF3C00"
              android:text="@string/no_results"/>
      
      </LinearLayout>
      

      round_activity_card_view_layout.xml

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="com.jawad.wifihotspotfinder.CardViewLayout"
          tools:deviceIds="wear_round"
          android:background="#FF3C00"
          android:smoothScrollbar="true">
      
          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:background="#FF3C00"
              android:paddingTop="20dp"
              android:paddingLeft="15dp"
              android:paddingRight="10dp"
              android:paddingBottom="10dp">
      
              <ExpandableListView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/round_expandableListView"
                  android:background="#FF3C00"
                  android:choiceMode="multipleChoice"
                  android:groupIndicator="@null"
                  android:longClickable="true"
                  android:smoothScrollbar="true" />
      
          </LinearLayout>
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 2017-12-21
        • 2012-10-09
        • 2017-03-21
        • 1970-01-01
        • 1970-01-01
        • 2012-04-10
        相关资源
        最近更新 更多