添加 CoordinatorLayout 后,tablayout+view pager 中的选项卡标题未显示在 Android 5.0 及更高版本中

Tab Title in tablayout+view pager Not showing in Android 5.0 and above after adding CoordinatorLayout

我已经为我的应用程序开发了选项卡布局,它在 android 的所有版本中都运行良好。但是当我添加一个片段来显示来自数据库的图像时,问题开始于 Lollipop 及以上版本。滑动后标签标题被删除。(如屏幕截图所示。)

android kitkat 上的屏幕

但在 android 棒棒糖上滑动标签后,同一屏幕发生了变化

在一个选项卡上,它完全消失了,就像

我用于添加 tablayout 的代码是...

代码

public class Event360View extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tkt_360_view);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

    tabLayout.addTab(tabLayout.newTab().setText("Event Dtl"));
    tabLayout.addTab(tabLayout.newTab().setText("Minutes"));
    tabLayout.addTab(tabLayout.newTab().setText("Images"));
    tabLayout.addTab(tabLayout.newTab().setText("Chat"));

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    if(General.tabflg) {
        viewPager.setCurrentItem(1);
        General.tabflg=false;
    }
    tabLayout.setOnTabSelectedListener(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) {

        }
    });
}

   }`

PagerAdapter class

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;


public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            TabDetail tab1 = new TabDetail();
            return tab1;
        case 1:
            TabMinutes tab2 = new TabMinutes();
            return tab2;
        case 2:
            TabImg tab3 = new TabImg();
            return tab3;
        case 3:
            TabConversation tab4 = new TabConversation();
            return tab4;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}

Build.gradle

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
    applicationId "cal.lipi.lipicalapp"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    useLibrary 'org.apache.http.legacy'
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.volley:volley:1.0.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
}

main_layout XML

<RelativeLayout
android:id="@+id/main_layout"
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Event360View">

<!--<android.support.v7.widget.Toolbar-->
    <!--android:id="@+id/toolbar"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_alignParentTop="true"-->
    <!--android:background="?attr/colorPrimary"-->
    <!--android:elevation="6dp"-->
    <!--android:minHeight="?attr/actionBarSize"-->
    <!--android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"-->
    <!--app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>-->

<LinearLayout
    android:id="@+id/ll_360genrate"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ProgressBar
        android:id="@+id/tic_360progressBar_gen"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center" />
</LinearLayout>
<LinearLayout
    android:id="@+id/ll_360view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    <!--android:layout_below="@+id/toolbar"-->

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tab_layout"/>
</LinearLayout>

实际问题不在上面的代码中。当我在应用程序中添加以下片段时,问题就开始了。

tabImg.class

package cal.calapp;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


import cal.calapp.adapter.GalleryAdapter;
import cal.calapp.app.AppController;
import cal.calapp.model.Image;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

public class TabImg extends Fragment //implements 
AdapterView.OnItemClickListener
{
    ListView lvVisDtl;
    private static final String endpoint = 
"http://xx.xxx.xxx.xx:81/phpwebservice/xxx/test_ver1.0/img_test.json";
//    private ProgressDialog pDialog;
    private GalleryAdapter mAdapter;
    private RecyclerView recyclerView;
    private ArrayList<Image> images;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.tab_img, container, false);
    }

    @Override
    public void onStart()
    {
    super.onStart();
    try
    {
//            Toolbar toolbar = (Toolbar) 
getActivity().findViewById(R.id.toolbar);
//            getActivity().setSupportActionBar(toolbar);

        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);

//            pDialog = new ProgressDialog(getActivity());
        images = new ArrayList<>();

        mAdapter = new GalleryAdapter(getActivity().getApplicationContext(), images);

        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), 2);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(getActivity().getApplicationContext(), recyclerView, new GalleryAdapter.ClickListener() {
            @Override
            public void onClick(View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("images", images);
                bundle.putInt("position", position);

                FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
                newFragment.setArguments(bundle);
                newFragment.show(ft, "slideshow");
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));
        fetchImages();

    }
    catch(Exception ex)
    {
        Toast.makeText(getActivity().getBaseContext(),ex.toString(), Toast.LENGTH_LONG).show();
    }
}
private void fetchImages() {


    JsonArrayRequest req = new JsonArrayRequest(endpoint,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    images.clear();
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject object = response.getJSONObject(i);
                            Image image = new Image();
                            image.setName(object.getString("name"));

                            JSONObject url = object.getJSONObject("url");
                            image.setSmall(url.getString("small"));
                            image.setMedium(url.getString("medium"));
                            image.setLarge(url.getString("large"));
                            image.setTimestamp(object.getString("timestamp"));

                            images.add(image);

                        } catch (JSONException e) {
                            Log.e(TAG, "Json parsing error: " + e.getMessage());
                        }
                    }

                    mAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    AppController.getInstance().addToRequestQueue(req);

}
}

Xml 对于 tabImg

<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">



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

<include layout="@layout/content_main" />

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

请帮我解决这个问题..

你没有提供你的布局文件,所以不清楚,请尝试下面的代码。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

     <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        style="@style/style_tab_strip"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:tabTextAppearance="@style/CustomTabText" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="15dp"
        android:layout_weight="1" />
</LinearLayout>

我的 gradle 看起来像

compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:support-v4:25.0.0'
compile 'com.android.support:design:25.0.0'

和构建工具版本

 buildToolsVersion "25.0.0"

请看这里的完美演示: https://github.com/hitesh-dhamshaniya/AndroidMaterialDesignTabViewpager

即使我接受了问题的实际答案(@Hitesh Dhamshaniya),但就我而言 我刚刚从 XML

中的 CoordinatorLayout 中删除了以下行
    android:fitsSystemWindows="true"

它在所有 android 版本中都开始正常工作..