2015-12-05 64 views
2

我在RecyclerView中顯示項目時有非常奇怪的問題。首先它不顯示項目(奇怪的D/OpenGLRenderer: endAllStagingAnimators on 0x6dd9c400 (CardView) with handle 0x6861d090出現 - 從未發生之前)和後退按鈕不起作用(它似乎但沒有反應)。好,讓我們開始。我有一些Fragment(這裏要說的是片斷與RecyclerViewAndroid RecyclerView奇怪的結局AllStagingAnimators錯誤

enter image description here

我也有1個相同的片段如此,但conatins你可以看到kitchen types和第二個具有meal types其中之一。當我點擊一個列表項,在這兩種情況下采取行動重定向我到新的活動

當我點擊一些項目FE第一位的,在這裏它是這個RecyclerView適配器裏面發生什麼事:

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    KitchenTypeItem kitchenItem = kitchenItems.get(position); 
    holder.kitchenTypeName.setText("Kuchnia " + kitchenItem.getKitchenName()); 
    holder.kitchenTypeImgThumbnail.setImageResource(kitchenItem.getKitchenThumbnail()); 
    holder.kitchenTypeDescription.setText(kitchenItem.getKitchenDescription()); 

    holder.setClickListener(new ItemClickListener() { 

     @Override 
     public void onClick(View view, int position) { 
      KitchenTypeItem kitchenItem = kitchenItems.get(position); 

      Intent intent = new Intent(context, RecipeActivity.class); 
      intent.putExtra(TYPE_NAME, kitchenItem.getKitchenName()); 
      intent.putExtra(DISPLAY_TYPE, 0); 
      context.startActivity(intent); 
     } 

    }); 
} 

當我按的那個項目我送2個參數,以新的活動這是一個:TYPE_NAME - 幫我弄知道,如果我將使用新開張的活動,從包含kitchen types食譜或meal types配方和DISPLAY_TYPE服務器下載數據 - 使用發送請求到MySQL數據庫。這是第二個適配器一些代碼(你可以幾乎相同):

 Intent intent = new Intent(context, RecipeActivity.class); 
     intent.putExtra(TYPE_NAME, mealItem.getMealName()); 
     intent.putExtra(DISPLAY_TYPE, 1); 
     context.startActivity(intent); 

這是我得到新開的活動中:

enter image description here

正如你可以看到,儘管沒有什麼displying事實上RecipeAdapter正確地做出我認爲:

這裏有它的代碼:

