2017-07-20 20 views
1

我是新來的Android,並跟隨在以下網站提供的教程:的Android RecycleView缺席兩場的TextView出8

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

差不多得到的一切工作,偷懶和使用的圖像他提供了,只是使用了兩個圖像,並複製到8.然而,不知道爲什麼兩個圖像標題(img2和img3)缺失....他們應該做同樣的事情

enter image description here

我的交流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

我相信這是兩件事的結合:在您的itemView佈局中使用adjustViewBounds以及RecyclerView的內部行爲。

你ItemView控件佈局,剝離下來的,是:

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

有了這個佈局,我會希望從未看到圖像稱號。這是因爲您正在給所有可用高度的第一個孩子。這爲TextView留下了空間。

但是,既然您也指定了adjustViewBounds="true",您就給了自己一個機會。如果ImageView中的圖像不像父母那麼高,那麼它會縮小,現在您將有空間用於TextView

它看起來像你的兩個圖像有不同的尺寸,所以有時會發生這種情況。有時候不會。

處理此問題的最佳方法是重新編寫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>