当Android设备处于横向模式时菜单不起作用

时间:2017-09-29 07:41:54

标签: java android listview asynctaskloader

在横向模式中选择菜单选项时似乎不起作用,它只有一个进度加载器但在纵向模式下我可以选择菜单项选项并完成预期的任务,它加载新的加载器并显示一个项目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();
}

}

在LANSCAPE MODDE中发生的图像 enter image description here

enter image description here

正在进行的加载程序继续,直到您将其旋转回纵向模式后才会结束。

1 个答案:

答案 0 :(得分:1)

Try this,

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