public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> { 

    private static String TAG = RecipeAdapter.class.getSimpleName().toString(); 

    private Context context; 
    private ArrayList<RecipeItem> recipeItems; 
    private CoordinatorLayout coordinatorLayout; 

    public RecipeAdapter(Context context, ArrayList<RecipeItem> recipeItems) { 
     this.context = context; 
     this.recipeItems = recipeItems; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item, parent, 
       false); 

     return new ViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     RecipeItem recipeItem = recipeItems.get(position); 
     Picasso.with(context).load(recipeItem.getRecipeImgThumbnailLink()).into(
       holder.recipeItemImgThumbnail); 
     holder.recipeItemTitle.setText(recipeItem.getRecipeTitle()); 
     holder.recipeItemKitchenMealType.setText("Kuchnia " + recipeItem.getRecipeKitchenType() + 
       ", " + recipeItem.getRecipeMealType()); 
     holder.recipeItemAddDate.setText(recipeItem.getRecipeAddDate()); 
     holder.recipeItemLikeCount.setText(recipeItem.getRecipeLikeCount()); 
     holder.setClickListener(new ItemClickListener2() { 

      @Override 
      public void onClick(View view, int position, boolean isLongClick) { 
       if (!isLongClick) { 
        // go to recipes site 
       } else { 
        RecipeItem recipeItem = recipeItems.get(position); 
        FragmentActivity fragmentActivity = (FragmentActivity)(context); 
        FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager(); 
        RecipeAddDialogFragment recipeDialogFragment = new RecipeAddDialogFragment(); 
        Log.d(TAG, "Ustawiono recipeUniqueId, coordinatorLayout oraz " + 
          "recipeDialogFragment w klasie RecipeAddDialogFragment"); 
        recipeDialogFragment.setReferences(recipeItem.getRecipeUniqueID(), 
          coordinatorLayout, recipeDialogFragment); 

        Log.d(TAG, "Uruchamiam okno dialogowe RecipeAddDialogFragment"); 
        recipeDialogFragment.show(fragmentManager, "recipeDialogFragment"); 
       } 
      } 

     }); 
    } 

    @Override 
    public int getItemCount() { 
     return recipeItems.size(); 
    } 

    // Recipe Item Holder 
    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, 
      View.OnLongClickListener { 

     private ImageView recipeItemImgThumbnail; 
     private TextView recipeItemTitle; 
     private TextView recipeItemKitchenMealType; 
     private TextView recipeItemAddDate; 
     private TextView recipeItemLikeCount; 
     private ItemClickListener2 clickListener2; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      recipeItemImgThumbnail = (ImageView) itemView.findViewById(
        R.id.recipe_item_img_thumbnail); 
      recipeItemTitle = (TextView) itemView.findViewById(R.id.recipe_item_title); 
      recipeItemKitchenMealType = (TextView) itemView.findViewById(
        R.id.recipe_item_kitchen_meal_type); 
      recipeItemAddDate = (TextView) itemView.findViewById(R.id.recipe_item_add_date); 
      recipeItemLikeCount = (TextView) itemView.findViewById(R.id.recipe_item_like_count); 

      itemView.setOnClickListener(this); 
      itemView.setOnLongClickListener(this); 
     } 

     public void setClickListener(ItemClickListener2 itemClickListener2) { 
      this.clickListener2 = itemClickListener2; 
     } 

     @Override 
     public void onClick(View view) { 
      clickListener2.onClick(view, getAdapterPosition(), false); 
     } 

     @Override 
     public boolean onLongClick(View view) { 
      clickListener2.onClick(view, getAdapterPosition(), true); 

      return true; 
     } 
    } 

    public void setCoordinatorLayout(CoordinatorLayout coordinatorLayout) { 
     this.coordinatorLayout = coordinatorLayout; 
    } 
} 

下面是該活動的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/recipe_activity_coordinator_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".RecipeActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/log_reg_background" 
     android:orientation="vertical"> 

     <!-- Toolbar --> 
     <include 
      android:id="@+id/toolbar" 
      layout="@layout/tool_bar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/activity_recipe_recyclerview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingBottom="8dp" 
      android:clipToPadding="false"/> 

    </LinearLayout> 

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

這是活動的主要代碼:

public class RecipeActivity extends AppCompatActivity { 

    private static String TAG = RecipeActivity.class.getSimpleName().toString(); 

    private ArrayList<RecipeItem> recipeItems; 
    private RecipeAdapter recipeAdapter; 
    private ProgressDialog pDialog; 
    private CoordinatorLayout coordinatorLayout; 
    private BroadcastReceiver broadcastReceiver; 
    private int offset; 

