如何访问到每一个网格视图项目的特定元素 [英] how to access to a specific element of every grid view item

查看:148
本文介绍了如何访问到每一个网格视图项目的特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,其中有在它的每一个项目两个因素,第一个是图像,第二个是标题,标题是不可见的应用程序的启动,但我有电网之外的按钮查看,当我点击它,我想改变项目的冠军能见度变得可见,我的问题是,我不能访问每个项目的每个标题在网格视图。当我设置的在我的活动独立按钮的onclick方法的标题(的TextView)的知名度,它改变了能见度仅适用于网格视图中的第一项!

I have a gridview in which there are two elements in each of its items, the first is an image and the second is a title, the title is invisible in the launching of the application, but I have a button outside the grid view that when I click on it, I want to change the visibility of the titles of items to become visible, my problem is that I can not access each title of each item in the grid view. When I set the visibility of the titles (TextView) in the onClick method of the independent button in my activity, it changes the visibility ONLY for the FIRST item in the grid view!

本草图再present我的接口,所以标题是不可见的开始,但是当我点击SetVisibilityButton,我想将它们设置为可见:

This sketch represent my interface, so the titles are invisible in the beginning, but when i click on "SetVisibilityButton", i want to set them to Visible :

    Image1    Image2      Image3
    Title1    Title2      Title3

    Image4    Image5      Image6
    Title4    Title5      Title6

    Image7    Image8      Image9
    Title7    Title8      Title9


    ----------------------------
    SetVisibilityButton
    ____________________________        

我设置网格视图在我的OnCreate()活动:

I set the grid view in my oncreate() activity :

    favorGrid = (GridView) findViewById(R.id.favorGrid);
    favorGrid.setAdapter(adapter);

在我ImageAdapter类,这是我的getView()方法:

In my ImageAdapter class, this is my getView() method :

          @Override
       public View getView(int position, View convertView, ViewGroup parent) 
       {
          View MyView = convertView;

           /*we define the view that will display on the grid*/

             //Inflate the layout
             LayoutInflater li = getLayoutInflater();
             MyView = li.inflate(R.layout.favor_item, null);

             // Add The Text!!!
             TextView tv = (TextView)MyView.findViewById(R.id.title);
             tv.setText(mTitles[position]);

             // Add The Image!!!           
             ImageView iv = (ImageView)MyView.findViewById(R.id.favor_item_image);
             iv.setImageResource(mThumbIds[position]);
          return MyView;
       }

从我的主要活动拿到冠军的TextView并设置自己的知名度,我想:

To get the title textview from my main activity and set his visibility, I tried :

    TextView title = (TextView) findViewById(R.id.title);
    title.setVisibility(View.VISIBLE);

和尝试:

     // gv is the gridview (R.id.gridview)
     TextView title = (TextView)gv.findViewById(R.id.title);
     title.setVisibility(View.VISIBLE);

和尝试:

      LinearLayout layout = (LinearLayout) findViewById(R.id.gridLayout);
      TextView title = (TextView)layout.findViewById(R.id.title);
      title.setVisibility(View.VISIBLE);

不过,所有这些解决方案设置能见度仅在网格视图中的第一个元素。

But all these solutions set the visibility for only the first element in the grid view.

我花了很多时间与这个问题,但我还没有找到一个解决办法,没有人可以帮我解决这个问题,请 谢谢你在前进

I spent a lot of time with this problem but I don't yet found a solution, does anyone could help me to fix it please thank you in advance

推荐答案

这是简单的:

GridView mGridView (Your object instance)

final int size = mGridView.getChildCount();
for(int i = 0; i < size; i++) {
  ViewGroup gridChild = (ViewGroup) mGridView.getChildAt(i);
  int childSize = gridChild.getChildCount();
  for(int k = 0; k < childSize; k++) {
    if( gridChild.getChildAt(k) instanceof TextView ) {
      gridChild.getChildAt(k).setVisibility(View.GONE);
    }
  }
}

这篇关于如何访问到每一个网格视图项目的特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