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

本文详细介绍如何在Android中准确地获取GridView中任意子View的方法,并通过实例代码展示如何避免类型转换异常的问题。

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

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

好了,思想+代码。

之前的文章地址:

地址:http://blog.youkuaiyun.com/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数据是不是显示出来了,然后这样是不是以后想哪个的数据都能找到了,然后也不会类型转换异常了。

注意:个人实践解决分享,转载请注明出处,谢谢!
03-08
### MathPix工具介绍 Mathpix Snipping Tool 是一款强大的数学公式识别工具,从最初的原型发展至今已经历了多次迭代和功能增强[^1]。该工具不仅能处理简单的数学表达式,还可以应对复杂的数学模型以及多行公式,并且支持手写公式的识别。 这款应用程序通过不断的科技创新与用户反馈改进,在学术研究和技术领域赢得了良好声誉,成为许多科研人员不可或缺的工作伙伴之一。除了基本的功能外,Mathpix 还提供了多种高级特性来满足不同用户的特定需求。 ### 使用方法 #### 安装与启动 为了使用 Mathpix Snipping Tool ,首先需要下载安装程序并按照提示完成设置过程。一旦成功安装后即可随时调用此应用来进行截图操作。 #### 截取屏幕上的公式图像 当遇到想要转换成 LaTeX 或 AsciiMath 的图片时,只需打开软件界面按下快捷键(默认为 `Ctrl+Alt+M`),此时鼠标指针会变为十字形状以便于选取目标区域;框选所需部分之后松开按键即刻上传至云端服务器等待进一步分析处理。 #### 获取LaTeX代码片段 经过短暂几秒钟的时间延迟过后,所截获的内容将以纯文本形式显示出来供复制粘贴到其他编辑器当中继续编写文档或是分享给他人查看交流之用。 ```python import pyperclip # 假设已获取到LaTeX字符串 stored_in_variable named latex_code pyperclip.copy(latex_code) print("LaTeX code has been copied to clipboard.") ``` 对于希望深入探索更多可能性的用户来说,可以考虑查阅官方提供的扩展插件——如用于解析Markdown文件内的公式标记语法的库 **mathpix-markdown-it** [^2] ——这将进一步提升工作效率和个人体验感。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值