禁用列表视图的滚动并启用整个布局

2023-11-23

您好,我目前正在开发一个 Android 应用程序,它在主活动中有两个列表视图。我想要的是禁用两个列表的滚动并仅允许整个页面滚动,有什么方法可以帮助... 。 我的代码 包com.example.listviewdemo;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.Toast;

public class MainActivity extends Activity {

ListView list,list2;
String[] name={"Happy","always","try","hard","you will","get it!","Believe","in","God","everything","will","work well!","Believe","in","God","everything","will","work well!"};
String[] name2={"Believe","in","God","everything","will","work well!","Believe","in","God","everything","will","work well!"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView) findViewById(R.id.listview);
    list2 = (ListView) findViewById(R.id.listview2);
    list.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name));
  list2.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name2));



    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), name [position],Toast.LENGTH_SHORT).show();
        }
    });

list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), name2 [position],Toast.LENGTH_SHORT).show();
        }
    });   
}   
  }

我的xml代码是:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="${relativePackage}.${activityClass}" >
    <TextView
    android:id="@+id/text_id1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
  android:text="@string/str1" />
   <ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"
    />
    <TextView
      android:id="@+id/text_id2"
     android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/str2" 

     />

      <ListView
    android:id="@+id/listview2"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"
       />
      </LinearLayout>

你可以试试这个。

对于 xml 部分,执行以下操作:

将整个布局数据放在一个 Scroll View 下,例如:

    <ScrollView
        android:id="@+id/scrollViewId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >             

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" /> // SAY YOUR FIRST LIST VIEW:

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" /> // SAY YOUR SECONDLIST VIEW:

                 // Add your other views as per requirement....

        </LinearLayout>

    </ScrollView>

现在在 JAVA 类中执行以下操作...

只需在将适配器设置为列表视图后将此自定义方法添加到您的代码中即可:

setListViewHeightBasedOnChildren(listview)

例如:

      list = (ListView) findViewById(R.id.listview);
      list.setAdapter(new ArrayAdapter<String> 
                      (MainActivity.this,android.R.layout.simple_list_item_1,name));
      setListViewHeightBasedOnChildren(list);

对第二个列表视图也执行相同的操作。

