简体   繁体   中英

When android device is in landscape mode menu doesn't work

When selecting a menu option item in landscape mode it seems not to work, it just has a progress loader but when in portrait mode I can select the menu item option and does expected task, it loads new loader and shows items in a gridview.

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<MovieImageData>>{

private final String LOG_TAG = MainActivity.class.getSimpleName();
//API KEY
private static final String API_KEY = "";

private static final String BASE_SORT_URL = "http://api.themoviedb.org/3/movie";
//Popular movie query
private static final String POPULAR_MOVIES_TAG = "/popular?api_key=";
//Top rated movie query
private static final String TOP_RATED_MOVIES_TAG = "/top_rated?api_key=";

private static final String POPULAR_MOVIES_URL = BASE_SORT_URL + POPULAR_MOVIES_TAG + API_KEY;
private static final String TOP_RATED_MOVIES_URL = BASE_SORT_URL + TOP_RATED_MOVIES_TAG + API_KEY;

private static String currentURL = POPULAR_MOVIES_URL;
static boolean hasLoaderOne = false;
MoviePosterAdapter adapter;
GridView moviePosterGridView;

View progress;

private static ArrayList<MovieImageData> movieData;

//Movie base
private static final String MOVIEDB_BASE_URL = "http://api.themoviedb.org/3/movie";
//Image base URL
private static final String MOVIEDB_IMAGE_BASE = "http://image.tmdb.org/t/p/w185/";

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

    progress = findViewById(R.id.loading_spinner);

    if(savedInstanceState == null || !savedInstanceState.containsKey("movie")) {
        Log.i(LOG_TAG, "Ent======================================================================");
        progress.setVisibility(View.GONE);
        getLoaderManager().initLoader(0, null, this).forceLoad();
    }else {

        movieData = savedInstanceState.getParcelableArrayList("movie");
        updateUi(movieData);
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.settings, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.popular:
            currentURL = POPULAR_MOVIES_URL;
            getLoaderManager().getLoader(0).forceLoad();
            progress.setVisibility(View.VISIBLE);
            hasLoaderOne = false;
            return true;


    case R.id.top_rated:

            currentURL = TOP_RATED_MOVIES_URL;
            getLoaderManager().initLoader(1, null, this).forceLoad();
            hasLoaderOne = true;
            progress.setVisibility(View.VISIBLE);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putParcelableArrayList("movie", movieData);
    super.onSaveInstanceState(outState);
}

@Override
public Loader<List<MovieImageData>> onCreateLoader(int id, Bundle args) {
    Log.i(LOG_TAG, "LOADER============================");
    progress.setVisibility(View.VISIBLE);
    return new MovieImageLoader(MainActivity.this, currentURL);
}

@Override
public void onLoadFinished(Loader<List<MovieImageData>> loader, List<MovieImageData> data) {
    progress.setVisibility(View.GONE);
    movieData = new ArrayList<MovieImageData>();
    movieData.addAll(data);
    updateUi(movieData);
}

@Override
public void onLoaderReset(Loader<List<MovieImageData>> loader) {
    progress.setVisibility(View.GONE);
    moviePosterGridView.setAdapter(null);
}

public void updateUi(final List<MovieImageData> moviePosterData){

    // Create a new {@link ArrayAdapter} of earthquakes
    adapter = new MoviePosterAdapter(this, moviePosterData);

    TextView noItemFound = (TextView) findViewById(R.id.no_list);
    // Find a reference to the {@link ListView} in the layout
    moviePosterGridView = (GridView) findViewById(R.id.gridview);
    moviePosterGridView.setEmptyView(noItemFound);

    if(!isOnline()){
        noItemFound.setVisibility(View.VISIBLE);
    }else{
        //noItemFound.setText("No earthquakes found!");
    }


    // Set the adapter on the {@link ListView}
    // so the list can be populated in the user interface
    moviePosterGridView.setAdapter(adapter);
}

public boolean isOnline() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

}

IMAGES OF WHAT HAPPENS IN LANSCAPE MODDE 在此处输入图片说明

在此处输入图片说明

The progressing loader continues and doesn't end until you rotate it back to portrait mode.

Try this,

<activity
   android:name=".MainActivity"
   android:configChanges="orientation|screenSize"
  />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM