2016-07-04 19 views
3

RecyclerViewCardViewに問題があります。 私のCardViewが突然黒くなり、私のRatingBarが青くなりました。私はInfiniteRecyclerViewを使用していますが、それを単純な0​​に変更することは効果がありません。 CardViewの背景色を白に変更することはできますが、RatingBarはまだ青です。これは、何が起こっているかの例です:CardViewが突然黒くなった

私は同じアダプターを断片内に別のアクティビティに通常の RecyclerViewを使用して、それだけで正常に見える

Black CardView

。次に例を示します。

Normal CardView

これはsingle_recipe_recycler_item.xmlレイアウトファイルです:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 
android:layout_marginTop="5dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?android:attr/selectableItemBackground" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/recipe_recycler_image" 
     android:layout_width="80dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_vertical" 
     android:src="@mipmap/ic_launcher" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="5dp" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:paddingTop="5dp"> 

     <!-- RealmRecipe Title --> 
     <TextView 
      android:id="@+id/recipe_recycler_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:gravity="end" 
      android:text="Very very very very very very long title" 
      android:textColor="@color/colorBlack" 
      android:textSize="@dimen/title" 
      android:textStyle="bold" /> 

     <!-- Publisher --> 
     <TextView 
      android:id="@+id/recipe_recycler_publisher" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_below="@id/recipe_recycler_title" 
      android:layout_marginTop="5dp" 
      android:text="Publisher" 
      android:textColor="@color/colorBlack" 
      android:textSize="@dimen/genre" 
      android:textStyle="italic" /> 

     <!-- Rating --> 
     <RatingBar 
      android:id="@+id/recipe_recycler_rating_bar" 
      style="@style/Widget.AppCompat.RatingBar.Small" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_below="@id/recipe_recycler_publisher" 
      android:layout_marginTop="5dp" 
      android:isIndicator="true" 
      android:max="5" /> 

    </RelativeLayout> 

</LinearLayout> 

</android.support.v7.widget.CardView> 

これは私のAppThemeです:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

ですが、なぜでしょうか? CardViewにバグはありますか?どのようにこれを修正するための任意の提案?

+0

'LinearLayout'で' selectableItemBackground'の色は何ですか? – Razor

+0

これは波紋効果の背景描画可能です。 'selectableItemBackground'を適用する前にCardViewが黒くなったので、問題の原因ではありません。 – frozzyk

+0

あなたのアプリをテストしているAndroidのバージョンは何ですか? – Razor

答えて

7

を働いていた私は、同じ問題に遭遇し、ここで私はそれを解決する方法です。 アダプタが次のようにレイアウトを拡張することを確認します。あなたはのは

@Override 
public View getView(int position, View view, ViewGroup parent) { 
    if (view == null){ 
     LayoutInflater layoutInflater = (LayoutInflater) getContext() 
       .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     view = layoutInflater.inflate(R.layout.recycler_row_layout_person, null, true); 

    } 

MyAdapterこのアダプタを呼びましょうよりよく理解するためには、あなたはあなたのアダプターを使用することがありますする活動のために、あなたは右のコンテキストを使用していることを確認してください。ここでMyAdapterというアクティビティをMyActivityという名前で使用しているとします。

  1. まず間違った方法を次のように間違っコンテキストとレイアウトのインフレータを使用する間違っの方法がある

    MyAdapter adapter = new MyAdapter(
              getApplicationContext(), R.layout.recycler_row_layout_person, arrayList 
            ); 
    
  2. 第二に間違った方法

    MyAdapter adapter = new MyAdapter(
              getContext(), R.layout.recycler_row_layout_person, arrayList 
            ); 
    

ここには方法です。そして、これが私がそれを解決した方法です。

MyAdapter adapter = new MyAdapter(
           MyActivity.this, R.layout.recycler_row_layout_person, arrayList 
         ); 

私はそれは私が同じ問題を抱えていた

+0

このコードは 'RecyclerView'ではなく' ListView'用のアダプタのようです。 – PPartisan

+0

はい、問題は間違ったコンテキストを使用していました!ありがとうございました! – frozzyk

3

私は同じ問題を抱えていたし、これを試してみました、それは私のため

card_view:cardBackgroundColor="#fff" 

あなたのXMLファイル

<android.support.v7.widget.CardView 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 
android:layout_marginTop="5dp" 
card_view:cardBackgroundColor="#fff"> 
+0

私は文脈が参照されている方法に間違ったことは見ませんでしたが、これはうまくいきました。あなたが何か他のものにカードビューをラップすると、問題ではないかもしれないと思います。 – Justin

1

に役立ちます願っています。私のためのトリックをした

<android.support.v7.widget.CardView 
    ...  
    card_view:cardBackgroundColor="#ffffff"> 

:私の場合は 私はデフォルトで使用、間違ったプロパティで背景を定義します。

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="300dp" 
    android:layout_height="200dp" 
    android:layout_gravity="center" 
    android:layout_marginTop="8dp" 
    android:background="#ffffff"> 

をそして、あなたはcardbackgroundプロパティを使用する必要があります。 Cardviewは独自のプロパティを持ち、デフォルトのViewプロパティの代わりにそれらを使用する必要があります。 エレベーションプロパティも同様です。

関連する問題