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

Posted

技术标签:

【中文标题】在选项卡布局中看不到我的 cardView【英文标题】:can't see my cardView in tab layout 【发布时间】:2018-06-11 12:08:38 【问题描述】:

我想在 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_
    android:layout_
    android:fitsSystemWindows="true"
    tools:context="com.example.murarilal.musicmania.MainActivity">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            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_
            android:layout_ />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_
        android:layout_
        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_
    android:layout_>

    <android.support.v7.widget.RecyclerView
        android:layout_
        android:layout_
        android:id="@+id/recyclerView">
    </android.support.v7.widget.RecyclerView>

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


</RelativeLayout>

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

【问题讨论】:

从 MainActivity 类调用片段。 ***.com/questions/37653037/… 【参考方案1】:

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

【讨论】:

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

从具有选项卡布局的活动中启动选项卡片段

使用选项卡布局从活动中启动选项卡片段

以编程方式使用选项卡更改片段的选项卡索引

使用带有选项卡布局的自定义视图时无法从选项卡中删除填充

当我在选项卡布局中更改选项卡时,回收站视图项目正在增加

我们如何在android中使用firebase Realtime数据库动态设置选项卡布局的选项卡标题?