当列表有大量数据时,我们的应用程序很容易由于应用程序内存不足而崩溃(由于处理不当的行视图)。有两种方法可以有效地解决和管理列表内存
1使用converView
convertView - 如果可能的话,要重用的旧视图。注意:在使用之前,您应该检查此视图是否为非空且类型合适。如果无法将此视图转换为显示正确的数据,则此方法可以创建一个新视图。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null) {
view = this.inflator.inflate(android.R.layout.simple_list_item_1, parent, false);
}
else {
view = convertView;
}
// remaining implementation
return view;
}
阅读更多
2使用RecyclerView
一种灵活的视图,用于为大型数据集提供有限的窗口。
将 RecyclerView 添加到布局
//A RecyclerView with some commonly used attributes
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
将 RecyclerView 小部件添加到布局后,获取对象的句柄,将其连接到布局管理器,并为要显示的数据附加适配器:
public class MyActivity extends Activity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
//... other code
}
适配器提供对数据集中项目的访问,为项目创建视图,并在原始项目不再可见时将某些视图的内容替换为新的数据项目。以下代码示例显示了数据集的简单实现,该数据集由使用 TextView 小部件显示的字符串数组组成:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);
// set the view's size, margins, paddings and layout parameters
...
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
阅读更多
Hope this will help one and all.