这是 setListViewHeightBasedOnChildren 方法的主体

   public static void setListViewHeightBasedOnChildren(ListView listView) 
{
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight=0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) 
    {
        view = listAdapter.getView(i, view, listView);

        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,  
                                      LayoutParams.MATCH_PARENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

希望对你有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

禁用列表视图的滚动并启用整个布局 的相关文章

随机推荐

  • 积分推广

    在整数提升方面 什么时候有符号整数无法表示原始类型的所有值 来自文本 K R C 编程语言 第二版 p 174 A 6 1 积分推广 字符 短整数或整数位域 无论是否有符号 或枚举类型的对象都可以在 凡是可以使用整数的表达式 如果一个int
  • 进行网络通话的最佳地点

    网络通话 static func getProfile parameters String AnyObject onComplete String AnyObject gt var requiredData String AnyObject
  • Namenode HA(UnknownHostException:nameservice1)

    我们通过 Cloudera Manager 启用 Namenode 高可用性 使用 Cloudera Manager gt gt HDFS gt gt 操作 gt 启用高可用性 gt gt 选定的备用名称节点和日志节点 然后名称服务1 整个
  • Wor2vec 微调

    我需要微调我的 word2vec 模型 我有两个数据集 data1 and data2 到目前为止我所做的是 model gensim models Word2Vec data1 size size v window size w min
  • 使用 jQuery 迭代 JavaScript 对象的属性

    是否有一种 jQuery 方法可以对对象的成员执行迭代 例如 for var member in obj 我只是不喜欢这个for从我可爱的 jQuery 符号中脱颖而出 each name John lang JS function i n
  • 使用 lm(poly) 获取公式系数[重复]

    这个问题在这里已经有答案了 我正在尝试使用 lm poly 获得某些点的多项式回归 但对它返回的回归公式系数有一些疑问 像这样的样本 x seq 1 100 y x 2 3 x 7 fit lm y poly x 2 结果是 lm form
  • EPOLLRDHUP 不可靠

    我正在通过客户端 服务器 TCP 连接使用非阻塞读 写epoll wait 问题是 我无法使用以下方法可靠地检测 对等关闭连接 事件EPOLLRDHUP旗帜 经常会发生标志未设置的情况 客户端使用close 大多数时候 服务器从epoll
  • 如何在 CSS 选择器或 jQuery 中处理 XML 命名空间?

    我使用 jQuery 来解析 RSS 提要 我可以使用 AJAX 成功获取 RSS 提要 get podcast xml function data xml data xml 现在我可以通过使用获取播客的标题xml find channel
  • data.table fread如何忽略空行[重复]

    这个问题在这里已经有答案了 看起来如果文件中的第二行为空 则不会读取列名 我已经使用过文档中的 header skip 等开关 但无法使其工作 如果我的文件中的第二行为空 如何忽略这个事实并仍然将第一行读取为列名 第二行是空的 gt fre
  • 为什么使用 sys.path.append(path) 而不是 sys.path.insert(1, path)?

    Edit 根据 Ulf Rompe 的评论 使用 1 而不是 0 很重要 否则你会打破sys path 我已经做Python有一段时间了 一年多了 我总是很困惑为什么人们推荐你使用sys path append 代替sys path ins
  • 将应用程序更新至 iOS6

    在苹果开发者论坛上未能找到对此问题的准确答复 与其他 Apple 开发人员一样 我将升级我们的应用程序以支持 iOS6 设备 我已经下载了支持iOS6 SDK的XCode 4 5 我知道我无法使用此 XCode 版本将应用程序的版本提交到应
  • 使用 Java API 从 Elasticsearch 获取所有记录

    我正在尝试使用 Java API 从 Elasticsearch 获取所有记录 但我收到以下错误 n Wild Thing localhost 9300 索引 data read search phase dfs 嵌套 QueryPhase
  • python中“import a.b as b”和“from a import b”有什么区别[重复]

    这个问题在这里已经有答案了 我一直用from a import b但最近一个工作团队决定将一个模块移动到一个新的命名空间中 并发出警告通知 告诉人们更换import b with import a b as b 我从来没有用过import
  • SQL Server 如何处理超时请求?

    假设我使用 C 运行一个长时间运行的 SQL Server 存储过程 假设 30 分钟 进一步假设我在 C 中的查询上设置了 1 小时的超时期限 这样如果出于某种原因该 SP 花费的时间比预期长 我最终不会垄断数据库 最后 假设该存储过程中
  • 在...中找不到 JNI_OnLoad 跳过 init

    我在过去一天左右就收到了这条消息 到目前为止还没有造成任何问题 我早些时候让我的代码与我的本机库一起运行 但今天我添加了一些新功能 但它再次无法工作 LogCat 中没有显示任何错误 但我的程序只是在我的设备上立即打开和关闭 没有任何消息表
  • Jquery 移动粘性页脚

    我想要 Jquery Mobile 中的页脚 它不是固定的 但始终位于页面底部 像这样 http ryanfait com sticky footer 但在 JQuery Mobile 中 不像标准的 JQuery Mobile 固定页脚
  • 如何捕获滚动事件?

    我想实现无限滚动 下面是我的布局的简短形式 由于我有一些元素相对定位 因此 javascript 滚动事件不会触发 如何解决这个问题以便触发滚动事件并实现无限滚动 我的主要布局是 div div class wrapper div div
  • 如何获取站点的物理路径属性

    当我仅列出具有默认格式的站点时 它会显示物理路径 PS C Windows system32 gt sm Get IISServerManager PS C Windows system32 gt sm Sites Name ID Stat
  • 如何获取 WebKit 中所有受支持的 CSS 属性?

    在 Firefox Opera 和 IE 中我可以通过以下方式获取它们 gt gt for k in document body style console log k gt opacity background height textAl
  • 禁用列表视图的滚动并启用整个布局

    您好 我目前正在开发一个 Android 应用程序 它在主活动中有两个列表视图 我想要的是禁用两个列表的滚动并仅允许整个页面滚动 有什么方法可以帮助 我的代码 包com example listviewdemo import android