Android GridView获取其中任意子View(并操作)详解

29 篇文章 0 订阅
10 篇文章 0 订阅

Android GridView获取其中任意子View(并操作)详解

之前也写过这个的文章,但还是不够详细,获取任意的view时,不够准确,有时会类型转换异常,今天再来详解,以找一个准。

好了,思想+代码。

之前的文章地址:

地址:http://blog.csdn.net/aierjun/article/details/54347669

点击直接跳转地址

1.找到GridView

myGridView= (MyGridView) findViewById(R.id.gridview);

2.监控item

 myGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });

3.和之前将的一样找到父布局

RelativeLayout relativeLayout=(RelativeLayout) myGridViewTopicClassify.getAdapter().getView(i,view,null);

注意:这里的原因上一篇已经讲了,不多做解释

4.获取子布局

View view=(View) relativeLayout.getChildAt(0);

注意:这里我之所以我不用TextView是因为它的子View第一个不一定是TextView,这里是和上次最大的区别。

5.思路

item布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/type_classify_image"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@mipmap/three"
        android:scaleType="centerCrop"/>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_toRightOf="@id/type_classify_image"
        android:paddingLeft="15dp"
        android:gravity="center">
        <TextView
            android:id="@+id/type_classify_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="爱情"
            android:textSize="15sp"
            android:textColor="@color/black"
            android:paddingBottom="10dp"
            android:maxLines="2"
            android:ellipsize="end"/>
        <TextView
            android:id="@+id/type_classify_num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7852"
            android:textSize="15sp"
            android:textColor="@color/black_gray"
            android:layout_below="@id/type_classify_name"
            android:maxLength="6"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="个"
            android:textSize="15sp"
            android:textColor="@color/black_gray"
            android:layout_below="@id/type_classify_name"
            android:layout_toRightOf="@id/type_classify_num"/>
    </RelativeLayout>
</RelativeLayout>
当我按上次一样转TextView时,异常,提示类型转换异常。
然后用View,发现是ImageView,然后将getChildAt(0);中的0改成1;然后用View接收,发现是RelativeLayout,然后你是不是发现规律了,但当你写成getChildAt(2);时,空指针异常。

现在再想想,getChildAt(1);时是RelativeLayout,所以RelativeLayout relativeLayout1=(RelativeLayout) relativeLayout.getChildAt(1);转一次,然后再View view1=relativeLayout1.getChildAt(0);发现view1是TextView,现在应该理解了。子view的子view

完整代码:

myGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                RelativeLayout relativeLayout=(RelativeLayout) myGridViewTopicClassify.getAdapter().getView(i,view,null);
                View textView=(View) relativeLayout.getChildAt(0);
                RelativeLayout relativeLayout1=(RelativeLayout) relativeLayout.getChildAt(1);
                TextView view1=(TextView)relativeLayout1.getChildAt(0);
                Toast.makeText(AllStoryActivity.this,view1.getText().toString(),Toast.LENGTH_SHORT).show();
            }
        });

现在你点击哪个item数据是不是显示出来了,然后这样是不是以后想哪个的数据都能找到了,然后也不会类型转换异常了。

注意:个人实践解决分享,转载请注明出处,谢谢!
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值