我刚刚完成了一个项目,其中我有一个包含3个片段的片段活动(每个片段有2个列表视图)。它类似于Google Play商店(水平滑动视图)。
当我通过在我创建的自定义适配器中缓存一些变量来改进我改进的视图时,我有一些滞后。不过,由于我还没有实现这些列表视图中的所有列,我担心稍后会遇到一些性能问题 - 从一开始就使用ViewHolder模式。
考虑到这一点,我搜索了一种方法来加载每个页面中的列表视图,因为我将仅在启动每个片段时更新数据集,但没有运气。
有谁知道这种方法?此外,如果您有一些建议以提高性能,我会留下部分代码(我可能会使用textviews切换标题列表视图):
ScoreBoardAdapter.java
public class ScoreBoardAdapter extends ArrayAdapter<TeamItem>{
private Activity _activity;
private ArrayList<TeamItem> _items;
private String TAG = "ScoreBoard Adapter";
private Typeface _typeface;
private int colorPos;
private int[] colorsRow = new int[] { Color.parseColor("#F0F0F0"), Color.parseColor("#D2E4FC") };
private static class ViewHolder{
TextView tv1, tv2, tv3;
}
/* Esta ArrayList terá que ser substituida/preenchida por um objecto JSON vindo do webservice */
public ScoreBoardAdapter(Activity activity, ArrayList<TeamItem> items){
super(activity, 0, items);
Log.i(TAG,"Constructor call");
this._activity = activity;
this._items = items;
this._typeface = Typeface.createFromAsset(_activity.getAssets(), "GOODTIME.ttf");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = _activity.getLayoutInflater().inflate(R.layout.scoreboard_body, null);
holder.tv1 = (TextView) convertView.findViewById(R.id.scoreboard_body_tv1);
holder.tv2 = (TextView) convertView.findViewById(R.id.scoreboard_body_tv2);
holder.tv3 = (TextView) convertView.findViewById(R.id.scoreboard_body_tv3);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
//Fetch input data
TeamItem lv_item = _items.get(position);
holder.tv1.setText(lv_item.getTeam());
holder.tv2.setText(lv_item.getGames());
holder.tv3.setText(lv_item.getPoints());
//Change font
holder.tv1.setTypeface(_typeface);
holder.tv2.setTypeface(_typeface);
holder.tv3.setTypeface(_typeface);
//Change row color
colorPos = position % colorsRow.length;
convertView.setBackgroundColor(colorsRow[colorPos]);
return convertView;
}
//Mudar os nomes das variaveis quando souber o que cada uma representa (equipa, pontos, etc...)
}
ScoreBoardFragment.java
public class ScoreBoardFragment extends Fragment {
View rootView;
ListView lvHeader, lvTable;
// Dados da tabela
private String jsonInput = "[{\"Equipa\":\"Benfica\",\"Jogos\":7,\"Pontos\":2},{\"Equipa\":\"Sporting\",\"Jogos\":1,\"Pontos\":1},{\"Equipa\":\"Porto\",\"Jogos\":8,\"Pontos\":1},{\"Equipa\":\"Moreirense\",\"Jogos\":4,\"Pontos\":4},{\"Equipa\":\"Braga\",\"Jogos\":7,\"Pontos\":8},{\"Equipa\":\"Guimarães\",\"Jogos\":2,\"Pontos\":2},{\"Equipa\":\"Rio Ave\",\"Jogos\":9,\"Pontos\":0},{\"Equipa\":\"Paços de Ferreira\",\"Jogos\":6,\"Pontos\":5},{\"Equipa\":\"Boavista\",\"Jogos\":0,\"Pontos\":9},{\"Equipa\":\"Estoril\",\"Jogos\":5,\"Pontos\":3}]";
private ArrayList<TeamItem> teamItemArray;
// Cabeçalho
private String[] inputHeaders;
ArrayList<HashMap<String, String>> lv_headers;
HashMap<String, String> headerMap;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
inputHeaders = new String[] {
getActivity().getResources().getString(R.string.equipa),
getActivity().getResources().getString(R.string.jogos),
getActivity().getResources().getString(R.string.pontos) };
/**** Preencher os headers da tabela ***/
fillHeaders();
/**** Preencher o body da tabela ***/
fillBody();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_scoreboard, container,
false);
return rootView;
}
private void fillBody() {
teamItemArray = new TeamItem().retrieveArrayFromJson(jsonInput); // input
// data
lvTable = (ListView) rootView.findViewById(R.id.f_scoreboard_lvbody);
ScoreBoardAdapter listAdapter = new ScoreBoardAdapter(getActivity(),
teamItemArray);
lvTable.setAdapter(listAdapter);
}
private void fillHeaders() {
lvHeader = (ListView) rootView.findViewById(R.id.f_scoreboard_lvheader);
lv_headers = new ArrayList<HashMap<String, String>>();
headerMap = new HashMap<String, String>();
headerMap.put("1", inputHeaders[0]);
headerMap.put("2", inputHeaders[1]);
headerMap.put("3", inputHeaders[2]);
lv_headers.add(headerMap);
try {
ListAdapter listAdapterHeader = new SimpleAdapter(getActivity(),
lv_headers, R.layout.scoreboard_header, new String[] { "1",
"2", "3" }, new int[] { R.id.scoreboard_header_tv1,
R.id.scoreboard_header_tv2,
R.id.scoreboard_header_tv3 });
lvHeader.setAdapter(listAdapterHeader);
} catch (Exception e) {
e.printStackTrace();
}
}
}
SectionsPagerAdapter.java
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private Activity _activity;
private List<String> fragments; // serve para guardar os fragmentos
// (screens) da aplicação
public SectionsPagerAdapter(FragmentManager fm, Activity activity) {
super(fm);
this._activity = activity;
fragments = new ArrayList<String>();
fragments.add(ScoreBoardFragment.class.getName());
fragments.add(FixturesFragment.class.getName());
fragments.add(TopScorersFragment.class.getName());
}
@Override
public Fragment getItem(int position) {
return Fragment.instantiate(_activity, fragments.get(position));
}
@Override
public int getCount() {
// Return number of tabs
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return _activity
.getString(com.eurom.ciftest.R.string.classificacao)
.toUpperCase(l);
case 1:
return _activity.getString(com.eurom.ciftest.R.string.jornada)
.toUpperCase(l);
case 2:
return _activity.getString(
com.eurom.ciftest.R.string.melhores_marcadores)
.toUpperCase(l);
}
return null;
}
}
答案 0 :(得分:1)
我假设你正在使用带有ViewPager的FragmentPagerAdapter。在这种情况下,请尝试将 offscreenPageLimit 设置为高于1的值。有关详细信息,请参阅docs