android - Using Intent to move from one activity to other takes alongtime and show blank screen -
i showing mp3 files of device in recyclerview.on click of item of recyclerview,i moving other activity shows blank screen few seconds , moves next activity. adapter class of recyclerview follos:
public class songsadapter extends recyclerview.adapter<songsadapter.myviewholder>{ context context; arraylist<hashmap<string, string>> songlist; public songsadapter(arraylist<hashmap<string, string>> songlist,context context) { this.songlist = songlist; this.context=context; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.list_item, parent, false); return new myviewholder(view,context); } @override public void onbindviewholder(myviewholder holder, int position) { holder.textview.settext(songlist.get(position).get("file_name")); } @override public int getitemcount() { return songlist.size(); } public class myviewholder extends recyclerview.viewholder implements view.onclicklistener { private textview textview; context context; public myviewholder(view itemview, context context){ super(itemview); this.context = context; textview=(textview)itemview.findviewbyid(r.id.listitemtextview); itemview.setonclicklistener(this); } @override public void onclick(view v) { int position=getadapterposition(); intent intent=new intent(context,finalactivity.class); intent.putextra("name",songlist.get(position).get("file_name")); intent.putextra("position",position); intent.setflags(intent.flag_activity_new_task); context.startactivity(intent); } } }
xml of final activity
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="pritish.sawant.com.musicplayer.finalactivity"> <imageview android:id="@+id/finalactivityimageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="20dp" /> <imagebutton android:id="@+id/shuffle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/finalactivityimageview" android:layout_marginleft="120dp" android:background="@android:color/white" android:src="@mipmap/repeat" /> <imagebutton android:id="@+id/repeat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/shuffle" android:layout_below="@+id/finalactivityimageview" android:layout_marginleft="50dp" android:layout_torightof="@+id/shuffle" android:background="@android:color/white" android:src="@mipmap/shuffle" /> <linearlayout android:id="@+id/linear" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/repeat" android:layout_margintop="30dp" android:orientation="horizontal"> <textview android:id="@+id/currenttextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/shuffle" android:layout_marginleft="30dp" tools:text="hi" /> <textview android:id="@+id/finaltextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/repeat" android:layout_marginleft="300dp" tools:text="hi" /> </linearlayout> <seekbar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linear" android:layout_marginleft="30dp" android:layout_marginright="30dp" android:layout_margintop="20dp" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/seekbar" android:layout_margintop="30dp" android:orientation="horizontal"> <imagebutton android:id="@+id/previous" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@android:color/white" android:src="@mipmap/previous" /> <imagebutton android:id="@+id/rewind" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@android:color/white" android:src="@mipmap/rewind" /> <imagebutton android:id="@+id/play" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@android:color/white" android:src="@mipmap/play" /> <imagebutton android:id="@+id/forward" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@android:color/white" android:src="@mipmap/forward" /> <imagebutton android:id="@+id/next" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@android:color/white" android:src="@mipmap/next" /> </linearlayout> </relativelayout>
i using asynctask retrieve mp3 files displaying mp3 files on recycler view takes time
its because started intent in myviewholder class. try this:
public class myviewholder extends recyclerview.viewholder { public textview textview;//make public view mview;// make view here public myviewholder(view itemview, context context){ super(itemview); this.context = context; textview=(textview)itemview.findviewbyid(r.id.listitemtextview); mview = itemview;//--note-- make mview = iteem view } //dont set on click listner methods in here
}
now our onbindviewholder set listners here holder parameter:
@override public void onbindviewholder(myviewholder holder, int position) { //--note replace dataitem class/model/ final dataitem itemsong = songlist.get(position); try { //this bind recyclerview textview not sure why there //a string says file_name should itemsong.getfilename(); holder.textview.settext(songlist.get("file_name")); } } catch (exception e) { e.printstacktrace(); } //clicking of recyclycleview holder.mview.setonclicklistener(new view.onclicklistener() {//getting viewholder class , ctor @override public void onclick(view v) { intent intent = new intent(context,finalactivity.class)); //--note create static constant string @ top of class // public static final string item_key ="item_id_key"; intent.putextra(item_key,itemsong); context.startactivity(intent); } });
know recive intent in final activity call:
final dataitemsongs itemsong = getintent().getextras().getparcelable(songsadapter.item_key);
from here can use itemsong object , call properties , set in views ex
nametxt.settext(itemsong.name);
Comments
Post a Comment