在标签视图中交换视图寻呼机时增加Recyclerview的项目

时间:2017-10-21 07:51:52

标签: java android android-recyclerview android-viewpager tabview

我创建了一个带有四个标签的tabview,并在此标签视图中使用了一个viewpager。在视图寻呼机中我设置了recyclerview。我给每个recylerview提供了五个项目清单,最初每个Recyclerview显示5个项目,但是交换这些项目变成了两倍。

我的主要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);
    }
}
}

Recycler视图适配器

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);


}

如何阻止这些自动增加的项目?

1 个答案:

答案 0 :(得分:2)

默认情况下,ViewPage可以容纳2个片段而你正在使用4个,所以如果你回到1,如果你将调用第一个片段的onCreate,反之亦然。防止这种使用

$(".clickhere").click(function(e) {
    //how to determine which button is clicked and change the span text in the same row 
});

setOffscreenPageLimit将在堆栈中维护所有4个片段并阻止重新播放。