RecyclerView奇怪的背景颜色

时间:2017-09-04 14:48:11

标签: android android-layout android-recyclerview

正如标题所说,我对RecyclerView和CardView有疑问。 在开发过程中,黑色框架出现在RecyclerView上,未在任何地方定义。有什么建议我怎么能摆脱它?

CardView有两个textView和MapView。

SingleRun是一个带有2个字符串的简单对象。

MainActivity

    MainActivity

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Main Activity onCreate");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new SingleRunsAdapter(new SingleRunProvider().readData(), getApplicationContext()));

CardViewRecyclerViewItem

CardViewRecyclerViewItem


public class CardViewRecyclerViewItem extends CardView {

protected MapView mapView;
protected TextView distance;
protected TextView time;

public CardViewRecyclerViewItem(Context context) {
    this(context, null);
}

public CardViewRecyclerViewItem(Context context, AttributeSet attrs) {
    super(context, attrs);
    View view = LayoutInflater.from(getContext()).inflate(R.layout.card_view_single_run, this);
    mapView = (MapView) view.findViewById(R.id.single_run_map_mapview);
    distance = (TextView) view.findViewById(R.id.details_distance_textview);
    time = (TextView) view.findViewById(R.id.details_time_textview);
}

public void cardViewOnCreate(Bundle savedInstanceState) {
    if (mapView != null) {
        mapView.onCreate(savedInstanceState);
    }
}
public void cardViewOnResume() {
    if (mapView != null) {
        mapView.onResume();
    }
}}

CardViewHolder

public class CardViewHolder extends RecyclerView.ViewHolder {

private CardViewRecyclerViewItem mCardViewRecyclerViewItem;

public CardViewHolder(CardViewRecyclerViewItem cardViewRecyclerViewItem) {
    super(cardViewRecyclerViewItem);
    mCardViewRecyclerViewItem = cardViewRecyclerViewItem;
}

public void cardViewRecyclerViewItemOnResume() {
    if (mCardViewRecyclerViewItem != null) {
        mCardViewRecyclerViewItem.cardViewOnResume();
    }
}}

SingleRunAdapter

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

Context context;

private final List<SingleRun> singleRuns;

public SingleRunsAdapter(List<SingleRun> singleRuns, Context context) {
    this.singleRuns = singleRuns;
    this.context = context;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    CardViewRecyclerViewItem cardViewRecyclerViewItem = new CardViewRecyclerViewItem(context);
    cardViewRecyclerViewItem.cardViewOnCreate(null);
    return new CardViewHolder(cardViewRecyclerViewItem);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
    CardViewHolder cardViewHolder = (CardViewHolder) viewHolder;
    cardViewHolder.cardViewRecyclerViewItemOnResume();
}

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

activity_main.xml中

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.coderspeak.lightweightrunningtracker.single_run.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

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

content_main.xml


    <LinearLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.coderspeak.lightweightrunningtracker.single_run.MainActivity"
        tools:showIn="@layout/activity_main">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="8dp" />

    </LinearLayout>

card_view_single_run.xml

<android.support.v7.widget.CardView xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.google.android.gms.maps.MapView xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/single_run_map_mapview"
            android:layout_width="match_parent"
            android:layout_height="144dp"
            map:liteMode="true"
            map:mapType="normal"
            tools:context=".MapsActivity" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:layout_margin="8dp">

            <TextView
                android:id="@+id/single_run_time"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="00:12:34"
                android:textAlignment="center" />

            <TextView
                android:id="@+id/single_run_distance"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="3.14km"
                android:textAlignment="center" />
        </LinearLayout>

    </LinearLayout>

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

外观如何:

RecyclerView with strange background color

如果您将使用card_view_single_run.xml中的内容和CardViewRecyclerViewItem中的mapView行同时评论LinearLayouts,您将看到:

THIS

在我看来这更奇怪,因为即使空卡,回收商也有一些背景。

感谢您的帮助。如有必要,我可以提供更多代码。

2 个答案:

答案 0 :(得分:0)

诊断此类错误的最佳方法IMO正在Android Studio中使用Layout Inspector工具:

Layout Inspector in Search Everywhere

答案 1 :(得分:0)

尝试以下

在您的MainActivity中

将上下文从getApplicationContext()更改为MainActivity.this

<onException>
     <exception>org.apache.camel.http.common.HttpOperationFailedException</exception>
     <throwException exceptionType="package.path.of.customException" message="custom message" />
</onException>