如何在自定义gridView中查找视图以更改它们

时间:2016-10-04 06:03:53

标签: android gridview

您能否帮助我在TextView位置找到onClick,以便我可以执行setText之类的内容,应用动画,更改背景等。

我在MainActivity中的gridView

    final GridView gv = (GridView) findViewById(R.id.gridView1);
    gv.setAdapter(g);
    gv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //TODO: IN HERE I WANT TO CHANGE TEXT, APPLY ANIMATION,AND CHANGE COLOR OF TEXTVIEW PRESENT IN CUSTOM LAYOUT 
            // SO I WANT TO FIND THE TEXTVIEW AS WE NORMALLY DO WITH findViewById();
        }
    });

我的自定义布局我找到了TextView atomicNumber的内容,以便可以应用更改文本并执行其他操作。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dip"
android:layout_height="50dip"
android:orientation="vertical">
<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text="1"
    android:textSize="15dip"
    android:id="@+id/atomicNumber"
    android:gravity="right"
    android:layout_marginTop="5dip"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text="H"
    android:textSize="20dip"
    android:id="@+id/elementSymbol"
    android:gravity="center"/>
</LinearLayout>

我试图找到解决方案但找不到一个

1 个答案:

答案 0 :(得分:1)

您可以在onItemClickListener中获取它。你走了:

gv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //TODO: IN HERE I WANT TO CHANGE TEXT, APPLY ANIMATION,AND CHANGE COLOR OF TEXTVIEW PRESENT IN CUSTOM LAYOUT 
            // SO I WANT TO FIND THE TEXTVIEW AS WE NORMALLY DO WITH findViewById();

       // You can use view object to apply findviewbyId method             
       TextView myTextView = (TextView) view.findViewById(R.id.yourTextview);

        }
    });