То, чего я хочу достичь, это horizontal
recyclerview
внутри вертикали recyclerview
(как в Google Play Store). Что я думаю, так это то, что параметр позиции зависит от 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);
}
}
}