另一件事我的导航抽屉,当我点击它时,它打开我点击它的地方后面的网格视图中的项目,虽然我还没有处理导航抽屉点击但它是奇怪的我认为
片段包含serachview和导航抽屉
package besho.hallofmovies;
import android.app.Fragment;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MovieFragment extends Fragment implements AdapterView.OnItemClickListener {
GridView myGridView ;
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
RecyclerView recyclerView;
RecyclerViewAdapter recyclerViewAdapter;
ArrayList<MyMovies> myMovies = new ArrayList<MyMovies>();
MyDatabaseHandler adapter;
int myState=0;
boolean firstTime=true;
public FetchMovies fetchMovies;
MyCommunicator communicator;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
adapter = new MyDatabaseHandler(getActivity());
if (savedInstanceState != null)
{
myState=savedInstanceState.getInt("myState", 0);
firstTime=savedInstanceState.getBoolean("firstTime", true);
}
else myState=0;
/*Log.d("myFilter",myState+"");
Log.d("myFilter",firstTime +"");*/
communicator = (MyCommunicator) getActivity();
}
@Override
public void onStart() {
super.onStart();
if (firstTime)
{
setStateAs();
firstTime=false;
}
updateMovies(myState);
drawerToggle.syncState();
}
public void setStateAs ()
{
SharedPreferences prefs = getActivity().getSharedPreferences("myData", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean("case", true);
edit.commit();
}
@Override
public void onResume() {
super.onResume();
myGridView.setOnItemClickListener(this);
drawerToggle.syncState();
}
public void updateMovies (int s)
{
if (s == 5)showFav();
else
{
fetchMovies = new FetchMovies();
fetchMovies.buildURL(s);
fetchMovies.execute();
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.movie_fragment, menu);
SearchManager searchManager =
(SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setQueryHint("enter the movie/tv show name");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Log.d("myFilter","submit");
return false;
}
@Override
public boolean onQueryTextChange(String s) {
Log.d("myFilter","ch");
return false;
}
});
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("myState", myState);
outState.putBoolean("firstTime", firstTime);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (drawerToggle.onOptionsItemSelected(item)) {
//Log.d("myFilter","do code");
return true;
}
if (id == R.id.action_mostPopular)
{
myState=1;
updateMovies(myState);
}
if (id == R.id.action_mostRated)
{
myState=2;
updateMovies(myState);
}
if (id == R.id.action_tv)
{
myState=3;
updateMovies(myState);
}
if (id == R.id.action_movie)
{
myState=4;
updateMovies(myState);
}
if (id == R.id.action_favourite)
{
myState=5;
showFav();
}
return super.onOptionsItemSelected(item);
}
public void showFav ()
{
if(adapter.getMovies().isEmpty())
Toast.makeText(getActivity(),"sorry,you don`t have any favourite movies",Toast.LENGTH_SHORT).show();
else{
myMovies = adapter.getMovies();
MoviesAdapter myAdapter = new MoviesAdapter(getActivity());
myGridView.setAdapter(myAdapter);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_movie, container, false);
myGridView = (GridView) view.findViewById(R.id.gridView);
drawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
recyclerViewAdapter = new RecyclerViewAdapter(getActivity(),loadData());
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
drawerToggle = new ActionBarDrawerToggle(getActivity(),drawerLayout,R.string.drawer_open,R.string.drawer_close){
@Override
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView)
{
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//drawerToggle.syncState();
return view ;
}
public static List<Row> loadData ()
{
List<Row> list = new ArrayList<>();
int[] imagesID = {R.drawable.movies,R.drawable.tv,R.drawable.favourite,R.drawable.top_rated,R.drawable.most_popular};
String[] dataText = {"Movies","TV Shows","Favourites","Top Rated","Most Popular"};
for (int i=0;i<imagesID.length && i<dataText.length;i++)
{
Row item = new Row();
item.imageId=imagesID[i];
item.text=dataText[i];
list.add(item);
}
return list;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences p = getActivity().getSharedPreferences("myBool", Context.MODE_PRIVATE);
if (p.getBoolean("singleFrag",true)) {
SharedPreferences prefs = getActivity().getSharedPreferences("myData", Context.MODE_PRIVATE);
Intent i = new Intent(getActivity(), MovieDetailActivity.class);
i.putExtra("title", myMovies.get(position).original_title); //5
i.putExtra("poster", myMovies.get(position).poster_path); //1
i.putExtra("id", myMovies.get(position).item_id); //2
//Log.d("myFilter",""+myMovies.get(position).item_id);
i.putExtra("date", myMovies.get(position).release_date); //4
i.putExtra("rate", myMovies.get(position).vote_average); //6
i.putExtra("overview", myMovies.get(position).overview); //3
i.putExtra("tvORmov", prefs.getBoolean("case", true));
startActivity(i);
}
else
{
communicator.respond(myMovies.get(position));
}
}
class FetchMovies extends AsyncTask<Void,Void,ArrayList<MyMovies>> {
int myCase = 1;
final String baseUrl = "https://api.themoviedb.org/3/";
final String movie = "movie/";
final String tv = "tv/";
final String apiKey = "api_key=f06b4506f14971248b2838078370f394";
final String popular= "popular?";
final String top_Rated = "top_rated?";
String urlAdress = baseUrl + movie +popular+ apiKey;
String myJSON = null;
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
InputStream inputStream = null;
FetchMovies ()
{
}
public void buildURL (int s)
{
SharedPreferences prefs = getActivity().getSharedPreferences("myData", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
if (s == 1)
{
if (prefs.getBoolean("case",true))
{
urlAdress = baseUrl + movie + popular + "&" + apiKey;
myCase=1;
}
else
{
urlAdress = baseUrl + tv + popular + "&" + apiKey;
myCase = 2;
}
//Log.d("myFilter",urlAdress+" s=1");
}
if (s == 2)
{
if (prefs.getBoolean("case",true))
{
urlAdress = baseUrl + movie + top_Rated + "&" + apiKey;
myCase=1;
}
else
{
urlAdress = baseUrl + tv + top_Rated + "&" + apiKey;
myCase = 2;
}
//Log.d("myFilter",urlAdress+" s=1");
}
if (s == 3)
{
urlAdress = baseUrl + tv + popular + "&" + apiKey;
myCase = 2;
edit.putBoolean("case", false);
edit.commit();
//Log.d("myFilter",urlAdress+" s=3");
}
if (s == 4)
{
urlAdress = baseUrl + movie + popular + "&" + apiKey;
myCase=1;
edit.putBoolean("case", true);
edit.commit();
// Log.d("myFilter",urlAdress+" s=4");
}
}
@Override
protected ArrayList<MyMovies> doInBackground(Void... params) {
try {
URL url = new URL(urlAdress);
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
inputStream = urlConnection.getInputStream();
int code = urlConnection.getResponseCode();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer buffer = new StringBuffer();
String line;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
myJSON = buffer.toString();
try {
return parseMyJson(myJSON);
} catch (JSONException e) {
e.printStackTrace();
}
//Log.d("myFilter",myJSON);
return null;
}
public ArrayList<MyMovies> parseMyJson (String json) throws JSONException
{
myMovies.clear();
final String poster_path="poster_path";
final String adult="adult";
final String overview="overview";
final String release_date="release_date";
final String original_title="original_title";
final String air_date="first_air_date";
final String original_name="original_name";
final String vote_count = "vote_count";
final String video="video";
final String vote_average="vote_average";
final String item_id ="id";
JSONObject allMovies = new JSONObject(json);
JSONArray results = allMovies.getJSONArray("results");
for (int i=0;i<results.length();i++)
{
boolean ad=false;
boolean vid=false;
String release="";
String title="";
JSONObject film = results.getJSONObject(i);
String poster = film.getString(poster_path);
String review = film.getString(overview);
int votes = film.getInt(vote_count);
int id = film.getInt(item_id);
double voteAverage = film.getDouble(vote_average);
if (myCase == 1) {
ad = film.getBoolean(adult);
release = film.getString(release_date);
title = film.getString(original_title);
vid = film.getBoolean(video);
}
if (myCase == 2)
{
release = film.getString(air_date);
title = film.getString(original_name);
}
MyMovies temp = new MyMovies(poster,id,ad,review,release,title,votes,vid,voteAverage,false);
//Log.d("myFilter",""+temp.adult);
myMovies.add(temp);
}
return myMovies;
}
@Override
protected void onPostExecute(ArrayList<MyMovies> s) {
super.onPostExecute(s);
MoviesAdapter myAdapter = new MoviesAdapter(getActivity());
myGridView.setAdapter(myAdapter);
//Log.d("myFilter",myMovies.get(0).poster_path);
}
}
class MoviesAdapter extends BaseAdapter
{
ArrayList<MyMovies> movies;
Context context;
MoviesAdapter (Context c)
{
this.context=c;
movies = myMovies;
}
@Override
public int getCount() {
return movies.size();
}
@Override
public Object getItem(int position) {
return movies.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ImageViewHolder
{
ImageView myImage;
ImageViewHolder(View v)
{
myImage = (ImageView) v.findViewById(R.id.imageView);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item=convertView;
ImageViewHolder myHolder;
if (item == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
item = inflater.inflate(R.layout.item_layout,parent,false);
myHolder = new ImageViewHolder(item);
item.setTag(myHolder);
}
else
{
myHolder = (ImageViewHolder) item.getTag();
}
MyMovies temp = movies.get(position);
Picasso.with(context).load("http://image.tmdb.org/t/p/w185" + temp.poster_path).into(myHolder.myImage);
return item;
}
}}
片段布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gridView"
android:layout_gravity="left|top"
android:numColumns="auto_fit"
android:padding="0dp"
android:layout_margin="0dp"
android:columnWidth="180dp"
android:stretchMode="columnWidth"
android:listSelector="@drawable/selector"
/>
</FrameLayout>
<LinearLayout
android:background="#EEE"
android:layout_width="280dp"
android:layout_height="match_parent"
android:id="@+id/navigation_drawer_layout"
android:orientation="vertical"
android:layout_gravity="left">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/movie_night"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_favourite"
android:title="@string/favourite"
app:showAsAction="never"/>
<item
android:id="@+id/action_tv"
android:title="@string/tv"
app:showAsAction="never"/>
<item
android:id="@+id/action_movie"
android:title="@string/movie"
app:showAsAction="never"/>
<item
android:id="@+id/action_mostPopular"
android:title="@string/mostPopular_button"
app:showAsAction="never"/>
<item
android:id="@+id/action_mostRated"
android:title="@string/mostRated_button"
app:showAsAction="never"/>
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView" />
searchview configration.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" >
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="besho.hallofmovies">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity android:name=".MovieDetailActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>