我要实现的是在垂直horizontal
内的recyclerview
recyclerview
(例如Google Play商店)。我认为位置参数取决于getItemCount
方法。
但是我的getItemcount
返回4,而onBindView()
仅循环2次(下面的代码)。
MainActivity
和VericalRecyclerview
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String title[] = this.getResources().getStringArray(R.array.titleArray);
String subTitle[] = this.getResources().getStringArray(R.array.subTitleArray);
String recy1[] = this.getResources().getStringArray(R.array.recy1);
String recy2[] = this.getResources().getStringArray(R.array.recy2);
String recy3[] = this.getResources().getStringArray(R.array.recy3);
String recy1Titles[] = this.getResources().getStringArray(R.array.recy1Title);
String recy2Titles[] = this.getResources().getStringArray(R.array.recy2Title);
String recy3Titles[] = this.getResources().getStringArray(R.array.recy3Title);
Play2G_S play2G_s = new Play2G_S(); //getter setter class
play2G_s.setImages(recy1);
play2G_s.setImageTitles(recy1Titles);
list1.add(play2G_s);
Log.i("sand2", list1+ "");
Play2G_S play2G_s2 = new Play2G_S();//getter setter class
play2G_s2.setImages(recy2);
play2G_s2.setImageTitles(recy2Titles);
list1.add(play2G_s2);
Log.i("sand2", list1+ "");
Play2G_S play2G_s3 = new Play2G_S();//getter setter class
play2G_s3.setImages(recy3);
play2G_s3.setImageTitles(recy3Titles);
list1.add( play2G_s3);
Log.i("sand2", Arrays.toString(list1.get(1).getImageTitles()) + "");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
for (int i = 0; i < title.length; i++) {
PlayStore1G_S g_s = new PlayStore1G_S();
g_s.setSubTitle(subTitle[i]);
g_s.setTitle(title[i]);
g_s.setgSList(list1.get(i));
list.add(g_s);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
RecyclerView recyclerView = findViewById(R.id.recyView);
RecyclerView.LayoutManager manager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(manager);
PlayStore1Adapter adapter = new PlayStore1Adapter(list);
recyclerView.setAdapter(adapter);
}
适配器
class PlayStore1Adapter extends RecyclerView.Adapter<PlayStore1Adapter.MyViewHolder> {
Context context;
List<PlayStore1G_S> list;
public PlayStore1Adapter(List<PlayStore1G_S> list) {
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
this.context= parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row1,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.title.setText(list.get(position).getTitle());
holder.subTitle.setText(list.get(position).getSubTitle());/*
Log.i("sand", Arrays.toString(list.get(position).getList().getImages()));
Log.i("sand", Arrays.toString(list1.get(0).getImageTitles()));*/
RecyclerView.LayoutManager manager = new LinearLayoutManager(this.context,LinearLayoutManager.HORIZONTAL,false);
holder.recyclerView.setLayoutManager(manager);
PlayStore2Adapter adapter = new PlayStore2Adapter(list.get(position).getgSList(),context );
holder.recyclerView.setAdapter(adapter);
Log.i("sand3", Arrays.toString(list.get(position).getgSList().getImageTitles()) +"");
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title,subTitle;
RecyclerView recyclerView;
public MyViewHolder(View v) {
super(v);
title = v.findViewById(R.id.title1);
subTitle = v.findViewById(R.id.subTitle1);
recyclerView = v.findViewById(R.id.newRecyView);
}
}
}
和水平适配器
public class PlayStore2Adapter extends RecyclerView.Adapter<PlayStore2Adapter.MyViewHolder> {
Play2G_S list;
Context context;
public PlayStore2Adapter(Play2G_S list, Context context) {
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row2,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
// holder.textView.setText(list.getImageTitles()[position]+""); /// shows error set null
Log.i("sand34", Arrays.toString(list.getImageTitles()) +"");
}
@Override
public int getItemCount() {
return list.getImageTitles().length; //returns 4
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(View v) {
super(v);
textView = v.findViewById(R.id.textView);
}
}
}
答案 0 :(得分:1)
方法getItemCount()
将返回附加到相应适配器的项目数。
每次视图持有人需要在屏幕上显示时,都会调用方法onBindViewHolder
。
getItemCount
仅被调用2次而onBindViewHolder
返回4的原因是因为尚未创建另外两个视图持有者。
recyclerview将推迟绑定视图持有人,直到它需要出现在屏幕上为止。
如果您进一步滚动,将调用onBindViewHolder
。
希望它有所帮助!