NestedScrollview在纵向和横向模式下表现不同

时间:2016-03-12 15:27:36

标签: android android-fragments

我正在创建一个应用,其详细活动包括两个数组适配器。活动显示预告片列表和评论列表。列表的长度取决于api调用。

为了能够向上和向下滚动,我使用嵌套的ScrollView。问题是它表现得有些奇怪。

在第一张图片中,我看到几个预告片使片段比视口大,但它不允许我滚动由于某种原因: http://postimg.org/image/c8fvd52i7/

在相同的电影细节中,但在横向模式下,由于某种原因,它仅显示1个预告片和评论。 http://postimg.org/image/o91bdv9wv/

另一部电影,仍然表现得很奇怪。在纵向模式下,我只看到预告片而没有评论,即使有足够的空间来显示它。 http://postimg.org/image/ni8l837jj/

同样的电影,风景。它显示一个评论 http://postimg.org/image/4bve4wr1r/

这是我的详细活动片段:

    group1  group2  group3
0        3       3       5  
1        4       2       1
2        1       1       5
3        5       1       2

这是我的XML activity_detail文件:

/**
 * Fragment where the information about a selected movie will be displayed
 */
public class DetailActivityFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int MY_LOADER_ID = 2;
    private static final int NON_FAVORITE = 0;


    List<Trailer> mTrailerList;
    List<Review> mReviewList;
    TrailerAdapter mTrailerAdapter;
    ReviewAdapter mReviewAdapter;
    Movie mMovie;
    private final int FAVORITE = 2;



    @Bind(R.id.movie_name) TextView mMovieTitle;
    @Bind(R.id.release_date) TextView mReleaseDate;
    @Bind(R.id.plot_synopsis) TextView mSynopsis;
    @Bind(R.id.vote_average) TextView mRating;
    @Bind(R.id.movie_thumbnail) ImageView mMovieThumbnail;
    private String mMovieId;
    private boolean FAVORITE_MOVIE = false;
    private ShareActionProvider mShareActionProvider;

    public DetailActivityFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        mMovieId = getActivity().getIntent().getStringExtra(MovieListFragment.EXTRA_MOVIE);

        Cursor trailerCursor = getActivity().getContentResolver().query(
                TrailersEntry.CONTENT_URI,
                null,
                TrailersEntry.COLUMN_MOVIE_ID + " = ?",
                new String[] {mMovieId},
                null
        );

        Cursor reviewCursor = getActivity().getContentResolver().query(
                ReviewsEntry.CONTENT_URI,
                null,
                ReviewsEntry.COLUMN_MOVIE_ID + " = ?",
                new String[]{mMovieId},
                null
        );

        mTrailerList = Utility.getTrailersFromCursor(trailerCursor);
        mReviewList = Utility.getReviewsFromCursor(reviewCursor);

        getLoaderManager().initLoader(MY_LOADER_ID, null, this);

        //If the movie is a favorite, we will change the appearance of the favorite button and its callback
        Cursor checkFavouriteCursor = getActivity().getContentResolver().query(
                MoviesEntry.CONTENT_URI,
                null,
                MoviesEntry.COLUMN_MOVIE_ID + " = ? AND " + MoviesEntry.COLUMN_MOVIE_TYPE + " = ?",
                new String[] {mMovieId,String.valueOf(FAVORITE)},
                null
        );
        if (checkFavouriteCursor != null && checkFavouriteCursor.getCount() >0) {
            FAVORITE_MOVIE = true;
            //Free cursor for resources
            checkFavouriteCursor.close();
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.detail_fragment, menu);
        // Retrieve the share menu item
        MenuItem menuItem = menu.findItem(R.id.action_share);

        // Get the provider and hold onto it to set/change the share intent.
        mShareActionProvider =
                (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

        // Attach an intent to this ShareActionProvider.  You can update this at any time,
        // like when the user selects a new piece of data they might like to share.
        if (mShareActionProvider != null ) {
            mShareActionProvider.setShareIntent(createShareTrailerIntent());
        }
    }

    private Intent createShareTrailerIntent() {
        if (mTrailerAdapter.getCount() == 0) return null;
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, mTrailerAdapter.getItem(0).getTrailerPath());
        return shareIntent;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_detail, container, false);

        //We target the appropriate views
        ButterKnife.bind(this, rootView);

        //Initialize the trailer adapter
        mTrailerAdapter = new TrailerAdapter(
                getContext(),
                R.id.trailer,
                (ArrayList<Trailer>) mTrailerList,
                inflater
        );
        //Initialize the trailer adapter
        mReviewAdapter = new ReviewAdapter(
                getContext(),
                R.id.review_holder,
                (ArrayList<Review>) mReviewList,
                inflater
        );

        ListView trailerListView = (ListView) rootView.findViewById(R.id.trailer_listview);
        ListView reviewListView = (ListView) rootView.findViewById(R.id.review_listview);
        trailerListView.setAdapter(mTrailerAdapter);
        reviewListView.setAdapter(mReviewAdapter);

        //We assign a callback method when the favourites_button is clicked
        final Button favouritesButton = (Button) rootView.findViewById(R.id.favourites_button);
        updateButtonStyling(favouritesButton);

        favouritesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateMovieAsFavourite(mMovie);
                updateButtonStyling(favouritesButton);
            }
        });

        //We assign a callback method to the trailer elements
        trailerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Trailer trailer = (Trailer) parent.getItemAtPosition(position);
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(trailer.getTrailerPath())));
            }
        });

        //If the movie we have selected


        return rootView;
    }

    //Method to save a movie into the database
    private void updateMovieAsFavourite(Movie movie) {

        ContentValues values = new ContentValues();
        values.put(MoviesEntry.COLUMN_MOVIE_ID, movie.getMovieId());
        values.put(MoviesEntry.COLUMN_POSTER_URL,movie.getUrlPath());
        values.put(MoviesEntry.COLUMN_TITLE, movie.getOriginalTitle());
        values.put(MoviesEntry.COLUMN_PLOT,movie.getPlotSynopsis());
        values.put(MoviesEntry.COLUMN_RATING_AVERAGE, movie.getUserRating());
        values.put(MoviesEntry.COLUMN_RATING_COUNT, movie.getVoteCount());
        values.put(MoviesEntry.COLUMN_RELEASE_DATE, movie.getReleaseDate());
        //Actually updated field, marking movie as a favorite
        if (FAVORITE_MOVIE) {
            values.put(MoviesEntry.COLUMN_MOVIE_TYPE, NON_FAVORITE);
        } else {
            values.put(MoviesEntry.COLUMN_MOVIE_TYPE, FAVORITE);
        }
        //Return value from the insertion
        int numrows = 0;
        //Actual insertion, if the movie is not saved, we do it now
        try {
            getActivity().getContentResolver().update(
                    MoviesEntry.CONTENT_URI,
                    values,
                    MoviesEntry.COLUMN_MOVIE_ID + " = ?",
                    new String[]{String.valueOf(movie.getMovieId())}
            );
            if (FAVORITE_MOVIE) {
                Toast.makeText(getActivity(),
                        String.format(
                                "%s removed from your favorites list",
                                movie.getOriginalTitle()),
                        Toast.LENGTH_LONG
                ).show();
            } else {
                Toast.makeText(getActivity(),
                        String.format(
                                "%s added to your favorites list",
                                movie.getOriginalTitle()),
                        Toast.LENGTH_LONG
                ).show();
            }


        } catch (SQLException e) {e.printStackTrace();}
        //Clicking the button changes wether the movie is a favourite or not
        FAVORITE_MOVIE = !FAVORITE_MOVIE;
    }

    private void updateButtonStyling(Button button) {
        if (FAVORITE_MOVIE) {
            button.setText(R.string.remove_favorite);
            button.setBackgroundColor(getResources().getColor(R.color.danger_button));
        } else {
            button.setText(R.string.favorite);
            button.setBackgroundColor(getResources().getColor(R.color.success_button));
        }
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(getContext(),
                MoviesEntry.CONTENT_URI,
                null,
                MoviesEntry.COLUMN_MOVIE_ID + " = ?",
                new String[] {mMovieId},
                null
        );
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

        mMovie = Utility.makeMovieFromCursor(data);

        if (mMovie != null) {
            mMovieTitle.setText(Html.fromHtml("<b>Original Title: </b>" + mMovie.getOriginalTitle()));
            mReleaseDate.setText(Html.fromHtml("<b>Release date: </b>" + mMovie.getReleaseDate()));
            mSynopsis.setText(Html.fromHtml("<b>Synopsis: </b><br></br>" + mMovie.getPlotSynopsis()));
            mRating.setText(Html.fromHtml("<b>Rating: </b>"
                    + String.format("%.2f / 10 - Based on %d reviews",
                    mMovie.getUserRating(),
                    mMovie.getVoteCount()
            )));
            Picasso.with(getContext())
                    .load(mMovie.getUrlPath())
                    .resize(300,450)
                    .into(mMovieThumbnail);
        }

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}

最后,这是我的fragment_detail文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity" tools:ignore="MergeRootFrame">

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.madelenko.movierating.DetailActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />

            </android.support.design.widget.AppBarLayout>

            <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true">

                <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/detail_container"/>
            </android.support.v4.widget.NestedScrollView>

        </LinearLayout>




    </android.support.design.widget.CoordinatorLayout>

</FrameLayout>

您能帮我确定一下这种行为的根源吗?非常感谢你提前。

0 个答案:

没有答案