setVisibility在片段中不起作用

时间:2016-02-12 08:25:13

标签: android xml android-fragments

我有一个xml文件,其中包含带有可见性的ImageView和TextView"已消失"。 下面是我的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>

我在片段中使用了这个布局。在片段中,当我使用ImageVisibility方法可以看到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));
    }
}

当我调试程序时,debuger转到else部分,但之后我的视图不是visbile

1 个答案:

答案 0 :(得分:0)

尝试从您的图片和textView以及您的活动中使用

生成getter
 mFragment.getImageView().setVisibility(Visible);