Vi diamo la risposta a questo enigma, almeno lo speriamo. Se hai domande puoi lasciarlo nella sezione domande e ti aiuteremo senza esitazione
Soluzione:
Non so cosa sia questo ActionBarActivity
che si sta estendendo, ma io l'ho fatto funzionare bene usando una FragmentActivity
public class ActivityMain extends FragmentActivity implements OnRefreshListener {
private SwipeRefreshLayout mSwipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);
mSwipeRefreshLayout.setOnRefreshListener(this);
super.onCreate(savedInstanceState);
}
@Override
public void onRefresh() {
Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
}, 2000);
}
}
Vale la pena sottolineare che ho copiato il tuo layout xml esattamente com'è
In termini di personalizzazione, non c'è molto che si possa fare, a parte cambiare il colore della barra colorata chiamando setColorScheme(int colorResId, int colorResId, int colorResId, int colorResId);
ad esempio
#0099CC
#9933CC
#669900
#FF8800
mSwipeRefreshLayout.setColorScheme(R.color.blue, R.color.purple, R.color.green, R.color.orange);
È un'aggiunta piuttosto deludente. La sensibilità del refresh è piuttosto alta e non c'è alcuna impostazione per modificarla.
Modifica
Ho scritto questo quando questa classe (e la classe ActionBarActivity
) erano appena state aggiunte all'sdk. Pertanto, alcune cose sono cambiate rispetto a quando ho scritto questa risposta. Inoltre, il tipo di attività che si utilizza non dovrebbe influire su questa soluzione.
setColorScheme
è ora deprecato, setColorSchemeResources(int... colorResIds)
dovrebbe essere usato al suo posto. (si possono inserire tutti gli id colore che si vogliono).
setDistanceToTriggerSync(int distance)
può essere usato anche per impostare quanto in basso deve scorrere l'utente per attivare un aggiornamento.
Si consiglia di consultare la documentazione ufficiale per vedere cos'altro ha da offrire questa classe.
MainActivity.java
public class MainActivity extends ActionBarActivity {
TextView textView;
private SwipeRefreshLayout mSwipeRefreshLayout;
static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.scrollTextView);
// /You will setup the action bar with pull to refresh layout
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);
mSwipeRefreshLayout.setColorScheme(R.color.blue,
R.color.green, R.color.orange, R.color.purple);
mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
Log.e(getClass().getSimpleName(), "refresh");
new GetLinks().execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class GetLinks extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//Here you can update the view
textView.setText(textView.getText().toString()+"--New Content Added" + ++count);
// Notify swipeRefreshLayout that the refresh has finished
mSwipeRefreshLayout.setRefreshing(false);
}
}
}
activity_main.xml
colori.xml
- #FF33B5E5
- #FFAA66CC
- #FF99CC00
- #FFFFBB33
È possibile seguire semplicemente il metodo seguente (sto usando swipelayout su una lista espandibile)
Test.xml
ListHeader.xml
ChildItem.xml
colori utilizzati
#8ec549
#c1f57f
#f7ffed
#d6d4d4
Mainactivity.java
swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe2);
swipeView.setColorSchemeResources(R.color.pDarkGreen, R.color.Gray, R.color.Yellow, R.color.pFullLightGreen);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeView.setRefreshing(false);
}
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
Log.d("Swipe", "Refreshing Number");
}
}, 0);
}
});
È possibile impostare swipeView.setRefreshing(false);
a false o a true in base alle proprie esigenze.
questo meccanismo di scorrimento funzionerà in tutti i livelli API di Android
Se sei d'accordo, puoi lasciare un post su ciò che ti è piaciuto di questa cronaca.