[android]代码库
main
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{
private XListView mXListView;
private List<Bean.ResultBean.ListBean> mListBean = new ArrayList<>();
private String path;
private int i = 1;
Handler mHandler = new Handler(){
private MyBaseAdapter adapter;
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//接受传过来的数据
String s = (String) msg.obj;
//开始解析
Gson mGson = new Gson();
Bean bean = mGson.fromJson(s, Bean.class);
int tag = msg.arg1;
switch (tag){
//下拉刷新
case 1:
mListBean.clear();
mListBean.addAll(bean.getResult().getList());
adapter.notifyDataSetChanged();
mXListView.stopRefresh();
break;
//上拉加载
case 2:
mListBean.addAll(bean.getResult().getList());
adapter.notifyDataSetChanged();
mXListView.stopLoadMore();
break;
//第一次进入界面调用的数据
case 3:
mListBean.addAll(bean.getResult().getList());
adapter = new MyBaseAdapter(MainActivity.this,mListBean);
mXListView.setAdapter(adapter);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件的方法
initView();
getData(3);
//设置xlistview的点击监听
mXListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent it = new Intent(MainActivity.this,SecondActivity.class);
it.putExtra("String",mListBean.get(i).getUrl());
startActivity(it);
}
});
}
private void getData(final int tag) {
//判断tag
if(tag == 1){
//下拉刷新
path = "http://v.juhe.cn/weixin/query?pno="+1+"&ps=15&dtype=&key=0863c5f38bfa7a84bcceb6b629f91a35";
}else if(tag == 2){
path = "http://v.juhe.cn/weixin/query?pno="+(i+=1)+"&ps=15&dtype=&key=0863c5f38bfa7a84bcceb6b629f91a35";
}else if(tag == 3){
path = "http://v.juhe.cn/weixin/query?pno="+1+"&ps=15&dtype=&key=0863c5f38bfa7a84bcceb6b629f91a35";
}
//开始请求数据
HttpUtils mHttpUtils = new HttpUtils();
mHttpUtils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack<String>() {
//请求成功的返回
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String mString = responseInfo.result;
//把返回的数据传给message
Message mMessage = Message.obtain();
mMessage.obj = mString;
mMessage.arg1 = tag;
mHandler.sendMessage(mMessage);
}
@Override
public void onFailure(HttpException e, String s) {
}
});
}
private void initView() {
mXListView = (XListView) findViewById(R.id.xlistview);
//创建xlistview的监听
mXListView.setXListViewListener(this);
//设置加载更多
mXListView.setPullLoadEnable(true);
}
//上拉刷新
@Override
public void onRefresh() {
getData(1);
}
//下拉加载
@Override
public void onLoadMore() {
getData(2);
}
}
////////////////适配器
public class MyBaseAdapter extends BaseAdapter{
private Context mContext;
private List<Bean.ResultBean.ListBean> list;
public MyBaseAdapter(Context mContext, List<Bean.ResultBean.ListBean> list) {
this.mContext = mContext;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder v;
if(view == null){
v = new ViewHolder();
view = View.inflate(mContext, R.layout.item,null);
v.imageView = (ImageView) view.findViewById(R.id.item_imageView);
v.title = (TextView) view.findViewById(R.id.item_title);
v.name = (TextView) view.findViewById(R.id.item_name);
view.setTag(v);
}else{
v = (ViewHolder) view.getTag();
}
new BitmapUtils(mContext).display(v.imageView,list.get(i).getFirstImg());
v.name.setText(list.get(i).getTitle());
v.title.setText(list.get(i).getSource());
return view;
}
static class ViewHolder{
ImageView imageView;
TextView title,name;
}
}
//////////////////////////////////////////second Xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#1B9A16"
/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="3dip"
android:progressDrawable="@drawable/pg"
android:visibility="gone"
/>
<WebView
android:id="@+id/webview1"
android:layout_below="@id/progressBar1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
/////////////////////////////////////////////SecondActivity
public class SecondActivity extends AppCompatActivity {
/* private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mWebView = (WebView) findViewById(R.id.webView);
Intent intent = getIntent();
String titleUrl=intent.getStringExtra("String");
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
mWebView.loadUrl(titleUrl);
}*/
private WebView webView;
private ProgressBar pg1;
private String titleUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_second);
init();
Intent intent = getIntent();
titleUrl = intent.getStringExtra("String");
webView.loadUrl(titleUrl);
}
private void init() {
// TODO 自动生成的方法存根
webView=(WebView) findViewById(R.id.webview1);
pg1=(ProgressBar) findViewById(R.id.progressBar1);
webView.setWebViewClient(new WebViewClient(){
//覆写shouldOverrideUrlLoading实现内部显示网页
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO 自动生成的方法存根
view.loadUrl(url);
return true;
}
});
WebSettings seting=webView.getSettings();
seting.setJavaScriptEnabled(true);//设置webview支持javascript脚本
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO 自动生成的方法存根
if(newProgress==100){
pg1.setVisibility(View.GONE);//加载完网页进度条消失
}
else{
pg1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
pg1.setProgress(newProgress);//设置进度值
}
}
});
}
//设置返回键动作(防止按返回键直接退出程序)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO 自动生成的方法存根
if(keyCode==KeyEvent.KEYCODE_BACK) {
if(webView.canGoBack()) {//当webview不是处于第一页面时,返回上一个页面
webView.goBack();
return true;
}
else {//当webview处于第一页面时,直接退出程序
System.exit(0);
}
}
return super.onKeyDown(keyCode, event);
}
初级程序员
by: 浅唱 发表于:2017-09-12 10:14:24 顶(0) | 踩(0) 回复
很好
回复评论