【问题标题】:Recycler view shows a static duplicated background of my views [duplicate]回收站视图显示了我的视图的静态重复背景 [重复]
【发布时间】:2018-12-22 05:06:15
【问题描述】:

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

然后在我开始滚动后,我的视图后面会出现一个静态背景:

这是我的回收器视图的适配器或 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>

【问题讨论】:

  • 请在您将Fragment 添加到活动的位置发布代码。
  • 我的意思是你的活动代码,你如何将片段添加到活动中。我认为您添加了两次片段。
  • &lt;fragment&gt; 元素不仅仅是Fragment 的占位符。它实际上导致在setContentView() 调用中的膨胀期间将Fragment 实例加载到该位置。将其更改为&lt;FrameLayout&gt;
  • 我的意思是,只需将&lt;fragment ... 更改为&lt;FrameLayout ...。然后,您还可以删除android:name 属性或class 属性;无论你现在在那里。
  • 有效!谢谢,不知道片段已经和要加载的实例。

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


【解决方案1】:

当您想将片段附加到活动时,请使用replace 而不是add。它可能会导致在活动娱乐(例如设备旋转)中在前一个片段上添加一个新片段。

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

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

【讨论】:

  • 感谢您的帮助,但这对我仍然不起作用。我仍然得到奇怪的静态背景。
  • 对此我很抱歉。什么时候发生?第一次开始?还是旋转后?去参加另一个活动然后回来?等等……
  • 第一次启动,我的主要activity在onCreate方法中启动fragment
  • 可以发activity_sidebar.xml吗?
  • 当应用程序在暗模式和亮模式之间转换时遇到了这个问题,我使用的是添加 API 而不是替换 API。使用 replace api 实际上解决了该案例的问题。谢谢@aminography
【解决方案2】:

试试这个

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;
    }
}

【讨论】:

  • 谢谢,但不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 2019-05-22
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多