适配器不附加跳过布局

时间:2018-01-03 06:21:59

标签: android android-layout android-fragments android-recyclerview android-adapter

我收到错误,说没有适配器附加跳过布局。当模拟器运行时,列表不会出现。请帮助。我无法看到列表中的任何内容。屏幕显示为空白。活动中的其他项目是appearing.i已经在网上看到了几乎所有的答案,但没有得到任何解决方案。当我试图在其他标签中制作列表视图时,我得到了列表。

这是 songsTab活动

public class SongsTab extends Fragment {

    private ArrayList<songInfo> songs = new ArrayList<songInfo>();
    RecyclerView recyclerView;
    SeekBar seekBar;
    songAdapter songAdapter1;
    MediaPlayer mediaPlayer;
    Cursor cursor;
    private Handler myHandler = new Handler();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.songs, container, false);
        recyclerView = (RecyclerView)v. findViewById(R.id.recyclerView);
        seekBar = (SeekBar) v.findViewById(R.id.seekBar);
        songAdapter1 = new songAdapter(songs, getActivity());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                linearLayoutManager.getOrientation());
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(songAdapter1);
        solved();
        checkUserPermission();

        Thread t = new runThread();
        t.start();
        return v;
    }

    public void solved() {

        songAdapter1.setOnitemClickListener(new songAdapter.OnitemClickListener() {
            @Override
            public void onItemclick(songAdapter.ViewHolder holder, View v, final songInfo obj, int position) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                    mediaPlayer.release();
                    mediaPlayer = null;

                } else {

                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mediaPlayer = new MediaPlayer();
                                mediaPlayer.setDataSource(obj.getSongPath());
                                mediaPlayer.prepareAsync();
                                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                    @Override
                                    public void onPrepared(MediaPlayer mp) {
                                        mp.start();
                                        seekBar.setProgress(0);
                                        seekBar.setMax(mediaPlayer.getDuration());
                                        Log.d("Prog", "run: " + mediaPlayer.getDuration());
                                    }
                                });


                            } catch (Exception e) {
                            }
                        }

                    };
                    myHandler.postDelayed(runnable, 100);

                }
            }
        });
    }


    public class runThread extends Thread {


        @Override
        public void run() {
            while (true) {

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d("Runwa", "run: " + 1);
                if (mediaPlayer != null) {
                    seekBar.post(new Runnable() {
                        @Override
                        public void run() {
                            seekBar.setProgress(mediaPlayer.getCurrentPosition());
                        }
                    });

                    Log.d("Runwa", "run: " + mediaPlayer.getCurrentPosition());
                }
            }
        }

    }

    private void checkUserPermission(){
        if(Build.VERSION.SDK_INT>=23){
            if(ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED){
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},123);
                return;
            }
        }
        loadSongs();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 123:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    loadSongs();
                }else{
                    Toast.makeText(getContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
                    checkUserPermission();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        }

    }

    private void loadSongs(){
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC+"!=0";
         cursor = getContext().getContentResolver().query(uri,null,selection,null,null);
        if(cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    String SongPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                    songInfo s = new songInfo(name, artist, SongPath);
                    songs.add(s);


                } while (cursor.moveToNext());
            }

            cursor.close();
            songAdapter1 = new songAdapter(songs, getActivity());

        }
    }
}

这是我的适配器

public class songAdapter extends RecyclerView.Adapter<songAdapter.ViewHolder> {
    ArrayList<songInfo> songs;
    Context context;
    OnitemClickListener onitemClickListener;

    public songAdapter(ArrayList<songInfo> songs, Context context) {
        this.songs = songs;
        this.context = context;
    }



    public interface OnitemClickListener{
        void onItemclick(ViewHolder holder, View v, songInfo obj, int position);
    }
    public void setOnitemClickListener(OnitemClickListener onitemClickListener)
    {
        this.onitemClickListener=onitemClickListener;
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView= LayoutInflater.from(context).inflate(R.layout.card,parent,false);
        return new ViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final songInfo song = songs.get(position);
        holder.songName.setText(songs.get(position).getSongName());
        holder.artistName.setText(songs.get(position).getArtistName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onitemClickListener!=null)
                {
                    onitemClickListener.onItemclick(holder,view,song,position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return songs.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView songName,artistName;
        public ViewHolder(View itemView) {
            super(itemView);
            songName=(TextView)itemView.findViewById(R.id.songName);
            artistName=(TextView)itemView.findViewById(R.id.artistName);

        }


    }
}  

主要活动

public class MainActivity extends AppCompatActivity {

    public ViewPagerAdapter pagerAdapter;
    private ViewPager mViewPager;
    private Toolbar toolbar;
    private TabLayout tabLayout;
    public static View rootView;
    public static int tabNo;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        mViewPager.setAdapter(pagerAdapter);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {


            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabNo = tab.getPosition();
                mViewPager.setCurrentItem(tabNo);//setting current selected item over viewpager

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();

            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
//              View rootView;

            switch (getArguments().getInt(ARG_SECTION_NUMBER))
            {
                case 1: {

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

                    break;
                }
                case 2: {
                    rootView = inflater.inflate(R.layout.album, container, false);
                    break;
                }

                case 3: {
                    rootView = inflater.inflate(R.layout.genres, container, false);
                    break;
                }
                case 4: {
                    rootView = inflater.inflate(R.layout.artist, container, false);
                    break;
                }

            }
            return rootView;


        }

    }
}

recyclerView xml
    <?xm version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView">
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:id="@+id/recyclerView"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:layout_above="@+id/seekBar" />

        </ScrollView>

        <SeekBar
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/seekBar"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="37dp"
            android:indeterminate="false" />




    </RelativeLayout>

this is cardView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:cardBackgroundColor="#f5fffa"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/songName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:text="Shape of you"
                    android:maxLines="1"/>

                <TextView
                    android:id="@+id/artistName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:text="Ed Sheeran"
                    android:maxLines="1"/>
            </LinearLayout>

        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

mainActivity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.murarilal.musicmania.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}内再次初始化songAdapter1

loadSongs() class:

中创建这样的方法
songAdapter

public void notifySongAdded(ArrayList<songInfo> songs) { this.songs = songs; notifyDataSetChanged(); } 内添加歌曲后致电songAdapter1.notifySongAdded(songs);