我有一个包含2个片段的Activity,我想通过点击ActionBar工具栏上的按钮来刷新它。
我知道有很多关于stackoverflow的问题的答案,我已经尝试过这个问题并没有完全解决我的问题,或者对我来说实现起来相当复杂。
我尝试使用
ViewGroup parent = (ViewGroup) viewPager.getParent();
if (parent != null) {
parent.removeView(viewPager);
}
删除viewpager,点击刷新按钮时,它没有用。
我试过了notifyDataChanged();
,它也没有用。
这是我的Activity,下面的FragmentPagerAdapter代码。
public class MainActivity extends AppCompatActivity implements android.view.View.OnClickListener{
private Toolbar toolbar;
int firstrun = 0;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager, 0);
viewPager.setOffscreenPageLimit(1);
}
SlidingTabLayout mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
// Setting Custom Color for the Scroll bar indicator of the Tab View
mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
mTabs.setViewPager(viewPager);
/** buttons at toolbar, that would trigger the refresh **/
ImageView A = (ImageView) findViewById(R.id.refresh);
A.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId() == R.id.refresh ){
viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager, 1); // Setting to 1 or 0
viewPager.setOffscreenPageLimit(2);
}
Toast.makeText(MainActivity.this, "Refreshing...", Toast.LENGTH_SHORT).show();
}
}
private void setupViewPager(ViewPager viewPager, int num) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new TabOneFragment(), "TAB 1");
adapter.addFragment(new TabTwoFragment(), "TAB 2");
// I tried this , It didn't work.
/*ViewGroup parent = (ViewGroup) viewPager.getParent();
if (parent != null) {
parent.removeView(viewPager);
}*/
if(num==0) {
viewPager.setAdapter(adapter);
}else{
// I tried this, It didn't work as well
viewPager.getAdapter().notifyDataSetChanged();
}
}
// My Fragment Pager Adapter static class
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
}
}
这是我的片段代码。
public class TabOneFragment extends Fragment {
private int mPage;
private RecyclerView view;
private View mview;
public static TabOneFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
TabOneFragment fragment = new TabOneFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mview = inflater.inflate(R.layout.fragment_all_tabs, container, false);
view = (RecyclerView) mview.findViewById(R.id.homelistrecyclerview);
RestAdapter restAdapter = new RestAdapter.Builder().
setEndpoint(Info.getEndpoint()).build();
MovieApi mApi = restAdapter.create(MovieApi.class);
mApi.getTabOneContent(new Callback<Info>() {
@Override
public void success(Info info, Response response) {
Info example = info;
view.setLayoutManager(new LinearLayoutManager(view.getContext()));
view.setAdapter(new ListingAdapter(getActivity(),
example.getMovies(), tabId, Info.getSchedule(), example.getShowtime()));
}
@Override
public void failure(RetrofitError error) {
//Toast.makeText(getActivity(), error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
//Log.d("RetrofitError: ", error.getLocalizedMessage());
}
});
return view;
}
下面的布局。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/homelistrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
答案 0 :(得分:0)
基本上你应该把它添加到你的适配器:
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}