在标签布局中看不到我的cardView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在标签布局中看不到我的cardView相关的知识,希望对你有一定的参考价值。

我想在Tablayout的一个选项卡中显示cardView列表,但是当应用程序运行选项卡显示为空白时。我没有在互联网上获得解决方案。其中一个选项卡中有四个选项卡,应显示cardView列表,但模拟器显示空白活动。我没有在选项卡中获取任何类型的内容。

这是我的主要活动:

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>

我几乎尝试了所有的解决方案,但我没有摆脱这个问题。

答案

我看不到将片段放入TabLayout的部分。当onTabSelected事件触发时,您应将片段放入选项卡中。如果没有,标签只是空的。

以上是关于在标签布局中看不到我的cardView的主要内容,如果未能解决你的问题,请参考以下文章

在 Android Studio 中看不到对话框片段

在ViewPager中看不到碎片

标签栏和导航控制器在视图控制器中看不到了

我在片段中看不到选项菜单

CardView 不在披萨片段中显示图片

选项卡片段内的卡片视图