10

各項目に2つの要素があるグリッドビューがあります.1つ目は画像で、2つ目はタイトルです.タイトルはアプリケーションの起動時には見えませんが、グリッドビューの外にボタンがあります.クリックして、アイテムのタイトルの表示を変更して表示したいのですが、グリッド ビューで各アイテムの各タイトルにアクセスできないという問題があります。アクティビティの独立したボタンの onClick メソッドでタイトル (TextView) の可視性を設定すると、グリッド ビューの最初のアイテムの可視性のみが変更されます。

このスケッチは私のインターフェイスを表しているため、最初はタイトルが見えませんが、「SetVisibilityButton」をクリックすると、それらを Visible に設定したいと思います:

    Image1    Image2      Image3
    Title1    Title2      Title3

    Image4    Image5      Image6
    Title4    Title5      Title6

    Image7    Image8      Image9
    Title7    Title8      Title9


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

oncreate() アクティビティでグリッド ビューを設定します。

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

私の ImageAdapter クラスでは、これは私の getView() メソッドです:

          @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 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);

ただし、これらのソリューションはすべて、グリッド ビューの最初の要素のみの可視性を設定します。

私はこの問題に多くの時間を費やしましたが、まだ解決策が見つかりません。誰かがそれを修正するのを手伝ってくれますか?

4

2 に答える 2

28

これは簡単です:

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);
    }
  }
}
于 2012-01-20T16:32:17.040 に答える
4

特定の位置の listView のビューを取得する正しい方法は次のとおりです。

View viewItem = list.getChildAt(position);
if (viewItem != null) {                 
   TextView text = (TextView) viewItem.findViewById(R.id.item);
}
于 2014-09-26T10:08:56.280 に答える