我有两项活动:MainActivity和DetailActivity。
最初,应用程序已启动到MainActivity。
公共类MainActivity扩展了AppCompatActivity {
private ArrayList<Movie> stringArrayList ;
private RecyclerView mRecyclerView;
private MovieData mAdapter;
private String name = "popular";
private String string ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView)findViewById(R.id.recycler_view);
stringArrayList = new ArrayList<>();
}
@Override
protected void onStart() {
super.onStart();
Log.v("LOG_V","onStart() is called");
string = name ;
updateMovie(string);
}
public void updateMovie(String s)
{
new MovieAsyncTask().execute(s);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_list,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id)
{
case R.id.top_rated :
name = "top_rated";
string = name;
updateMovie(name);
break;
case R.id.popular_movie :
name = "popular";
string = name;
updateMovie(name);
break;
case R.id.about :
Toast.makeText(this,"About",Toast.LENGTH_SHORT).show();
break;
case R.id.upComing_movie :
name = "upcoming";
string = name;
updateMovie(name);
break;
}
return super.onOptionsItemSelected(item);
}
public class MovieAsyncTask extends AsyncTask<String,Void,ArrayList<Movie>>
{
@Override
protected ArrayList<Movie> doInBackground(String... params) {
HttpURLConnection httpURLConnection = null;
InputStream inputStream ;
String jsonString ;
BufferedReader bufferedReader = null;
final String MOVIE_BASE_URL = "http://api.themoviedb.org/3/movie/"+params[0]+"?";
final String APPID_PARAM = "api_key";
try {
Uri uri = Uri.parse(MOVIE_BASE_URL).buildUpon()
.appendQueryParameter(APPID_PARAM,BuildConfig.OPEN_MOVIE_API_KEY).build();
URL url = new URL(uri.toString());
Log.v("URL : ",uri.toString());
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String line ;
while ((line=bufferedReader.readLine())!=null)
{
stringBuffer.append(line+"\n");
}
if(stringBuffer.length()==0)
{
return null;
}
jsonString = stringBuffer.toString();
}catch (IOException I)
{
Log.e("LOG_TAG","Error");
return null;
}finally {
if(httpURLConnection!=null)
{
httpURLConnection.disconnect();
}
if(bufferedReader!=null)
{
try {
bufferedReader.close();
}catch (IOException i)
{
i.printStackTrace();
}
}
}
try {
return getMovieDataFromJson(jsonString);
}catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
public ArrayList<Movie> getMovieDataFromJson(String json) throws JSONException
{
ArrayList<Movie> arrayList = new ArrayList<>();
final String RESULTS = "results";
final String POSTER_BASE_URL = "http://image.tmdb.org/t/p/w185";
final String POSTER_PATH = "poster_path";
final String VOTE_AVERAGE = "vote_average";
final String BACKDROP_URL = "backdrop_path";
final String RELEASE_DATE = "release_date";
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray(RESULTS);
Log.v("jsonArray Size :",jsonArray.length()+"");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String posterUrl = POSTER_BASE_URL+jsonObject1.getString(POSTER_PATH);
String vote = jsonObject1.getString(VOTE_AVERAGE);
String date = jsonObject1.getString(RELEASE_DATE);
String backImageUrl = jsonObject1.getString(BACKDROP_URL);
Log.v("Poster Url" , posterUrl);
arrayList.add(new Movie(vote,date,posterUrl,backImageUrl));
}
return arrayList;
}
@Override
protected void onPostExecute(ArrayList<Movie> arrayList) {
stringArrayList = arrayList ;
if(arrayList!=null)
{
mAdapter = new MovieData(getApplicationContext(),stringArrayList);
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
mRecyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
}
super.onPostExecute(arrayList);
}
}
}
Recycler View Adapter
公共类MovieData扩展了RecyclerView.Adapter {
public ArrayList<Movie> movieArrayList ;
public Context mContext ;
public class viewHolder extends RecyclerView.ViewHolder
{
ImageView imageView ;
TextView ratingView,releaseView ;
public viewHolder(View itemView) {
super(itemView);
imageView = (ImageView)itemView.findViewById(R.id.image_view);
ratingView = (TextView)itemView.findViewById(R.id.rating_view);
releaseView = (TextView)itemView.findViewById(R.id.release_date);
}
}
public MovieData(Context context , ArrayList<Movie> arrayList) {
movieArrayList = arrayList;
mContext = context ;
}
@Override
public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.content_main,parent,false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(viewHolder holder, final int position) {
final Movie movie = movieArrayList.get(position);
final String u = "https://image.tmdb.org/t/p/w500"+movie.getBackdropUrl();
Log.v("LOG_TAG",u);
Picasso.with(mContext).load(movie.getPosterUrl()).into(holder.imageView);
holder.releaseView.setText("Release : "+movie.getmReleaseDate());
holder.ratingView.setText("Rating : "+movie.getmRating()+"/10");
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),DetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_TEXT,u);
v.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return movieArrayList.size();
}
}
Detail Activity
公共类DetailActivity扩展了AppCompatActivity {
String url ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar)findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
url = intent.getStringExtra(Intent.EXTRA_TEXT);
ImageView imageView = (ImageView)findViewById(R.id.backdrop_image);
Picasso.with(this).load(url).into(imageView);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.float_button);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
});
}
}
我如何从详细活动回归到菜单中选择的电影列表。
答案 0 :(得分:0)
如果您想通过按后退按钮返回MainActivity
,则必须实施onBackPressed()
方法。更多详情here。
另外,您可以在this.finish()
课程中的任何位置拨打DetailActivity
,以便退出当前活动并返回父级堆栈中的活动。