Android RecycleView缺少8个textView

时间:2017-07-20 17:14:54

标签: android textview

我是Android新手,并且正在关注以下网站上的教程:

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

几乎让一切工作,懒得并使用他提供的图像,只使用了两个图像并复制到8.然而,无法弄清楚为什么图像的两个标题(img2和img3)缺失......他们应该做同样的事情

enter image description here

我的activity_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);
    }
}

}

与教程示例完全相同......但不知道为什么会出现这个缺失的标题。先感谢您。

1 个答案:

答案 0 :(得分:0)

我认为这是两件事的组合:在itemView布局中使用adjustViewBoundsRecyclerView的内部行为。

您的itemView布局,剥离,是:

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

通过这种布局,我希望永远不会看到图片标题。这是因为您给了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>