Recycler视图为黑色

时间:2017-02-20 10:58:34

标签: android

ScreenShot of the screen from Tab(Marshmallow)

带有cardview项目的回收商视图显示为黑色。我也无法改变背景颜色。但是当在Android模拟器中运行时似乎很好,背景默认为白色。我怎么可能做错了

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mRecycleAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerviewss);
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mRecycleAdapter = new RentalAdapter(getApplicationContext(), 75);
        mRecyclerView.setAdapter(mRecycleAdapter);
    }
}
class RentalAdapter extends RecyclerView.Adapter<RentalAdapter.RentalAdapterHolder> {

    private final LayoutInflater mLayoutInflater;
    private Context mContext;
    private int mListCount;
    int lastPosition = -1;
    RentalAdapter(Context context, int listCount) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mListCount = listCount;
    }
    @Override
    public RentalAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.sampcard, parent, false);
        RentalAdapter.RentalAdapterHolder rentalHolder = new RentalAdapterHolder(view);

        return rentalHolder;
    }


    @Override
    public void onBindViewHolder(final RentalAdapterHolder holder, int position) {

        holder.mTextView.setText("sample");


    }

    @Override
    public int getItemCount() {
        return 75;
    }

    class RentalAdapterHolder extends RecyclerView.ViewHolder {

        private TextView mTextView;
        private ImageView mImageView;

        public RentalAdapterHolder(View itemView) {
            super(itemView);
            mTextView = (TextView)itemView.findViewById(R.id.info_text);
        }
    }

}

sampcard.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        card_view:cardCornerRadius="4dp"
        android:layout_marginBottom="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ASKDNAKJDHN"
            android:id="@+id/info_text"/>
    </android.support.v7.widget.CardView>
</LinearLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerviewss"
        />
</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

只需在sampcard.xml布局中添加背景属性.footer-links{ text-align:center; }

android:background="@android:color/white"

答案 1 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        card_view:background_color="@color/white"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        card_view:cardCornerRadius="4dp"
        android:layout_marginBottom="10dp">
        <TextView
            android:layout_width="wrap_content"
          android:gravity="left|center_vertical"
            android:paddingLeft="20dp"
            android:layout_height="match_parent"
            android:text="ASKDNAKJDHN"
            android:id="@+id/info_text"/>
    </android.support.v7.widget.CardView>
</LinearLayout>

试试这段代码。

答案 2 :(得分:0)

我遇到了同样的问题,并通过将背景颜色应用于XML文件中的根视图来解决它,在这种情况下,它是Recycler视图。

在您的Recycler View XML代码中添加:

android:background = "#ffffff"