2017-01-24 16 views
0

画像ビュー内にテキストを設定したいと思います。現時点では、ビューアホルダを使用する方が良いと思うにもかかわらず、私はdrawableからイメージを取得しますか?ここに私のアダプタです:私はこの質問How can I add a TextView into an ImageView in GridView Layout? が、明確な説明がないのを見てきたAndroid - gridviewで画像ビュー内のテキストを設定する

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/single" 
    > 

    <ImageView 
     android:id="@+id/myImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_round" /> 

    <TextView 
     android:id="@+id/myImageViewText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/myImageView" 
     android:layout_alignTop="@+id/myImageView" 
     android:layout_alignRight="@+id/myImageView" 
     android:layout_alignBottom="@+id/myImageView" 
     android:layout_margin="1dp" 
     android:gravity="center" 
     android:text="" 
     android:textColor="#000000" /> 

</RelativeLayout> 

item_single.xml:

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 


    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { 
      // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 

    // references to our images 
    private Integer[] mThumbIds = { 
      R.drawable.ic_round, R.drawable.ic_round, 
      R.drawable.ic_round, R.drawable.ic_round, 
      R.drawable.ic_round, R.drawable.ic_round, 
      R.drawable.ic_round, R.drawable.ic_round, 

    }; 
} 

ここで私が使用したい私のレイアウトです。その例では、ViewHolderを使用しています。私はRecycleViewHolderを使いたいと思っています。

答えて

0

あなたは、私がviewholdersを使用することをお勧めいたしますだろうが、あなたの要件ごとに、あなたがこのようにそれを行う必要があります

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView; 
    TextView textView; 
    View view; 
    if (convertView == null) { 
     // if it's not recycled, initialize some attributes 
     view = LayoutInflater.from(mContext).inflate(R.layout. item_single,false); 
     imageView = view.findViewById(R.id.myImageView); 
     textView = view.findViewById(R.id.myImageViewText); 
     imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(8, 8, 8, 8); 
    } else { 
     view = convertView; 
     imageView = convertView.findViewById(R.id.myImageView); 
     textView = convertView.findViewById(R.id.myImageViewText); 
    } 

    imageView.setImageResource(mThumbIds[position]); 
    textView.setText("Any Text"); 
    return view; 
} 

を膨らませる必要があります。

+0

'view = LayoutInflater.from(mContext).inflate(R.layout.item_single、false);'メソッドを解決できませんinflate(int、boolean) –

+0

各画像ビューでテキストビューを1ずつインクリメントしたいのですが? –

+0

nvm、私はそれを働かせた –

0

ImageViewTextViewの両方を持つカスタムビューを膨張させます。 AdapterクラスのあなたのgetView方法、複数の検索カスタムAdapter例えば、いくつかの

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView; 
    if (convertView == null) { 
    // This view will contain TextView and ImageView 
    convertView = inflater.inflate(R.layout.item_row, parent, false); 

インサイド。

関連する問題