如何用意图来展示电影海报

时间:2015-08-06 12:05:11

标签: android android-intent

我有一个应用程序在应用程序运行时在主页面中显示电影海报。现在,当用户点击电影海报时,我想在DetailMovieActivity类中显示电影海报。

以下代码能够显示海报的URL,但问题是如何让它显示实际的海报而不是URL?我想使用intent和parcelable来实现这一点。为了帮助我,我一直在阅读这篇文章,但它对我没有意义: http://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html

DetailMovieActivity.java

...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {

   View rootView = inflater.inflate(R.layout.fragment_detail_movie, container, false);

   // The detail Activity called via intent.  
  Intent intent = getActivity().getIntent();
   if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
       String send = intent.getStringExtra(Intent.EXTRA_TEXT);
       ((TextView) rootView.findViewById(R.id.detail_title_textview)).setText(send);

   }
   return rootView;
}

MainActivityFragment.java

...

//Displays URL when poster is tap
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
       String forecast = mMovieAdapter.getItem(position);
       Intent intent = new Intent(getActivity(), DetailMovieActivity.class)
               .putExtra(Intent.EXTRA_TEXT, forecast);
       startActivity(intent);
   }
});

fragment_detail_movie.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
       android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
       android:paddingRight="@dimen/activity_horizontal_margin"
       android:paddingTop="@dimen/activity_vertical_margin"
       android:paddingBottom="@dimen/activity_vertical_margin"
       tools:context="com.example.android.sunshine.app.DetailMovieActivity.DetailFragment">

       <ImageView  android:layout_width="wrap_content"
           android:id="@+id/detail_poster_imageview"
           android:layout_height="wrap_content"
          />

       <TextView  android:layout_width="wrap_content"
           android:id="@+id/detail_title_textview"
           android:layout_height="wrap_content"
           android:layout_below="@+id/detail_poster_imageview"/>


</RelativeLayout>

Movie.java

public class Movie implements  Parcelable {


   private String originalTitle;
   private double userRating;
   private String releaseDate;
   private String plotSynopsis;
   private String posterURL;

   public Movie(String originalTitle, double userRating, String releaseDate, String plotSynopsis, String posterPath)
   {
       this.originalTitle = originalTitle;
       this.userRating = userRating;
       this.releaseDate = releaseDate;
       this.plotSynopsis = plotSynopsis;
       this.posterURL =  "http://image.tmdb.org/t/p/w185" + posterPath;
   }



   public String getOriginalTitle() {
       return originalTitle;
   }

   public double getUserRating() {
       return userRating;
   }

   public String getReleaseDate() {
       return releaseDate;
   }

   public String getPlotSynopsis() {
       return plotSynopsis;
   }

   public String getPosterUrl() {
       return posterURL;
   }


   protected Movie(Parcel in) {
       originalTitle = in.readString();
       userRating = in.readDouble();
       releaseDate = in.readString();
       plotSynopsis = in.readString();
       posterURL = in.readString();
   }

   @Override
   public int describeContents() {
       return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
       dest.writeString(originalTitle);
       dest.writeDouble(userRating);
       dest.writeString(releaseDate);
       dest.writeString(plotSynopsis);
       dest.writeString(posterURL);
   }

   @SuppressWarnings("unused")
   public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
       @Override
       public Movie createFromParcel(Parcel in) {
           return new Movie(in);
       }

       @Override
       public Movie[] newArray(int size) {
           return new Movie[size];
       }
   };
}

2 个答案:

答案 0 :(得分:2)

标准ImageView窗口小部件不支持远程源。您需要自己下载或使用像Glide或Picasso这样可以处理远程图像的库

答案 1 :(得分:0)

您需要创建一个类似于此的setOnItemClickListener:

yourView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(getApplicationContext(), New_Activity.class);
                intent.putExtra("imageID", position);
                startActivity(intent);
            }
});

因此,当用户点击电影海报时,它会将它们转换为新活动。