2016-02-12 54 views
1

我有一個xml文件,其中包含ImageView和TextView的可見性「消失」。 下面是我的XML文件,它的名字是video_fragment.xmlsetVisibility不能在片段中工作

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

我片段使用此佈局。在片段,當我使Imageview和TextView通過setVisibility方法可見。它不可見。 這裏是我的片段類

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去別的部分,但在那之後我的看法是不visbile

回答

0

嘗試從你的形象和TextView的,並在你的活動中使用

產生的吸氣
mFragment.getImageView().setVisibility(Visible);