2017-07-20 1 views
1

のうちの2のTextViewを逃す:アンドロイドRecycleViewは、私は、Androidに新たなんだ、と次のサイトのチュートリアル次られた8

http://www.androidauthority.com/how-to-build-an-image-gallery-app-718976/

かなり多くの仕事をするすべてのものを持って、怠け者だとイメージを使用します彼は画像の2つを使用し、8にコピーしました。しかし、画像の2つのタイトル(img2とimg3)が欠落している理由を理解できません....同じことをする必要があります。

私のac tivity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.peter.recycleviewtest.MainActivity"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/imagegallery" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</android.support.constraint.ConstraintLayout> 

cell_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="#FFFFFF" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:layout_height="match_parent" 
     android:id="@+id/img" 
     android:adjustViewBounds="true" 
     android:layout_width="match_parent" /> 
    <TextView 
     android:id="@+id/title" 
     android:layout_gravity="center" 
     android:textColor="#000" 
     android:textStyle="bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

main_activity

package com.example.peter.recycleviewtest; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.GridLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 
    private final String image_titles[] = { 
      "Img1A", 
      "Img2B", 
      "Img3C", 
      "Img4D", 
      "Img5E", 
      "Img6F", 
      "Img7G", 
      "Img8H", 
    }; 

    private final Integer image_ids[] = { 
      R.drawable.img1, 
      R.drawable.img2, 
      R.drawable.img3, 
      R.drawable.img4, 
      R.drawable.img5, 
      R.drawable.img6, 
      R.drawable.img7, 
      R.drawable.img8, 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     RecyclerView recyclerView = (RecyclerView)findViewById(R.id.imagegallery); 
     recyclerView.setHasFixedSize(true); 

     RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2); 
     recyclerView.setLayoutManager(layoutManager); 
     ArrayList<CreateList> createLists = prepareData(); 
     MyAdapter adapter = new MyAdapter(getApplicationContext(), createLists); 
     recyclerView.setAdapter(adapter); 
    } 

    private ArrayList<CreateList> prepareData(){ 

     ArrayList<CreateList> theimage = new ArrayList<>(); 
     for(int i = 0; i< image_titles.length; i++){ 
      CreateList createList = new CreateList(); 
      createList.setImage_title(image_titles[i]); 
      Log.d("DEBUG", "Title added: " + image_titles[i]); 
      createList.setImage_ID(image_ids[i]); 
      theimage.add(createList); 
     } 
     return theimage; 
    } 
} 

アダプタ

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
private ArrayList<CreateList> galleryList; 
private Context context; 

public MyAdapter(Context context, ArrayList<CreateList> galleryList) { 
    this.galleryList = galleryList; 
    this.context = context; 
} 

@Override 
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_layout, viewGroup, false); 
    return new ViewHolder(view); 
} 

@Override 
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) { 
    Log.d("DEBUG", "onBindView added: " + galleryList.get(i).getImage_title()); 
    viewHolder.title.setText(galleryList.get(i).getImage_title()); 
    viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    viewHolder.img.setImageResource((galleryList.get(i).getImage_ID())); 
} 

@Override 
public int getItemCount() { 
    return galleryList.size(); 
} 

public class ViewHolder extends RecyclerView.ViewHolder{ 
    private TextView title; 
    private ImageView img; 
    public ViewHolder(View view) { 
     super(view); 

     title = (TextView)view.findViewById(R.id.title); 
     img = (ImageView) view.findViewById(R.id.img); 
    } 
} 

}

チュートリアルのサンプルとほとんど同じですが、なぜこの欠けているタイトルが起こっているのか分かりません。前もって感謝します。

答えて

0

私は、これは2つの要素の組み合わせであると考えています:をitemViewレイアウトに使用し、内部動作をRecyclerViewとします。

あなたitemViewのレイアウトは、ダウン剥奪、次のとおりです。決しては、画像のタイトルを表示するには、このレイアウトで

<LinearLayout orientation=vertical> 
    <ImageView height=match_parent/> 
    <TextView> 
</LinearLayout> 

は、私が期待されます。これは、あなたがLinearLayoutの最初の子どもにの高さのを与えているからです。それはTextViewの余地がありません。

しかし、adjustViewBounds="true"も指定しているので、自分自身にチャンスを与えました。 ImageViewの画像が親画像と同じくらいの高さでない場合は、画像が縮小され、TextViewの領域が確保されます。

2つの画像の寸法が異なるように見えることがあります。時にはそれはしません。

これを処理する最善の方法は、itemViewのレイアウトを書き換えることです。 LinearLayoutlayout_weightのいずれかの属性を使用するか、またはFrameLayoutを使用して画像の上に画像のタイトルを重ねることをおすすめします。

のLinearLayout溶液

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textColor="#000" 
     android:textStyle="bold"/> 

</LinearLayout> 

でframeLayout溶液

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF"> 

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|center_horizontal" 
     android:textColor="#000" 
     android:textStyle="bold"/> 

</FrameLayout> 
関連する問題