Android GridView获取其中任意子View(并操作)详解
来源:互联网 发布:python初学者的书籍 编辑:程序博客网 时间:2024/06/17 07:40
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数据是不是显示出来了,然后这样是不是以后想哪个的数据都能找到了,然后也不会类型转换异常了。
注意:个人实践解决分享,转载请注明出处,谢谢!
0 0
- Android GridView获取其中任意子View(并操作)详解
- Android GridView获取子View(并操作)
- GridView 子View 获取
- 在gridview中的adapter中引入gridview,并获取其中子控件的值
- robotium gridview获取子view方法(笔记)
- robotium gridview获取子view方法(笔记)
- 弹出带View的对话框并操作其中的控件
- android recylerview 中如何获取子view?
- Android 在不可操作时 获取Recyclerview的viewholder并动态改变view
- asp.net实现 gridview 鼠标单击任意字段选中一行 ,并获取数据
- asp.net 获取GridView中a标签并进行操作
- 由UITableViewCell里的任意子view获取当前所在的UITableViewCell
- ViewPager的使用及获取子view控件的操作(inflate)
- Android获取Context(任意位置任意地方,全局上下文)
- PopupWindow其中的view获取焦点?
- 制做任意大小的bmp,并在其中写汉字
- Android获取View的宽高与View.measure详解
- Android获取View的宽高与View.measure详解
- border
- 【蓝桥杯题】之 算法训练-寂寞的数
- C语言,指针
- jquery的on、off、one、bind、delegate、live、die绑定事件方法研究
- Spring Boot:简单Hello World
- Android GridView获取其中任意子View(并操作)详解
- ModelMap和ModelAndView的作用
- 搭建包含3台机器的Replica Set
- 注册树模式
- DKNightVersion框架的原理和使用
- SQL Server中关于跟踪(Trace)
- C++引用作为函数参数
- 图论(七)哥尼斯堡七桥问题
- jquery mobile初始class清除(取消自动添加的class)