我制作了一个简单的RecyclerView,但它没有加载图片! 这是我的代码。 RecyclerView有11个原始文件,每个原始文件有2个textview和1个imageview, 这是主要活动的重要部分, MainActivity.java:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] F_Name, D_Name;
int[] Img_res = {R.drawable.a_beautiful_mind,
R.drawable.a_seperation,
R.drawable.casablanca,
R.drawable.daei_jan_napelon,
R.drawable.game_of_thrones,
R.drawable.life_of_pie,
R.drawable.the_dictator,
R.drawable.the_godfather,
R.drawable.the_martian,
R.drawable.the_revenant,
R.drawable.v_for_vendetta};
ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
F_Name = getResources().getStringArray(R.array.Film_Names);
D_Name = getResources().getStringArray(R.array.Director_Names);
int i = 0;
for (String name : F_Name){
DataProvider dataProvider = new DataProvider(Img_res[i], name, D_Name[i]);
arrayList.add(dataProvider);
i++;
}
adapter = new RecyclerAdapter(arrayList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
这是我的适配器,我添加了11个原始文件,每个原始文件包含2个textview和1个ImageView, 适配器:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();
public RecyclerAdapter(ArrayList<DataProvider> arrayList){
this.arrayList = arrayList;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recyclerview_raws, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
DataProvider dataProvider = arrayList.get(position);
holder.imageView.setImageResource(dataProvider.getImg_res());
holder.f_name.setText(dataProvider.getF_name());
holder.d_name.setText(dataProvider.getD_name());
}
@Override
public int getItemCount() {
return arrayList.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView f_name, d_name;
public RecyclerViewHolder(View view){
super(view);
imageView = (ImageView)view.findViewById(R.id.myRecyclerViewImage);
f_name = (TextView)view.findViewById(R.id.myTextVFilmName);
d_name = (TextView)view.findViewById(R.id.myTextVDirectorName);
}
}
}
有人可以帮我吗?