Tablet UI не работает

Я работаю над приложением, которое представляет сетку популярных фильмов для пользователей, и когда пользователь нажимает на один из элементов, он показывает детали фильма.

Приложение работает хорошо на телефонах, но моя реализация пользовательского интерфейса планшета не работает

I don’t get any crashs as such it’s just that when I tap on a movie, the Details don’t show up on tablets.

Вот ссылка на репозиторий GitHub: https://github.com/Hackertronix/Project-Motion/tree/Stage_2?files=1

И вот файлы, которые касаются планшетного пользовательского интерфейса

activity_movies.XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    
    android_layout_width="match_parent"
    android_layout_height="match_parent"
    android_id="@+id/viewpager_container">

</FrameLayout>

activity_movies.xml (layout-sw600dp )

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
             android_layout_width="match_parent"
             android_layout_height="match_parent">

    <LinearLayout
        android_layout_width="match_parent"
        android_layout_height="match_parent"
        android_orientation="horizontal">

        <FrameLayout
            
            android_layout_width="0dp"
            android_layout_height="match_parent"
            android_layout_weight="1"
            android_id="@+id/viewpager_container">
        <FrameLayout
            android_id="@+id/details_container"
            android_layout_width="0dp"
            android_layout_height="match_parent"
            android_layout_weight="1"
            >
        </FrameLayout>
        </FrameLayout>
    </LinearLayout>

</FrameLayout>

Кино.Ява

package com.execube.genesis.views.activities;

import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Explode;
import android.view.View;


import com.execube.genesis.R;
import com.execube.genesis.model.Movie;
import com.execube.genesis.views.fragments.DetailsFragment;
import com.execube.genesis.views.fragments.PopularMoviesFragment;
import com.execube.genesis.views.fragments.TopRatedMoviesFragment;
import com.execube.genesis.views.fragments.ViewPagerFragment;


public class MoviesActivity extends AppCompatActivity implements PopularMoviesFragment.openDetailsListener,
        TopRatedMoviesFragment.openDetailsListener{


    boolean isTablet;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_movies);


        if (findViewById(R.id.details_container) == null)//CHECKING FOR TABLET CONFIGURATION
        {
            isTablet=false;
        }
        else{
            isTablet=true;
        }
        View view= findViewById(R.id.viewpager_container);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setExitTransition(new Explode());
        }
        FragmentManager fragmentManager= getSupportFragmentManager();
        Fragment fragment;
        fragmentManager.findFragmentById(R.id.viewpager_container);

        if(savedInstanceState==null)
        {
            fragment= new ViewPagerFragment();
            fragmentManager.beginTransaction()
                    .add(R.id.viewpager_container,fragment)
                    .commit();

        }


    }

    @Override
    public void openDetails(Movie movie,ActivityOptions options) {

        //options parameter is for the transition

        if(isTablet)
        {
           //TODO Retain the transitions.

          Bundle bundle=new Bundle();
            bundle.putParcelable("PARCEL",movie);

            DetailsFragment fragment= new DetailsFragment();
            fragment.setArguments(bundle);

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.details_container,fragment)
                    .commit();
}

        else{
            Intent intent= new Intent(this,DetailsActivity.class);
            intent.putExtra("PARCEL",movie);
            startActivity(intent,options.toBundle());
        }

    }
}

DetailsActivity.Ява

package com.execube.genesis.views.activities;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.transition.Fade;
import android.transition.Slide;
import android.view.Gravity;
import android.view.View;

import com.execube.genesis.R;
import com.execube.genesis.views.fragments.DetailsFragment;

/**
 * Created by Prateek Phoenix on 5/1/2016.
 */
public class DetailsActivity extends FragmentActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);


        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.details_status_bar));
            Slide slide=new Slide(Gravity.BOTTOM);
            slide.excludeTarget(android.R.id.statusBarBackground,true);
            slide.excludeTarget(android.R.id.navigationBarBackground,true);
            getWindow().setEnterTransition(slide);
            postponeEnterTransition();
        }

        FragmentManager fragmentManager= getSupportFragmentManager();
        Fragment fragment= fragmentManager.findFragmentById(R.id.details_container);

        if(fragment==null)
        {
            Bundle arguments = new Bundle();
            arguments.putParcelable("PARCEL",
                    getIntent().getParcelableExtra("PARCEL"));

            fragment = new DetailsFragment();
            fragment.setArguments(arguments);
            fragmentManager.beginTransaction()
                    .add(R.id.details_container,fragment)
                    .commit();

        }

    }
}

