Android setVisibility无法在片段中工作

Android setVisibility无法在片段中工作,android,xml,android-fragments,Android,Xml,Android Fragments,我有一个xml文件,其中包含ImageView和TextView,可见性为“gone”。 下面是我的xml文件,名为video_fragment.xml <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="h

我有一个xml文件,其中包含ImageView和TextView,可见性为“gone”。 下面是我的xml文件,名为video_fragment.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white">
        </android.support.v7.widget.RecyclerView>


            <ImageView
                android:id="@+id/emotion_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/emotion"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="125dp"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/emotion_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="@string/video_not_available"
                android:textColor="@color/black_color"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                android:visibility="gone"/>

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

我使用这个布局和片段。在片段中,当我通过setVisibility方法使Imageview和TextView可见时。它不可见。 这是我的片段类

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.video_fragment, container, false);
    emotionImage = (ImageView)view.findViewById(R.id.emotion_image);
    emotionText = (TextView)view.findViewById(R.id.emotion_text);
    final String url = Constant.SUB_CAT_URL+categoryId;
    coordinatorLayout = (CoordinatorLayout) view. findViewById(R.id.fragment_coordinatorLayout);
    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    getSubCategoryItem(url);
    return view;
}

public void getSubCategoryItem(String url){
    ConnectivityManager cm = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nf = cm.getActiveNetworkInfo();
    isConnected = nf != null && nf.isConnectedOrConnecting();
    if(isConnected) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            String message = response.optString(MESSAGE);
                            if (message.equals("Success")) {
                                JSONArray rootJsonArray = response.optJSONArray(DATA);
                                for (int i = 0; i < rootJsonArray.length(); i++) {
                                    videoDetails = new ArrayList<>();
                                    JSONObject childJsonObject = rootJsonArray.optJSONObject(i);
                                    String subCategoryName = childJsonObject.optString(SUB_CATEGORY_NAME);
                                    JSONArray childJsonArray = childJsonObject.optJSONArray(VIDEO_DETAILS);
                                    try {
                                        if (!childJsonArray.equals(null)) {
                                            for (int j = 0; j < childJsonArray.length(); j++) {
                                                JSONObject jsonObject = childJsonArray.optJSONObject(j);
                                                String videoId = jsonObject.optString(VIDEO_ID);
                                                String videoTitle = jsonObject.optString(VIDEO_TITLE);
                                                String videoDes = jsonObject.optString(VIDEO_DESCRIPTION);
                                                String videoDuration = jsonObject.optString(VIDEO_DURATION);
                                                videoDetailsModel = new VideoDetailsModel(videoId, videoTitle, videoDes, videoDuration);
                                                if (subCategoryName.equals("None")) {
                                                    nonSubCagegoryList.add(videoDetailsModel);
                                                } else {
                                                    videoDetails.add(videoDetailsModel);
                                                }
                                            }
                                        } else {
                                            pdialog.dismiss();
                                            emotionImage.setVisibility(View.VISIBLE);
                                            emotionText.setVisibility(View.VISIBLE);

                                        }
                                    } catch (NullPointerException e) {
                                        e.printStackTrace();
                                    }
                                    if (!subCategoryName.equals("None")) {
                                        dashBoardModel = new DashBoardModel(subCategoryName, videoDetails);
                                        dashBoard.add(dashBoardModel);
                                        adapterContent.add(dashBoard);
                                        subCategoryList.put(subCategoryName, videoDetails);
                                        new SubCategoryModel(subCategoryList);
                                    }

                                }

                                DashBoardRecyclerAdapter adapter = new DashBoardRecyclerAdapter(getActivity(), nonSubCagegoryList, adapterContent, categoryId);
                                recyclerView.setAdapter(adapter);
                                pdialog.dismiss();


                            } else {
                                pdialog.dismiss();
                                emotionImage.setVisibility(View.VISIBLE);
                                emotionText.setVisibility(View.VISIBLE);

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                            pdialog.dismiss();
                            new ShowSnackBar(coordinatorLayout, getResources().getString(R.string.server_not_response));

                        }

                    }
                }
        );

        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(20000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(jsonObjectRequest);
    }else {
        pdialog.dismiss();
        new ShowSnackBar(coordinatorLayout,getResources().getString(R.string.check_internet));
    }
}
创建视图时的公共视图(布局、充气机、视图组容器、捆绑包保存状态){ 视图=充气机。充气(R.layout.video_碎片,容器,错误); emotionImage=(ImageView)view.findViewById(R.id.emotion\u image); emotionText=(TextView)view.findViewById(R.id.emotion\u text); 最终字符串url=常量.SUB_CAT_url+categoryId; coordinatorLayout=(coordinatorLayout)view.findViewById(R.id.fragment_coordinatorLayout); recyclerView=(recyclerView)view.findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); layoutManager=新的LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(layoutManager); GetSubcategory项目(url); 返回视图; } public void GetSubcategory项目(字符串url){ ConnectivityManager cm=(ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_服务); NetworkInfo nf=cm.getActiveNetworkInfo(); isConnected=nf!=null&&nf.isConnectedOrConnecting(); 如果(未连接){ JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.GET,url, 新的Response.Listener(){ @凌驾 公共void onResponse(JSONObject响应){ 试一试{ 字符串消息=response.optString(消息); if(message.equals(“Success”)){ JSONArray rootJsonArray=response.optJSONArray(数据); for(int i=0;i mFragment.getImageView().setVisibility(Visible);