@Override protected void initialize() { ListView lv = (ListView) findViewById(android.R.id.list); mEntries = new ArrayList<>(); getSupportLoaderManager().initLoader(LOADER, new Bundle(), DefaultEngineAsyncListActivity.this); ArrayAdapter myAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, android.R.id.text1, mEntries) { private static final int TYPE_EVEN = 0; private static final int TYPE_ODD = 1; protected static final int NEAR_END_THRESHOLD = 3; private OnPositionNearEndListener mNearEndListener = new OnPositionNearEndListener() { @Override public void onNearEnd() { final Loader l = getSupportLoaderManager().getLoader(LOADER); if (l != null && ((ListDataLoader) l).hasMoreResults() && !((ListDataLoader) l).isLoading()) { l.forceLoad(); } } }; public boolean isNearEndOfList(int position) { return getCount() - position <= NEAR_END_THRESHOLD; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { return position % 2 == 0 ? TYPE_EVEN : TYPE_ODD; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (mNearEndListener != null && isNearEndOfList(position)) mNearEndListener.onNearEnd(); View v = super.getView(position, convertView, parent); if (getItemViewType(position) == TYPE_EVEN) v.setBackgroundColor(getResources().getColor(R.color.colorAccent)); return v; } }; // show the irr view at the 40th position (index starts at 0) // don't pass a rule engine here but use the default implementation provided by the irr itself irrDecoAdapter = new IrrAdapterDecorator(this, myAdapter, 39, R.layout.standard_irr_layout); lv.setAdapter(irrDecoAdapter); engine = (DefaultRuleEngine) irrDecoAdapter.getUnderlyingRuleEngine(); // change demo description ((TextView) findViewById(R.id.intro)).setText(R.string.default_engine_async_list_intro); }
@Override public void onLoadFinished(Loader<List<String>> loader, List<String> data) { Toast.makeText( this, (data.isEmpty() ? "Reached end of list" : "More elements loaded"), Toast.LENGTH_SHORT) .show(); mEntries.addAll(data); irrDecoAdapter.notifyDataSetChanged(true); }
@Override void evaluateRules(boolean onlyDump) { if (!onlyDump) irrDecoAdapter.notifyRuleEngineStateChanged(); super.evaluateRules(onlyDump); }