我的问题是,当我第一次运行我的应用程序时,它不会加载片段中的数据,只有当我关闭然后打开它时,歌曲才会显示出来。 我该如何解决?
我有5个选项卡,其中包含保存歌曲数据并使用自定义recyclerview适配器显示它们的数组列表。
检查权限和setupViewPager在我的MainActivity.java内部
然后我制作了单独的Tab类(Tab1.java,Tab2.java等)
问题是在授予权限后,它仅显示我的viewpager布局,但不显示所有歌曲的recyclerview。
仅当我退出应用程序或切换选项卡时,它才有效。
检查权限
// Permission read-write storage, read phone-state
private void checkUserPermission() {
Log.d("TAG", "checkUserPermissions: asking for user permissions!");
String [] permissions = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE};
if (ContextCompat.checkSelfPermission(this.getApplicationContext(), permissions[0]) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this.getApplicationContext(), permissions[1]) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this.getApplicationContext(), permissions[2]) == PackageManager.PERMISSION_GRANTED) {
setupViewPager();
}else {
ActivityCompat.requestPermissions(MainActivity.this, permissions, REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestcode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
switch (requestcode){
case 123:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
setupViewPager();
}else{
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
checkUserPermission();
break;
}
default:
}
super.onRequestPermissionsResult(requestcode, permissions,
grantResults);
}
setupViewPager
private void setupViewPager() {
//TabLayout
tabLayout = findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Songs"));
tabLayout.addTab(tabLayout.newTab().setText("Albums"));
tabLayout.addTab(tabLayout.newTab().setText("Artists"));
tabLayout.addTab(tabLayout.newTab().setText("Genres"));
tabLayout.addTab(tabLayout.newTab().setText("Playlist"));
tabLayout.setTabGravity(TabLayout.MODE_FIXED);
viewPager = findViewById(R.id.viewPager);
tabsAdapter = new TabsAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(tabsAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
TAB:在这里我会显示所有歌曲
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tab1, container, false);
recyclerViewSongs = rootView.findViewById(R.id.recyclerViewSongs);
activeSongTab1 = rootView.findViewById(R.id.activeSongTab1);
albumIvBottom = rootView.findViewById(R.id.albumIvBottom);
tvCurrSongTitle = rootView.findViewById(R.id.tvCurrSongTitle);
tvCurrSongArtist = rootView.findViewById(R.id.tvCurrSongArtist);
Main.musicList = Main.songs.songs;
// Connects the song list to an adapter
// (thing that creates several Layouts from the song list)
if ((Main.musicList != null) && (! Main.musicList.isEmpty())) {
AllSongsAdapter allSongsAdapter = new AllSongsAdapter(getContext(), Main.musicList);
recyclerViewSongs.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerViewSongs.setHasFixedSize(true);
recyclerViewSongs.setAdapter(allSongsAdapter);
Log.i(TAG, "List is not empty");
}else{
Log.i(TAG, "List is empty");
}
recyclerViewSongs.addOnItemTouchListener(new OnItemClickListeners(getContext(), new OnItemClickListeners.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.O)
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getContext(), "You Clicked position: " + position, Toast.LENGTH_SHORT).show();
playAudio(position);
//Fetch current song details
tvCurrSongTitle.setText(Main.musicList.get(position).getTitle());
tvCurrSongArtist.setText(Main.musicList.get(position).getArtist());
//Fetch album art activeSong
//loadAlbumArtBottom();
}
}));
activeSongTab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent activeSong = new Intent(getContext(), SongActivity.class);
startActivity(activeSong);
}
});
return rootView;
}
RecyclerView适配器
public class AllSongsAdapter extends RecyclerView.Adapter<AllSongsAdapter.SongHolder>{
private LayoutInflater songInf;
private Context context;
private ArrayList<Song> musicList;
public AllSongsAdapter(Context context, ArrayList<Song> musicList) {
super();
this.musicList = musicList;
this.context = context;
songInf = LayoutInflater.from(context);
}
public static class SongHolder extends RecyclerView.ViewHolder {
TextView tvSongTitle,tvArtistName;
ImageView albumIv;
public SongHolder(final View itemView) {
super(itemView);
tvSongTitle = itemView.findViewById(R.id.tvSongTitle);
tvArtistName = itemView.findViewById(R.id.tvArtistName);
albumIv = itemView.findViewById(R.id.albumIv);
//tvIndex = itemView.findViewById(R.id.tvIndex);
}
}
@NonNull
@Override
public SongHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
View view = songInf.inflate(R.layout.song_layout, viewGroup, false);
return new SongHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SongHolder holder, final int position) {
//holder.tvIndex.setText(String.format(Locale.US, "%d", position));
Song currentSong = musicList.get(position);
String title = currentSong.getTitle();
if (title.isEmpty()){
holder.tvSongTitle.setText("<Unknown>");
}else{
holder.tvSongTitle.setText(currentSong.getTitle());
}
String artist = currentSong.getArtist();
if (artist.isEmpty()){
holder.tvArtistName.setText("<Unknown>");
}else {
holder.tvArtistName.setText(currentSong.getArtist());
}
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, currentSong.getAlbumID());
Picasso.with(context)
.load(albumArtUri)
.placeholder(R.drawable.no_album)
.error(R.drawable.no_album)
.resize(220,220)
.centerCrop()
.into(holder.albumIv);
}
@Override
public int getItemCount() {
return musicList.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
TabsAdapter
public class TabsAdapter extends FragmentStatePagerAdapter{
int numberOfTabs;
public TabsAdapter(FragmentManager fm, int NumberOfTabs) {
super(fm);
this.numberOfTabs = NumberOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new Tab1();
case 1:
return new Tab2();
case 2:
return new Tab3();
case 3:
return new Tab4();
case 4:
return new Tab5();
default: return null;
}
}
@Override
public int getCount() {
return numberOfTabs;
}
}
答案 0 :(得分:0)
尝试添加类似permissionRequested
的布尔值,如果权限请求通过,则将其设置为true
,然后在您的onResume
中使用它:
if(permissionRequested){ setupViewPager(); permissionRequested = false; }
您的viewPager依靠fragmentmanager
,并且在活动完全恢复之前调用onRequestPermissionsResult
,这会在添加片段时导致奇怪的行为。
也:
您绝不应在onRequestPermissionsResult
内部重新请求被拒绝的许可。
如果用户选中“不再询问”,这将导致无限循环的重新请求(用户不可见),并且会干扰您的活动。权限请求是一项叠加活动,因此您将不断收到onPause
onStart
回调。