RecyclerView内存问题

时间:2015-08-04 17:39:20

标签: android android-recyclerview

只需从ListView更改为RecyclerView。但是,面对记忆问题。

https://github.com/yau34992004/ReuseYourLayoutStructure

尝试下载此来源。启动应用程序并检查网格视图的演示一。打开内存监视器并上下滚动几次。会看到记忆逐渐增长。

任何人都知道为什么?

编辑:

Recycler Adapter

public class DemoAdapter extends RecyclerView.Adapter<DemoViewHolder> {

private ArrayList<String> mArrayList;

public DemoAdapter() {
    mArrayList = new ArrayList<>();
}

@Override
public DemoViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.viewholder_demo, viewGroup, false);
    DemoViewHolder demoViewHolder = new DemoViewHolder(view);
    return demoViewHolder;
}

@Override
public void onBindViewHolder(DemoViewHolder demoViewHolder, int i) {
    demoViewHolder.setTitle(i + "");
}

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

@Override
public int getItemViewType(int position) {
    return 0;
}

public void setArrayList(ArrayList<String> arrayList) {

    if (arrayList == null || arrayList.isEmpty()) {
        throw new IllegalArgumentException("youShouldNotSetEmptyArrayList");
    }

    mArrayList.addAll(arrayList);
}
}

viewholder_demo.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">

<!--<Space-->
    <!--android:layout_width="50dp"-->
    <!--android:layout_height="50dp" />-->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView2"/>

片段

public abstract class RecyclerFragment<T extends RecyclerView.Adapter<?>> extends Fragment {

protected RecyclerView mRecyclerView;
protected T mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAdapter = mRecyclerModel.provideAdapter();

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(getLayoutResourcesId(), container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

    mLayoutManager = new GridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
    return view;
}

protected int getLayoutResourcesId() {
    return R.layout.fragment_vertical_recycler;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    mRecyclerView.setAdapter(null);
    mRecyclerView = null;
}


public T getAdapter() {
    return mAdapter;
}

0 个答案:

没有答案