DetailsFragment.Ява

package com.execube.genesis.views.fragments;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.execube.genesis.R;
import com.execube.genesis.model.Movie;
import com.execube.genesis.model.Review;
import com.execube.genesis.utils.API;
import com.execube.genesis.utils.OkHttpHandler;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Response;


/**
 * Created by Prateek Phoenix on 4/30/2016.
 */
public class DetailsFragment extends Fragment {
    private static final String TAG = "DETAILS";
    private static final int DEFAULT_NUM_COLORS = 5;
    private Movie mMovie;
    public Intent intent;

    private TextView mDetailTitle;
    private TextView mReleaseDate;
    private TextView mOverview;
    private TextView mOverviewHeader;


    private ImageView mBackdrop;
    private Toolbar mToolbar;

    private RatingBar mRatingBar;

    private ArrayList<Review> mReviews;


    public DetailsFragment() {

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_detail, container, false);


        mBackdrop = (ImageView) view.findViewById(R.id.details_poster);
        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);

        mDetailTitle = (TextView) view.findViewById(R.id.detail_title_text);
        mReleaseDate = (TextView) view.findViewById(R.id.release_date);
        mOverview = (TextView) view.findViewById(R.id.overview);
        mOverviewHeader = (TextView) view.findViewById(R.id.overview_header);

        mRatingBar = (RatingBar) view.findViewById(R.id.movie_rating);


        Bundle bundle=getArguments();
        mMovie=bundle.getParcelable("PARCEL");


        String id = String.valueOf(mMovie.getId());
        String reviewQueryUrl = API.MOVIES_BASE_URL + id + "/reviews" + API.API_KEY;
        String trailerQueryUrl = API.MOVIES_BASE_URL+id+"/videos"+API.API_KEY;

        mDetailTitle.setText(mMovie.getTitle());
        mReleaseDate.setText(mMovie.getReleaseDate());
        mRatingBar.setProgress((int) mMovie.getVoteAverage());
        mOverview.setText(mMovie.getOverview());


        if (Build.VERSION.SDK_INT != 21) {
            Typeface fontBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Gotham-Rounded-Bold.ttf");
            Typeface fontMedium = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Gotham-Rounded-Medium.ttf");
            Typeface fontMediumLight = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Gotham-Rounded-Book_.ttf");


            mDetailTitle.setTypeface(fontBold);
            mReleaseDate.setTypeface(fontMedium);
            mOverview.setTypeface(fontMediumLight);
            mOverviewHeader.setTypeface(fontBold);
        }


        OkHttpHandler handler = new OkHttpHandler(reviewQueryUrl, mCallback);
        handler.fetchData();

        Picasso.with(getActivity()).load(API.IMAGE_URL + API.IMAGE_SIZE_500 + mMovie.getPosterPath())
                .into(mBackdrop);
        getActivity().startPostponedEnterTransition();


        return view;
    }

    private okhttp3.Callback mCallback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            //TODO handle failure on UI thread
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException,IllegalStateException{

            try {
                String jsonResponse= response.body().string();
                Log.v(TAG,jsonResponse );
                JSONObject jsonObject = new JSONObject(jsonResponse);
                int resultCount = jsonObject.getInt("total_results");
                if (resultCount != 0) {
                    mReviews = parseReviews(jsonObject);
                } else
                    mReviews = null;

            } catch (JSONException e) {

            }
            catch (IllegalStateException e){}
        }
    };

    private ArrayList<Review> parseReviews(JSONObject jsonObject) throws JSONException {


        ArrayList<Review> Reviews = new ArrayList<>();
        JSONArray reviewsJSONArray = jsonObject.getJSONArray("results");

        for (int i = 0; i < reviewsJSONArray.length(); i++) {

            Review review = new Review();
            JSONObject reviewJson = reviewsJSONArray.getJSONObject(i);

            review.setId(reviewJson.getInt("id"));
            review.setAuthor(reviewJson.getString("author"));
            review.setContent(reviewJson.getString("content"));
            review.setTotalResults(reviewJson.getInt("total_results"));

            Reviews.add(review);
        }

        return Reviews;
    }


}

1 ответ

  1. Я думаю, что вы должны написать тот же xml-файл для мобильных устройств и планшетов в макете и
    макет-папка sw600dp. Система будет выбирать по имени папки. Если вы откроете в телефоне он забирает макет из папки макета, и если вы откроете в планшете он забирает
    макет из папки layout-sw600dp.