我是新來的Android,並跟隨在以下網站提供的教程:的Android RecycleView缺席兩場的TextView出8
http://www.androidauthority.com/how-to-build-an-image-gallery-app-718976/
差不多得到的一切工作,偷懶和使用的圖像他提供了,只是使用了兩個圖像,並複製到8.然而,不知道爲什麼兩個圖像標題(img2和img3)缺失....他們應該做同樣的事情
我的交流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);
}
}
}
與教程示例非常相似...但不知道爲什麼這個缺失的標題正在發生。先謝謝你。