    private String typeName; 
    private int displayType; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_recipe); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     offset = 0; 

     recipeItems = new ArrayList<>(); 

     coordinatorLayout = (CoordinatorLayout) findViewById(
       R.id.recipe_activity_coordinator_layout); 

     RecyclerView recipeRecyclerView = (RecyclerView) findViewById(
       R.id.activity_recipe_recyclerview); 

     recipeAdapter = new RecipeAdapter(getApplicationContext(), recipeItems); 

     recipeAdapter.setCoordinatorLayout(coordinatorLayout); 

     recipeRecyclerView.setHasFixedSize(true); 
     recipeRecyclerView.setAdapter(recipeAdapter); 
     recipeRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext())); 
     recipeRecyclerView.setItemAnimator(new DefaultItemAnimator()); 

     getBundleExtras(); // Getting Argument Passed By Meal Or Kitchen Type Adapter 
     setActivityTitle(); // Setting Title Depending On Adapter Type 

     setBroadcastReceiver(); 
    } 

    @Override 
    public void onResume() { 
     registerReceiver(broadcastReceiver, new IntentFilter(
       "android.net.conn.CONNECTIVITY_CHANGE")); 
     Log.d(TAG, "Zarejestrowano BroadcastReceiver'a"); 
     super.onResume(); 
    } 

    @Override 
    public void onPause() { 
     unregisterReceiver(broadcastReceiver); 
     Log.d(TAG, "Wyrejestrowano BradcasReceiver'a"); 
     super.onPause(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 

     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     if (id == R.id.home) { 
      NavUtils.navigateUpFromSameTask(this); 

      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    private void getBundleExtras() { 
     Bundle extras = getIntent().getExtras(); 
     // Static Fields Could Bo From MealAdapter Aswell 
     typeName = extras.getString(KitchenTypeAdapter.TYPE_NAME); 
     displayType = extras.getInt(KitchenTypeAdapter.DISPLAY_TYPE); 
     Log.d(TAG, "Trzymano dane z listy fragmentu: typeName=" + typeName + ", displayType=" + 
       displayType); 
    } 

    private void setActivityTitle() { 
     switch (displayType) { 
      case 0: { 
       setTitle(getString(R.string.nav_kitchen_type)); 
       Log.d(TAG, "Ustawiono tytuł: " + displayType); 
       break; 
      } 
      case 1: { 
       setTitle(getString(R.string.nav_meal_type)); 
       Log.d(TAG, "Ustawiono tytuł: " + displayType); 
       break; 
      } 
     } 
    } 

    private void setBroadcastReceiver() { 
     broadcastReceiver = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 
       ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
         Context.CONNECTIVITY_SERVICE); 
       NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
       if (activeNetwork == null) { 
        Log.d(TAG, "Brak połączenia. Pokazuję komuikat."); 
        showSnackbarInfo(getString(R.string.err_msg_connection_problem), 
          R.color.snackbar_error_msg); 
       } else { 
        if (recipeItems.isEmpty()) { 
         Log.d(TAG, "Jest połączenie. Listy puste. Pobieram przepisy."); 
         getRecipes(); 
        } else { 
         Log.d(TAG, "Jest połączenie. Listy pełne. Nic nie robię."); 
        } 
       } 
      } 

     }; 
    } 

    private void getRecipes() { 
     switch (displayType) { 
      case 0: { 
       Log.d(TAG, "Pobieranie przepisów z warunkiem typ_kuchnii"); 
       getKitchenTypeRecipes(); 
       break; 
      } 
      case 1: { 
       Log.d(TAG, "Pobieranie przepisów z warunkiem typ_posiłku"); 
       getMealTypeRecipes(); 
       break; 
      } 
     } 
    } 

    private void getKitchenTypeRecipes() { 
     JsonArrayRequest kitchenTypeRecipesReq = new JsonArrayRequest(prepareGetMethodUrl(), 
       new Response.Listener<JSONArray>() { 

        @Override 
        public void onResponse(JSONArray response) { 
         Log.d("odpowiedz", "Odpowiedź przepisów: " + response.toString()); 
         Log.d("odpowiedz", "Dlugosc response: " + Integer.toString(
           response.length())); 
         if (response.length() == 0) { 
          showSnackbarInfo("Na serwerze nic nie ma (kitchen type)", R.color.snackbar_info_msg); 
         } else { 
          for (int i = 0; i < response.length(); i++) { 
           try { 
            JSONObject jObj = response.getJSONObject(i); 
            RecipeItem recipeItem = new RecipeItem(); 
            recipeItem.setRecipeUniqueID(jObj.getString("unique_id")); 
            recipeItem.setRecipeTitle(jObj.getString("title")); 
            recipeItem.setRecipeImgThumbnailLink(jObj.getString(
              "img_tumbnail_link")); 
            recipeItem.setRecipeAddAte(jObj.getString("add_date")); 
            recipeItem.setRecipeKitchenType(jObj.getString("kitchen_type")); 
            recipeItem.setRecipeMealType(jObj.getString("meal_type")); 
            recipeItem.setRecipeLikeCount(jObj.getString("like_count")); 

           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 
          } 
          recipeAdapter.notifyItemRangeInserted(recipeItems.size(), 
            response.length()); 
         } 
        } 

       }, new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError error) { 
         Log.e("odpowiedz", "Błąd pobierania przepisów (kichen type): " + 
           Integer.toString(error.networkResponse.statusCode)); 

         showSnackbarInfo(Integer.toString(error.networkResponse.statusCode), 
           R.color.snackbar_error_msg); 
        } 

       }); 
     AppController.getInstance().addToRequestQueue(kitchenTypeRecipesReq); 
    } 

    private void getMealTypeRecipes() { 
     JsonArrayRequest mealTypeRecipesReq = new JsonArrayRequest(prepareGetMethodUrl(), 
       new Response.Listener<JSONArray>() { 

        @Override 
        public void onResponse(JSONArray response) { 
         Log.d("odpowiedz", "Odpowiedź przepisów: " + response.toString()); 
         Log.d("odpowiedz", "Dlugosc response: " + Integer.toString(
           response.length())); 
         if (response.length() == 0) { 
          showSnackbarInfo("Na serwerze nic nie ma (meal_type)", 
            R.color.snackbar_info_msg); 
         } else { 
          for (int i = 0; i < response.length(); i++) { 
           try { 
            JSONObject jObj = response.getJSONObject(i); 
            RecipeItem recipeItem = new RecipeItem(); 
            recipeItem.setRecipeUniqueID(jObj.getString("unique_id")); 
            recipeItem.setRecipeTitle(jObj.getString("title")); 
            recipeItem.setRecipeImgThumbnailLink(jObj.getString(
              "img_tumbnail_link")); 
            recipeItem.setRecipeAddAte(jObj.getString("add_date")); 
            recipeItem.setRecipeKitchenType(jObj.getString("kitchen_type")); 
            recipeItem.setRecipeMealType(jObj.getString("meal_type")); 
            recipeItem.setRecipeLikeCount(jObj.getString("like_count")); 

           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 
          } 
          recipeAdapter.notifyItemRangeInserted(recipeItems.size(), 
            response.length()); 
         } 
        } 

       }, new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError error) { 
          Log.e("odpowiedz", "Błąd pobierania przepisów (meal type): " + 
            Integer.toString(error.networkResponse.statusCode)); 

         showSnackbarInfo(Integer.toString(error.networkResponse.statusCode), 
           R.color.snackbar_error_msg); 
        } 

       }); 
     AppController.getInstance().addToRequestQueue(mealTypeRecipesReq); 
    } 

    private String prepareGetMethodUrl() { 
     switch (displayType) { 
      case 0: { 
       return AppConfig.URL_GETRECIPESBYKITCHENTYPE + "?kitchen_type=" + typeName + 
         "&offset=" + Integer.toString(offset); 
      } 

      case 1: { 
       return AppConfig.URL_GETRECIPESBYMEALTYPE + "?meal_type=" + typeName + 
         "&offset=" + Integer.toString(offset); 
      } 

      default: 
       return null; 
     } 
    } 

    // Showing Dialog Window 
    private void showDialog() { 
     if (!pDialog.isShowing()) { 
      pDialog.show(); 
     } 
    } 

    // Hiding Dialog Window 
    private void hideDialog() { 
     if (pDialog.isShowing()) { 
      pDialog.dismiss(); 
     } 
    } 

    // Fucntion Showing Text In Snackbar 
    private void showSnackbarInfo(String info, int textColor) { 
     Snackbar snackbar = Snackbar.make(coordinatorLayout, info, Snackbar.LENGTH_LONG); 

     View sbView = snackbar.getView(); 
     TextView sbText = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); 
     sbText.setTextColor(ContextCompat.getColor(getApplicationContext(), textColor)); 

     snackbar.show(); 
    } 
} 

我可以保證,一切都很好,有下載和putExtra工作正常,所以參數發送正確。只是顯示它的問題和那個奇怪的錯誤:D/OpenGLRenderer: endAllStagingAnimators on 0x6dd9c400 (CardView) with handle 0x6861d090和不工作up button。可以幫我嗎?

+0

你有沒有得到一個答案,我只是注意到相同的logcat - 'D/OpenGLRenderer:endAllStagingAnimators在0xa2c70600(CardView)與處理0xa2c9d350' –

回答

0

我在清單中添加

<application android:hardwareAccelerated="false"> 
<activity android:hardwareAccelerated="true" /> 
</application> 

和解決我的問題。

+0

嗨,周杰倫,歡迎來到堆棧溢出。關於它如何解決這個問題,你的答案會更好一些。具體來說,請閱讀常見問題解答中的[我如何撰寫一個好的答案?](http://stackoverflow.com/help/how-to-answer)部分,瞭解我們正在尋找的一些正確答案的提示。 – GalacticCowboy