0
我有一個recyclerview裏面viewpager裏面的片段。我有viewpager之外的按鈕。點擊按鈕後如何更新recyclerview。Android:如何更新裏面的viewpager裏面的片段裏面的recyclerview點擊按鈕
public class Level2MainActivity_grid1 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level2mainlayout_place1);
// I am putting a viewpager with fragments into them and put tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
Button buttonId = (Button) findViewById(R.id.planmytraveludupitown);
buttonId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//I want to save some things and do the changes in the recyclerview of tab 0;
}
});
}
}
在viewpager,在我使用recyclerview的標籤0 framgment代碼如下:
public class Udupiwhattosee extends Fragment {
private RecyclerView rv;
private List<Udupiwhattoseearray> udupiwhattoseearray;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
RecyclerView v =(RecyclerView) inflater.inflate(R.layout.tab_1,container,false);
v.setLayoutManager(new LinearLayoutManager(getActivity()));
v.setHasFixedSize(true);
initializeData();
WhattoseeRVAdapter adapter = new WhattoseeRVAdapter(udupiwhattoseearray,container.getContext());
v.setAdapter(adapter);
return v;
}
private void initializeData(){
udupiwhattoseearray = new ArrayList<>();
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi Sri Krishna Temple", "Udupi, Karanata,INDIA", R.drawable.udupi));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi place 2", "Udupi, Karanata,INDIA", R.drawable.udup_krishna_math_gopura_udupi));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi Place 3", "Udupi, Karanata,INDIA", R.drawable.udupi_rath));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi place 4", "Udupi, Karanata,INDIA", R.drawable.udupi_window));
}
你可以使用一個標誌,比如'isRecyclerViewNeedUpdated'來表示你的'RecyclerView'是否需要更新。單擊按鈕時,將此標誌設置爲true。使用'ViewPage#addOnPageChangeListener(...)',這樣當你的目標'Fragment'被選中時,檢查標誌並在需要時更新'RecyclerView'。 –