<ListView android:layout_width= "Match_parent"Android:id= "@+id/lv"Android:layout_height= "Match_parent" ></ListView> <framelayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Surfaceview Android:id= "@+id/surface_view"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"/> <ImageView Android:id= "@+id/iv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"/></framelayout><TextView Android:id= "@+id/content"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/>
Public classMyadapterextendsBaseadapter {PrivateList<videoitem>list; Privatecontext Context; PrivateLayoutinflater Inflater; PrivateMediaPlayer player; //the Position,-1 currently playing item indicates that no item is playing Private intCurrentplayposition =-1; Private intpreposition =-1; PublicMyadapter (context context, list<videoitem>list) { This. Context =context; This. List =list; Inflater=Layoutinflater.from (context); Player=NewMediaPlayer (); Player.setonpreparedlistener (NewMediaplayer.onpreparedlistener () {//call the Start method to start playback when asynchronous preparation is complete@Override Public voidonprepared (MediaPlayer MP) {Mp.start (); } }); } @Override Public intGetCount () {returnlist.size (); } @Override PublicObject GetItem (intposition) { returnList.get (position); } @Override Public LongGetitemid (intposition) { returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {ListView ListView=(ListView) parent; intFirstvisibleposition =listview.getfirstvisibleposition (); if(Preposition! =firstvisibleposition) { if(preposition = =currentplayposition) {player.stop (); Currentplayposition=-1; } preposition=firstvisibleposition; } log.d ("GOOGLE_LENVE_FB", "Getview:position:" +position); Viewholder Holder; if(Convertview = =NULL) {Convertview= Inflater.inflate (R.layout.listview_item,NULL); Holder=NewViewholder (); Holder.content=(TextView) Convertview.findviewbyid (r.id.content); Holder.cover=(ImageView) Convertview.findviewbyid (R.ID.IV); Holder.surfaceview=(Surfaceview) Convertview.findviewbyid (R.id.surface_view); Convertview.settag (holder); } Else{Holder=(Viewholder) Convertview.gettag (); } Videoitem Videoitem=List.get (position); Holder.content.setText (Videoitem.getcontent ()); //Loading thumbnail imagesPicasso.with (context). Load (Videoitem.getcover ()). Placeholder (r.mipmap.ic_launcher). into (Holder.cover); //re-set the width and height of the picture and SurfaceviewViewgroup.layoutparams LP =Holder.surfaceView.getLayoutParams (); Lp.width=videoitem.getwidth (); Lp.height=videoitem.getheight (); HOLDER.SURFACEVIEW.SETLAYOUTPARAMS (LP); HOLDER.COVER.SETLAYOUTPARAMS (LP); /************* processing Playback ****************/// //get every picture that exists//Object tag = Holder.cover.getTag ();// //when tag is not empty, it indicates that item is recycled instead of the first screen item//if (tag! = null) {//integer tag1 = (integer) tag;// //stop playing when item is moved out of the screen//if (Tag1 = = Currentplayposition && tag1! = position) {//player.stop ();//currentplayposition =-1;// }// } //Save the position of the current item as the tag of the pictureHolder.cover.setTag (position); String p360=videoitem.getp360 (); //determine if the item currently being played is not about to be displayed if(Currentplayposition = =position) {holder.cover.setVisibility (view.invisible); Holder.surfaceView.setVisibility (view.visible); //Reset MediaPlayerPlayer.reset (); //set the Surfaceholder required to display the videoPlayer.setdisplay (Holder.surfaceView.getHolder ()); Try { //set up a data sourcePlayer.setdatasource (Context, Uri.parse (p360)); //Asynchronous PrepPlayer.prepareasync (); } Catch(IOException e) {e.printstacktrace (); } } Else{holder.surfaceView.setVisibility (view.invisible); Holder.cover.setVisibility (view.visible); } holder.cover.setOnClickListener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {v.setvisibility (view.invisible); //get the tag that is carried in the current ImageView, by which the tag can be obtained to play the first few itemInteger tag =(Integer) V.gettag (); //When you click ImageView, if you already have item in play, stop playback first if(Player! =NULL&&player.isplaying ()) {player.stop (); } currentplayposition=tag; //Refresh list, refresh list will call GetView methodnotifydatasetchanged (); } }); returnConvertview; } Public voidStop () {//stop playback and release resources if(Player! =NULL) {player.stop (); Player.release (); } } classViewholder {Surfaceview surfaceview; ImageView cover; TextView content; }}
Public classMainactivityextendsappcompatactivity {PrivateList<videoitem>list; PrivateMyadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView ListView=(ListView) Findviewbyid (r.id.lv); InitData (); Adapter=NewMyadapter ( This, list); Listview.setadapter (adapter); } Private voidInitData () {String jsonstr=getjsonstring (); Parsejson (JSONSTR); } Private voidParsejson (String jsonstr) {list=NewArraylist<>(); Try{jsonobject Jo=NewJsonobject (JSONSTR); Jsonobject Data= Jo.getjsonobject ("Data"); Jsonarray Dataarr= Data.getjsonarray ("Data"); for(inti = 0; I < dataarr.length (); i++) {Jsonobject Item=Dataarr.getjsonobject (i); String P360_video=NULL; intVideo_height = 0; intVideo_width = 0; String cover=NULL; String content=NULL; Try{jsonobject Group= Item.getjsonobject ("group"); P360_video= Group.getjsonobject ("360p_video"). Getjsonarray ("Url_list"). Getjsonobject (0). getString ("url"); Video_height= Group.getint ("Video_height"); Video_width= Group.getint ("Video_width"); Cover= Group.getjsonobject ("Medium_cover"). Getjsonarray ("Url_list"). Getjsonobject (0). getString ("url"); Content= group.getstring ("Content"); Videoitem Videoitem=NewVideoitem (content, cover, Video_height, P360_video, video_width); List.add (Videoitem); } Catch(jsonexception e) {//E.printstacktrace (); //Handling Exception data here ... } } } Catch(jsonexception e) {e.printstacktrace (); } } PrivateString getjsonstring () {InputStream is=NULL; StringBuffer result=NewStringBuffer (); Try{ is= Getassets (). Open ("Video1.json"); intLen = 0; byte[] buf =New byte[1024]; while(len = Is.read (BUF))! =-1) {result.append (NewString (buf, 0, Len)); } returnresult.tostring (); } Catch(IOException e) {e.printstacktrace (); } finally { if(Is! =NULL) { Try{is.close (); } Catch(IOException e) {e.printstacktrace (); } } } return NULL; } @Overrideprotected voidOnStop () {Super. OnStop (); Adapter.stop (); }}
Listvideo Video Playlist