回收者视图显示我的视图的静态重复背景

时间:2018-12-22 05:06:15

标签: android android-studio android-fragments android-recyclerview

你好,我是android studio(3.2)的新手,我正试图获得一个回收站视图以显示我的自定义视图的可滚动列表。但是,它在回收者视图的背景中显示了我的视图的静态,不可滚动的重复背景。 看起来像这样(在我滚动之前): enter image description here

然后,当我开始滚动后,静态背景就会出现在视图后面: enter image description here

我的回收者视图的适配器或xml文件是否有问题?另外,我正在片段中构建此回收器视图。

我的适配器:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
   private ArrayList<CardItem> card_item_list;

   // view holder is created from the recycler_card_item template
   public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
       public TextView title_tv, date_tv;
       public RecyclerViewHolder(LinearLayout layout) {
            super(layout);
            title_tv = layout.findViewById(R.id.card_item_title);
            date_tv = layout.findViewById(R.id.card_item_date);
       }
   }

   // take in a list of card items to initialize with
   public RecyclerAdapter(ArrayList<CardItem> card_list) {
        card_item_list = card_list;
   }

    @NonNull
    @Override
    // inflate and creates a viewholder objects, which is from the recycler care item template;
    public RecyclerAdapter.RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
       // create a new card item
        LinearLayout cardLayout = (LinearLayout) LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.recycler_card_item, viewGroup, false);
        RecyclerViewHolder vh = new RecyclerViewHolder(cardLayout);
        return vh;
    }

    @Override
    // bind the CardItem class with the viewholder to complete the card
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
       CardItem target_card = card_item_list.get(position);
       holder.title_tv.setText(target_card.title);
       holder.date_tv.setText(target_card.date);
    }

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

和我的reycler视图的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/recycler_view_item" />

</android.support.constraint.ConstraintLayout>

以下是该片段的代码:

public class RecycleFragment extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter viewAdapter;
    private RecyclerView.LayoutManager viewLayouManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.recycler_fragment, container, false);
        recyclerView = rootView.findViewById(R.id.recycler_view);
        // get and configure the recycle view
        recyclerView.setHasFixedSize(true);
        // layout manager for recyler view
        viewLayouManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(viewLayouManager);
        // creates the initial cards
        ArrayList<CardItem> card_lists = new ArrayList<>();
        for (int i = 0;i<7;i++){
            CardItem card = new CardItem("TODO"+i,"12/" + i);
            card_lists.add(card);
        }
        // adapter for recycler view, initialize the recycler view
        viewAdapter = new RecyclerAdapter(card_lists);
        recyclerView.setAdapter(viewAdapter);
        return rootView;
    }
}

我的活动代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sidebar);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        // starts the recycle view in the content
        RecycleFragment recycleFragment = new RecycleFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, recycleFragment).commit();
    }

activity_sidebar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_sidebar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_sidebar"
        app:menu="@menu/activity_sidebar_drawer" />

</android.support.v4.widget.DrawerLayout>

这是app_bar_sidebar.xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Sidebar">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        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="@android:color/holo_green_dark"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

这是具有片段的content_sidebar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_sidebar"
    tools:context=".Sidebar">

    <fragment
        android:id="@+id/fragment_container"
        android:name="com.steven97102gmail.todoassistant.RecycleFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.511"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

尝试一下

public class RecycleFragment extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter viewAdapter;
    private RecyclerView.LayoutManager viewLayouManager;
    private View rootView;    

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView != null) return rootView;
        rootView = inflater.inflate(R.layout.recycler_fragment, container, false);
        recyclerView = rootView.findViewById(R.id.recycler_view);
        // get and configure the recycle view
        recyclerView.setHasFixedSize(true);
        // layout manager for recyler view
        viewLayouManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(viewLayouManager);
        // creates the initial cards
        ArrayList<CardItem> card_lists = new ArrayList<>();
        for (int i = 0;i<7;i++){
            CardItem card = new CardItem("TODO"+i,"12/" + i);
            card_lists.add(card);
        }
        // adapter for recycler view, initialize the recycler view
        viewAdapter = new RecyclerAdapter(card_lists);
        recyclerView.setAdapter(viewAdapter);
        return rootView;
    }
}

答案 1 :(得分:0)

要将片段附加到活动时,请使用replace而不是add。在诸如设备旋转之类的活动重新创建中,这可能导致在前一个片段上添加新片段。

// starts the recycle view in the content
RecycleFragment recycleFragment = new RecycleFragment();

getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.fragment_container, recycleFragment)
    .commit();