简体   繁体   English

在选项卡布局中看不到我的cardView

[英]can't see my cardView in tab layout

I want to show list of cardView in one of the tab of Tablayout, but when app runs tab appears blank. 我想在Tablayout的标签之一中显示cardView的列表,但是当应用程序运行时标签显示为空白。 I am not getting the solution on the internet. 我没有在互联网上找到解决方案。 There are four tabs in one of the tabs the list of cardView should appear, but emulator is showing blank activity.im not getting any kind of content in tab. cardView列表应出现的一个选项卡中有四个选项卡,但仿真器显示空白的活动。在该选项卡中没有任何内容。

This is my Mainactivity: 这是我的主要活动:

public class MainActivity extends AppCompatActivity {


    private ViewPagerAdapter pagerAdapter;
    private ViewPager mViewPager;
    private Toolbar toolbar;
    private TabLayout tabLayout;

    @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);
        mViewPager.setAdapter(pagerAdapter);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
                switch (tab.getPosition()) {
                    case 0:
                        Log.e("TAG", "TAB1");
                        break;
                    case 1:
                        Log.e("TAG", "TAB2");
                        break;
                    case 2:
                        Log.e("TAG", "TAB3");
                        break;
                }
            }

            @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 = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.recyclerView);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
    */


    }
}

this is the fragment 

public class SongsTab extends Fragment {

    RecyclerView recyclerView;
    private ArrayList<songInfo> songs= new ArrayList<>();
    songAdapter songAdapter;
    MediaPlayer mediaPlayer;
    SeekBar seekBar;
    SeekBar seekBar2;
    private int seekForwardTime = 5000;
    private int seekBackwardTime = 5000;
    Cursor cursor;

    @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);
        songAdapter = new songAdapter(songs, getContext());
        recyclerView.setAdapter(songAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                linearLayoutManager.getOrientation());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(dividerItemDecoration);
        mediaPlayer = new MediaPlayer();

        songAdapter.setOnitemClickListener(new songAdapter.OnitemClickListener() {

            @Override
            public void onItemclick(final songAdapter.ViewHolder holder, View v, songInfo obj, int position) {

                System.gc();
                int songPath = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
                cursor.moveToPosition(position);
                final String filename = cursor.getString(songPath);

                if (mediaPlayer.isPlaying()) {
                    // mediaPlayer.stop();
                    mediaPlayer.reset();
                    // mediaPlayer.release();
                    //mediaPlayer = null;
                } else try {
                  /*  Intent intent = new Intent( MainActivity.this, Main2Activity.class  );
                    startActivity(intent);*/

                    try {
                        mediaPlayer.setDataSource(filename);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    mediaPlayer.prepareAsync();

                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
                    {
                        @Override
                        public void onPrepared (MediaPlayer mp){

                            mp.start();
                            seekBar.setProgress(0);
                            seekBar.setMax(mediaPlayer.getDuration());


                            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                                @Override
                                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                                    mediaPlayer.seekTo(i);
                                }

                                @Override
                                public void onStartTrackingTouch(SeekBar seekBar) {

                                }

                                @Override
                                public void onStopTrackingTouch(SeekBar seekBar) {

                                }
                            });


                                                              // });
                                                              //  mediaPlayer.prepareAsync();
                        }
                    }

                    );


                }catch(Exception e){
                }


            } ;

        });
        checkUserPermission();
        return v;
    }


    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{
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        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 = getActivity().getContentResolver().query(uri,null,selection,null,null);
        if(cursor != null){
            if(cursor.moveToFirst()){
                do{
                    String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                    String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    int songPath = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);

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

                }while (cursor.moveToNext());

            }

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

        }
    }



    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        cursor.close();

    }

    /*
        public void nextSong()  {

            int currentPosition = mediaPlayer.getCurrentPosition();
            // check if seekForward time is lesser than song duration
            if(currentPosition + seekForwardTime <= mediaPlayer.getDuration()){
                // forward song
                mediaPlayer.seekTo(currentPosition + seekForwardTime);
            }else{
                // forward to end position
                mediaPlayer.seekTo(mediaPlayer.getDuration());
            }
        }
    */
    public void play(){


        mediaPlayer.start();
    }


    }

this is  xml mainactivity
<?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>

recycler view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerView">
    </android.support.v7.widget.RecyclerView>

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="60dp"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

I have almost tried all the solution, but I'm not getting rid of this problem. 我几乎尝试了所有解决方案,但并没有摆脱这个问题。

I can't see the part where you put a Fragment into your TabLayout. 我看不到将Fragment放入TabLayout的部分。 When the onTabSelected Event fires you should put your Fragment in the tab. 当onTabSelected事件触发时,应将Fragment放在选项卡中。 If not, the tab is just empty. 如果不是,则选项卡只是空的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM