我在recyclerview中显示我的设备的所有mp3文件。点击recyclerview的项目,我正在转移到其他活动,但它显示空白屏幕几秒钟然后转移到下一个活动。 我的recyclerview的适配器类如下:
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
<?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>
我使用AsyncTask检索所有mp3文件,但即使在我的回收站视图上显示mp3文件也需要时间
答案 0 :(得分:0)
因为你在MyViewHolder类中启动了意图。试试这个:
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;//make public
View mView;// make a 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
}
现在我们的onBindViewHolder你可以在holder参数中设置列表器:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//--note replace DataItem with your Class/Model/
final DataItem itemSong = songList.get(position);
try {
//this will bind to your recyclerView textView not sure why there is
//a string that says file_name should be itemSong.getFileName();
holder.textView.setText(songList.get("file_name"));
}
} catch (Exception e) {
e.printStackTrace();
}
//clicking of the recyclycleView
holder.mView.setOnClickListener(new View.OnClickListener() {//getting viewholder class and ctor
@Override
public void onClick(View v) {
Intent intent = new Intent(context,FinalActivity.class));
//--note create a static constant string at top of class
// public static final String ITEM_KEY ="item_id_key";
intent.putExtra(ITEM_KEY,itemSong);
context.startActivity(intent);
}
});
知道在最后一次活动中回复意图,只需致电:
final DataItemSongs itemSong = getIntent().getExtras().getParcelable(SongsAdapter.ITEM_KEY);
从这里您可以使用itemSong对象并调用其属性并在视图中设置
nameTxt.setText(itemSong.name);