【问题标题】:Get tag of view within a ListView item layout获取 ListView 项目布局中的视图标记
【发布时间】:2015-10-23 00:15:38
【问题描述】:

我在 ListView 中使用了以下布局(意味着它对列表中的每个项目使用一次):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <LinearLayout
        android:id="@+id/stats"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="10 comments"
            android:tag="10"
            />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/stats"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:onClick="increaseComments"
            />

    </LinearLayout>

</LinearLayout>

如果用户点击ImageView,我想将TextView 的文本更改为“11 cmets”(递增)。给定下面ImageView的onClick方法,如何获取TextView的标签,然后用它来更改TextView的文本?

public void increaseComments(View view) {
    // TODO
}

【问题讨论】:

  • ListView 本身在哪里?
  • 在提出此类问题之前先进行 Google 搜索。您会在不到 1 分钟的时间内找到答案。 (int) imageView.getTag()
  • @osayilgan 重新阅读问题。我在问如果我在我提供的 onClick 方法中单击ImageView,如何获取TextView 的标签...
  • @AlexK 我没有必要提供 ListView (&lt;ListView&gt; ... &lt;/ListView&gt;) 布局。但是,我的问题中给出了列表 item 布局。
  • @user5477909 看我的回答。

标签: android android-layout android-intent android-activity android-xml


【解决方案1】:

您可以使用getTag()setTag(tagObject) 方法设置和获取视图的标签。请参阅android developer reference 了解更多信息。

您可以将getTag() 方法应用于布局中的任何视图对象。如果您在XML 或代码中设置了某些内容,则可以获取TAG,但它是一个对象,因此需要类转换。

例如,在你的情况下,int textViewTag = (int)total.getTag()

编辑

这很像一个伪代码,可以为您提供想法。这不是处理适配器的getView 方法的理想方式。参考this blog,了解ListViewsViewHolder Pattern 和Adapters

    /* Somewhere in your adapter class, define them Globally */
    ImageView imageView;
    TextView total;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = yourActivityInstance.getLayoutInflater().inflate(R.layout.your_list_item_xml, parent, false);

        imageView = (ImageView) convertView.findViewById(R.id.image);
        total = (TextView) convertView.findViewById(R.id.total);

        return convertView;
    }

    /**
     * Called when the ImageView is Clicked.
     */
    public void increaseComments() {

        /* There you have the Tag of your Text View */
        int textViewTag = (int) total.getTag();
    }

【讨论】:

  • 我不确定你是否理解我的问题。查看ImageViewincreaseComments())的onClick方法。从在那个方法中,我怎样才能得到布局中TextView的标签?
  • @user5477909 当您在适配器类的getView() 方法中扩展项目布局时,您需要保留对TextView 的引用,然后您可以访问它的标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 2020-02-11
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多