0
4つのタブでタブビューを作成し、このタブビューでビューページャーを使用しました。ビューページャーで私はrecyclerviewを設定しました。私は5つのアイテムリストをすべてのリサイクルビューに最初に与えました。リサイクルビューには5つのアイテムが表示されますが、これらを交換すると2倍になります。タブビューでビューページャーを交換するときにRecyclerviewの項目を増やす
MYメインJavaアクティビティ
public class WorldMAp extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.worldmap_activity);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(R.drawable.iconmap);
tabLayout.getTabAt(1).setIcon(R.drawable.iconmap);
tabLayout.getTabAt(2).setIcon(R.drawable.iconmap);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "Countries");
adapter.addFragment(new Tab2Fragment(), "RIVERS");
adapter.addFragment(new Tab3Fragment(), "PEAKS");
adapter.addFragment(new Tab4Fragment(), "WONDERS");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return 4;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
リサイクラービューアダプタ
public class Country_Adapter extends
RecyclerView.Adapter<Country_Adapter.MyViewHolder> {
private List<Country> countryList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, genre;
public ImageView imageView;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
imageView=(ImageView)view.findViewById(R.id.image);
}
}
public Country_Adapter(List<Country> countryList) {
this.countryList = countryList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.country_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Country movie = countryList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.imageView.setBackgroundResource(R.drawable.share);
}
@Override
public int getItemCount() {
return countryList.size();
}
}
タブ表示
private List<Country> countryList = new ArrayList<>();
private RecyclerView recyclerView;
private Country_Adapter mAdapter;
ImageView imageView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1_fragment,container,false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mAdapter = new Country_Adapter(countryList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareMovieData();
return view;
}
private void prepareMovieData() {
Country country = new Country("Mad Max: Fury Road", "Action & Adventure");
countryList.add(country);
Country country1 = new Country("Mad Max: Fury Road", "Action & Adventure");
countryList.add(country1);
Country country2 = new Country("Mad Max: Fury Road", "Action & Adventure");
countryList.add(country2);
Country country3 = new Country("Mad Max: Fury Road", "Action & Adventure");
countryList.add(country3);
Country country4 = new Country("Mad Max: Fury Road", "Action & Adventure");
countryList.add(country4);
}
どのように私はアイテムを増やすこれらの自動防ぐのですか?
おかげで兄弟は完全に働いています:) –