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
原创粉丝点击