我在Android上观看显示器,我注意到向下滚动时内存不断增加。
我使用的是Adaper
。它应该重用视图,但似乎它不起作用。
正如您在下面的图像中看到的那样,内存从9.94 MB开始,我可以通过向下和向上滚动将其增加到25.82 MB。
您可以看到我使用的代码。
public class ListMainFragment
extends Fragment
implements LoaderManager.LoaderCallbacks<List<Earthquake>> {
public ListMainFragment() {
// Required empty public constructor
}
ListView listView;
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_mainlist, container, false);
final ArrayList<Earthquake> earthquake =
(ArrayList<Earthquake>) new EarthquakeController(new EarthquakesJson()).getListOfEarthquakes();
listView = (ListView)viewRoot.findViewById(R.id.list_view);
EarthquakeAdapter noteAdapter = new EarthquakeAdapter(getActivity(), earthquake);
listView.setAdapter(noteAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//final String mensaje = notas.get(position).getMag();
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(earthquake.get(0).getMapURL()));
Log.w("ListView", "Se despliega la siguente URL " + earthquake.get(0).getMapURL());
startActivity(intent);
}
});
return viewRoot;
}
}
Adapter
:
public class EarthquakeAdapter extends ArrayAdapter <Earthquake> {
public EarthquakeAdapter(Context context, ArrayList<Earthquake> notas) {
super(context, 0, notas);
}
private static final String LOCATION_SEPARATOR = " of";
View view;
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
*/
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
Log.w("AppList", "converView es null");
}
Earthquake note = getItem(position);
Date dateObject = new Date(note.getTimeInMilliseconds());
TextView locationOffset = (TextView)convertView.findViewById(R.id.listItem_tv_locationOffset);
TextView place = (TextView)convertView.findViewById(R.id.listItem_tv_place);
TextView date = (TextView)convertView.findViewById(R.id.lisItem_tv_date);
TextView time = (TextView)convertView.findViewById(R.id.lisItem_tv_time);
TextView mag = (TextView) convertView.findViewById(R.id.listItem_tv_mag);
GradientDrawable magnitudCicle = (GradientDrawable) mag.getBackground();
magnitudCicle.setColor(getMagnitudColor(note.getMag()));
String str_locationOffset, str_place;
if (note.getPlace().contains(LOCATION_SEPARATOR)) {
String [] locations = note.getPlace().split(LOCATION_SEPARATOR);
str_locationOffset = locations[0];
str_place = locations[1];
}
else {
str_place = note.getPlace();
str_locationOffset = getContext().getString(R.string.near_the);
}
mag.setText( new DecimalFormat("0.0").format(note.getMag()));
date.setText(formatDate(dateObject));
place.setText(str_place);
time.setText(formatTime(dateObject));
locationOffset.setText(str_locationOffset);
/* final String mensaje = title.getText().toString();
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),mensaje,Toast.LENGTH_SHORT).show();
}
});*/
return convertView;
}
}
答案 0 :(得分:0)
您应该使用 Recycler View 来获得更好的效果。
来自:https://developer.android.com/training/material/lists-cards.html