草庐IT

android - 为 ListView 的特定子项应用 android 选择器,并在同一列表项中的其他子项上禁用选择器

coder 2023-12-19 原文

我有一个 ListView ,每个项目包含 2 个 View (这个 ListView 看起来像一个有 2 列的 GridView)。 我需要页眉和页脚,但它们在 GridView 中不可用,所以我转而使用 ListView 。

我想要一个用于每个列表项的单独 subview 的选择器,列表选择器不起作用,它将选择器应用于两个 subview 。

到目前为止我尝试了什么????

我将每个元素的 Root View 设为 FrameLayout 并使用 android:foreground="@drawable/some_selector" 属性,这样我就可以模拟 drawSelectorOnTop 属性。

我的问题:

如何才能将选择器仅应用于 listview 项目 中按下的子项。

ListView 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/episode_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/black"
        android:dividerHeight="5dp"
        android:listSelector="@null"
        android:descendantFocusability="afterDescendants" />

</RelativeLayout>

剧集项目

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:foreground="@drawable/all_show_cell_background_selector" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/llEpisode"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#282828"
        android:paddingBottom="5dp" >

        <ImageView
            android:id="@+id/episodeImage"
            android:layout_width="160dp"
            android:layout_height="90dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_vertical|center_horizontal"
            android:adjustViewBounds="true"
            android:contentDescription="@string/app_name"
            android:scaleType="centerCrop"
            android:src="@drawable/video_blank" />

        <TextView
            android:id="@+id/episodeDuration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/episodeImage"
            android:layout_alignLeft="@id/episodeImage"
            android:background="@android:color/black"
            android:gravity="right"
            android:padding="2dp"
            android:textColor="#FFFFFF"
            android:textSize="11sp" />
        <!-- android:textColor="#b5b4b4" -->

        <TextView
            android:layout_marginTop="2dp"
            android:id="@+id/episodeTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/episodeImage"
            android:gravity="right"
            android:paddingRight="5dp"
            android:singleLine="true"
            android:text="hdgsagafasfsdafsdfsdfasddfsad"
            android:textColor="#ffffff"
            android:textSize="14sp" >

        </TextView>

        <TextView
            android:id="@+id/showName"
            android:layout_marginTop="2dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/episodeTitle"
            android:clickable="true"
            android:gravity="right"
            android:paddingRight="5dp"
            android:text="hdgsagafasfsdafsdfsdfasddfsad"
            android:textColor="#A8A8A8"
            android:textSize="13sp" >

        </TextView>

        <TextView
            android:layout_marginTop="2dp"
            android:id="@+id/noOfViewsText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/showName"
            android:layout_alignParentBottom="true"
            android:gravity="right"
            android:paddingRight="5dp"
            android:text="@string/number_of_views_text"
            android:textColor="#A8A8A8"
            android:textSize="11sp" />

        <TextView
            android:id="@+id/episodeNumberOfViews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/noOfViewsText"
            android:layout_toLeftOf="@+id/noOfViewsText"
            android:layout_toRightOf="@+id/fav_img"
            android:ellipsize="none"
            android:gravity="right"
            android:paddingRight="5dp"
            android:singleLine="true"
            android:text="fsdafasdfsad"
            android:textColor="#A8A8A8"
            android:textSize="11sp" />

        <ImageView
            android:id="@+id/fav_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/episodeNumberOfViews"
            android:layout_alignParentLeft="true"
           android:layout_marginLeft="4dp"
            android:contentDescription="@string/favourite_title_show"
            android:src="@drawable/fav_episode" />
    </RelativeLayout>

</FrameLayout>

单个列表项包含 2 个剧集项

这是装有 2 集元素的容器

 <?xml version="1.0" encoding="utf-8"?>
    <!-- This container to help emulate a GridView in each ListView item -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="afterDescendants"
        android:orientation="horizontal" >

    </LinearLayout>

最佳答案

我不太确定前景,但我实际上做的是使用 ListView 。然后在适配器中我为列表中的每一行设置了两个相对布局,这是我的列表属性:

<ListView
android:id="@+id/seccion_list"
android:layout_height="wrap_content"
android:layout_width="fill_parent" 
android:divider="@null"
android:dividerHeight="0dp"/>

在我的适配器中,我将布局设置为为每一行加载,这是该行中第一帧的第一个相对布局:

 <RelativeLayout
        android:id="@+id/primerContenedor"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/seccion_3_imagen_height"
        android:layout_marginLeft="3dip"
        android:layout_marginRight="3dip"
        android:layout_marginTop="5dip"
        android:layout_weight="1"
        android:clickable="true"
        android:focusable="true" 
        android:background="@drawable/layout_selector">

基本上我所做的是将相对布局的可点击和可聚焦属性设置为 true 并定义一个选择器:

    <?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <item android:state_focused="true" android:drawable="@color/blue"/> 
    <item android:state_pressed="true"  android:drawable="@color/blue"/>
    <item android:drawable="@color/grey" /> 
</selector>

关于android - 为 ListView 的特定子项应用 android 选择器,并在同一列表项中的其他子项上禁用选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022230/

有关android - 为 ListView 的特定子项应用 android 选择器,并在同一列表项中的其他子项上禁用选择器的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  6. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  7. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  10. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

随机推荐