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

时间:2017-04-04 05:20:22

标签: android android-viewpager android-tablayout android-coordinatorlayout

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

关于android kitkat的屏幕

enter image description here

但是在将标签滑动为

后,在Android棒棒糖上更改了相同的屏幕

enter image description here

在一个标签上它完全消失了

enter image description here

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

CODE

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类

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>

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

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);

}
}

用于tabImg的Xml

<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>

请帮我解决这个问题..

2 个答案:

答案 0 :(得分:4)

您没有提供您的布局文件,因此请不要明白,请尝试以下代码。

    <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

答案 1 :(得分:0)

即使我接受了实际答案(@Hitesh Dhamshaniya)的问题,但在我的情况下 我刚从XML中删除了CoordinatorLayout中的bellow行

    android:fitsSystemWindows="true"

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