在TabLayout中使用相同适配器的两个片段不起作用

时间:2015-11-21 14:52:12

标签: android android-fragments

只有第一个片段填充了图像,第二个片段是空白的。我正在为两个片段使用相同的适配器。我不确定问题出在哪里。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mSectionsPagerAdapter.addFragment(PopularMoviesFragment.newInstance(SORT_POPULAR), "MOST POPULAR");
    mSectionsPagerAdapter.addFragment(RatingMoviesFragment.newInstance(SORT_RATING), "TOP RATING");

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

}

public void switchToDetail(Intent intent){
    startActivity(intent);
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
static class SectionsPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

    public SectionsPagerAdapter(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);
    }
}

}

第一个片段

public static PopularMoviesFragment newInstance(String sort) {
    PopularMoviesFragment fragment = new PopularMoviesFragment();
    Bundle args = new Bundle();
    args.putString("sort", sort);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sortOrder = getArguments().getString("sort");
    mContext = getActivity();

    if (savedInstanceState != null) {
        mPopularList = savedInstanceState.getParcelableArrayList(POPULAR_LIST);
        for (Movie m : mPopularList) {
            Log.d(LOG_TAG, "onCreate: " + m.getTitle());
        }
    } else {
        FetchPopularMovies pm = new FetchPopularMovies();
        pm.execute(SORT_POPULAR);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View pView = inflater.inflate(R.layout.fragment_popular_movies, container, false);

    mAdapter = new MoviePosterAdapter(mContext, mPopularList);

    mProgressBar = (ProgressBar) pView.findViewById(R.id.movies_progress_bar);
    RecyclerView mRecyclerView = (RecyclerView) pView.findViewById(R.id.my_recycler_view);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mRecyclerView.setAdapter(mAdapter);

    return pView;
}

第二个片段

public static RatingMoviesFragment newInstance(String sort) {
    RatingMoviesFragment fragment = new RatingMoviesFragment();
    Bundle args = new Bundle();
    args.putString("sort", sort);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sortOrder = getArguments().getString("sort");
    mContext = getActivity();

    if (savedInstanceState != null) {
        Log.d(LOG_TAG, "Has saved instance");
        mRatingList = savedInstanceState.getParcelableArrayList(RATING_LIST);
    } else {
        FetchRatingMovies rm = new FetchRatingMovies();
        rm.execute(SORT_RATING);
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_rating_movies, container, false);

    ratingAdapter = new MoviePosterAdapter(mContext, mRatingList);

    mProgressBar = (ProgressBar) view.findViewById(R.id.movies_progress_bar);
    RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(ratingAdapter);

    return view;
}

MoviePosterAdapter

public class MoviePosterAdapter extends RecyclerView.Adapter<MoviePosterAdapter.ViewHolder> {

private final String LOG_TAG = MoviePosterAdapter.class.getSimpleName();

final Context mContext;
private ArrayList<Movie> mDataset;

public static class ViewHolder extends RecyclerView.ViewHolder {

    //public final View mView;
    public final ImageView mImageView;

    public ViewHolder(View view) {
        super(view);
        mImageView = (ImageView) view.findViewById(R.id.imageView);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MoviePosterAdapter(Context context, ArrayList<Movie> myDataset) {
    this.mContext = context;
    mDataset = myDataset;
}

public void setMoviesData(ArrayList<Movie> moviesData){
    mDataset = moviesData;
}

// Create new views (invoked by the layout manager)
@Override
public MoviePosterAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                        int viewType) {
    // create a new view
    View v = LayoutInflater.from(mContext).inflate(R.layout.movie_poster, parent, false);
    // set the view's size, margins, paddings and layout parameters
    return new ViewHolder(v);
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    final Movie mMovie = mDataset.get(position);

    //Creating URL for image
    final String POSTER_BASE_URL = "http://image.tmdb.org/t/p/";
    final String SIZE_PATH = "w185";
    String IMG_PATH = mMovie.getImg();

    //Loading image using Glide
    Glide.with(holder.mImageView.getContext())
            .load(POSTER_BASE_URL + SIZE_PATH + IMG_PATH)
            .placeholder(R.color.grid_placeholder_bg)
            .into(holder.mImageView);

    holder.mImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(mContext, DetailActivity.class);
            i.putExtra("id", mMovie.getId());
            i.putExtra("title", mMovie.getTitle());

            if (mContext instanceof MainActivity) {
                MainActivity main= (MainActivity) mContext;
                main.switchToDetail(i);
            }
        }
    });
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.size();
}

}

请帮助我!

编辑:两个数组都不为空。这是我认为的问题,只有一个片段被填充,可能是因为适配器存在一些问题(不确定)。如果我试图阻止第一个片段被填充(注释掉notifyDatasetChanged()),那么第二个片段工作正常。

2 个答案:

答案 0 :(得分:1)

图像没有填充在第二个片段中,因为它是Glide库的一个问题。切换到毕加索。工作正常。

答案 1 :(得分:0)

我检查了你的代码,我发现一切都很好。我尝试使用虚拟数据,所以我确定错误发生在您设置数据的数据或适配器中。只需使用虚拟数据尝试一次,这样您就会更清楚,或者如果您仍然是空白屏幕,请告诉我。

由于没有数据,通常会出现空白屏幕。所以请检查一次..