Android Development----Pullrefreshlibrary Pull-up loading related code

Source: Internet
Author: User

Mainactivity

public class Mainactivity extends Activity {

Private String URL = "Http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml";
Private list<xmlarray> List;
Private ListView LV;
Define a variable to identify whether to exit
Achieve two clicks to exit
private static Boolean isexit = false;
private static final String TAG = MainActivity.class.getSimpleName ();
private static Handler Mhandler = new Handler () {

@Override
Public void Handlemessage (Message msg) {
super.handlemessage (msg);
isexit = false;
        }
    };

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
LV = (ListView) Findviewbyid (R.id.lv_main);
Httputils httputils = new Httputils ();
Httputils.send (httpmethod.get, URL, new requestcallback<string> () {

@Override
public void OnFailure (HttpException arg0, String arg1) {
TODO auto-generated Method Stub
LOG.I ("TAG", "request Failed");
}

@Override
public void onsuccess (responseinfo<string> arg0) {
TODO auto-generated Method Stub
LOG.I ("TAG", arg0.result.toString ());
XStream stream = new XStream ();
Stream.processannotations (Xmlroot.class);
XmlRoot xroot = (xmlroot) stream.fromxml (Arg0.result);
List = Xroot.getresult (). GetItem ();
LOG.I ("TAG", list.tostring ());
Lv.setadapter (New XMLAdapter (list, Getapplicationcontext ()));
Lv.setonitemclicklistener (New Onitemclicklistener () {

@Override
public void Onitemclick (adapterview<?> parent, view view,
int position, long ID) {
TODO auto-generated Method Stub
Intent Intent = new Intent (Mainactivity.this,
Itemactivitytwo.class);
Intent.putextra ("Lsid", list.get (position). GetId ());
StartActivity (Intent);
}
});
}
});

}

//Override onkeydown Method
@Override
Public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = keyevent.keycode_back) {
exit ();
return true;
        }
return Super.onkeydown (KeyCode, event);
    }

private void Exit () {
if (!isexit) {
isexit = true;
Toast.maketext (Getapplicationcontext (), "Press the back key again to exit the program",
toast.length_short). Show ();
//Use handler delay to send Change status information
//Time 2s
mhandler.sendemptymessagedelayed (0, +);
} else {

log.e (TAG, "Exit Application");

this.finish ();
        }
}

Itemactivity class

public class Itemactivitytwo extends Activity {
private int index = 0;
Private String xmlid;
Private list<jsonarray> List;
Private GridView GridView;
Private Jsonadapter adapter;
Private Pulltorefreshgridview Mpullrefreshgridview;

@SuppressWarnings ({"Unchecked", "Rawtypes"})
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.itemactivity);

Intent Intent = Getintent ();
xmlid = Intent.getstringextra ("Lsid");
//Get Pulltorefreshgridview
Mpullrefreshgridview = (pulltorefreshgridview) Findviewbyid (R.id.pull_refresh_grid);
GridView = Mpullrefreshgridview.getrefreshableview ();
gethttp ();
//Implement pull-up refresh with pull-down loading
Mpullrefreshgridview.setonrefreshlistener (New OnRefreshListener2 () {

@SuppressWarnings ("Rawtypes")
@Override
Public void Onpulldowntorefresh (Pulltorefreshbase refreshview) {
gethttp ();
            }

@SuppressWarnings ("Rawtypes")
@Override
Public void Onpulluptorefresh (Pulltorefreshbase refreshview) {
gethttp ();
            }

        });

    }

private void Gethttp () {
TODO auto-generated Method Stub
String url = "Http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="
+ xmlid + "&pn=5&rn=" + index + "";
Httputils httputils = new Httputils ();
Httputils.send (httpmethod.post, URL, new requestcallback<string> () {

@Override
public void OnFailure (HttpException arg0, String arg1) {
TODO auto-generated Method Stub
LOG.I ("TAG", "request Failed");
}

@Override
public void onsuccess (responseinfo<string> arg0) {
TODO auto-generated Method Stub
Gson parsing
Gson Gson = new Gson ();
Jsonresult Fromjson = Gson.fromjson (Arg0.result,
Jsonresult.class);
List = Fromjson.getresult (). GetData ();
LOG.I ("TAG", list.tostring ());
adapter = new Jsonadapter (list, getapplicationcontext ());
Gridview.setadapter (adapter);
index = index++;
//Adapter Refresh
adapter.notifydatasetchanged ();
Mpullrefreshgridview.onrefreshcomplete ();
}
});
}

}

Customizing the application implementation to load a circular picture

public class Myapliation extends application {

@Override
public void OnCreate () {
TODO auto-generated Method Stub
Super.oncreate ();
System of
Imageloaderconfiguration configuration = imageloaderconfiguration
. Createdefault (Getapplicationcontext ());
Imageloader.getinstance (). init (configuration);

}

public static Displayimageoptions GetOptions () {

Displayimageoptions displayimageoptions = new Displayimageoptions.builder ()
. Displayer (new Roundedbitmapdisplayer). Build ();

return displayimageoptions;

}
}

Adapter Loading Pictures

}

Imageloader.getinstance (). DisplayImage (List.get (position). Getimg (),
Vh.iamge, Myapliation.getoptions ());
Bitmap Loading pictures
Bitmaputils bitmaputils = new Bitmaputils (context);
Bitmaputils.display (Vh.iamge, list.get (position). GETIMG ());

Android Development----Pullrefreshlibrary Pull-up loading related code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.