2017-01-24 1 views
0

Je voudrais définir le texte à l'intérieur de l'image. Au moment où je reçois l'image de drawable, même si je pense qu'il est préférable d'utiliser un viewholder? voici mon adaptateur:Android - Définir le texte dans l'imageview avec gridview

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, 

    }; 
} 

voici ma mise en page que je veux utiliser: item_single.xml

<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> 

J'ai vu cette question How can I add a TextView into an ImageView in GridView Layout? mais il n'y a pas d'explication claire. Dans cet exemple, il utilise un ViewHolder, le mien continue de vouloir que j'utilise RecycleViewHolder.

Répondre

0

Vous devez gonfler

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; 
} 

Je vous recommande d'utiliser viewholders mais selon vos besoins vous devriez le faire de cette façon.

+0

'view = LayoutInflater.from (mContext) .inflate (R.layout.item_single, false);' impossible de résoudre la méthode inflate (int, boolean) –

+0

Ok, je l'ai mis au travail. Que faire si je veux incrémenter la textview de 1 sur chaque imageview? –

+0

nvm, je l'ai eu le travail –

0

Inflatez votre vue personnalisée dans laquelle vous avez ImageView et TextView les deux. A l'intérieur de votre méthode de getViewAdapter classe, certains

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

Pour plus de recherche personnalisé Adapter exemple.