您的位置:首页 > 其它

选择

2016-04-05 21:31 246 查看
package com.bwie.test;

import java.util.ArrayList;

import com.bwie.fragment.Fragment1;
import com.bwie.fragment.Fragment2;
import com.bwie.fragment.Fragment3;
import com.bwie.fragment.Fragment4;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.RadioButton;

public class MainActivity extends FragmentActivity implements OnClickListener{

private ViewPager viewPager;
private ArrayList<RadioButton> rbList=new ArrayList<RadioButton>();
private ArrayList<View> viewList=new ArrayList<View>();
private RadioButton rb_btn1;
private RadioButton rb_btn2;
private RadioButton rb_btn3;
private RadioButton rb_btn4;
private View view1;
private View view2;
private View view3;
private View view4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

@Override
public int getCount() {
return 1;
}

@Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position) {
case 0:
fragment=new Fragment1();
break;
case 1:
fragment=new Fragment2();
break;
case 2:
fragment=new Fragment3();
break;
case 3:
fragment=new Fragment4();
break;
default:
break;
}
return fragment;
}
});
//设置监听
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
for (int i = 0; i < rbList.size(); i++) {
if(i==position){
rbList.get(i).setTextColor(Color.GREEN);
viewList.get(i).setBackgroundColor(Color.GREEN);
}else{
rbList.get(i).setTextColor(Color.BLACK);
viewList.get(i).setBackgroundColor(Color.WHITE);
}
}
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
});
}
/**
* 初始化
*/
private void initView() {
viewPager=(ViewPager) findViewById(R.id.viewpager);
rb_btn1=(RadioButton)findViewById(R.id.rb_btn1);
rb_btn2=(RadioButton)findViewById(R.id.rb_btn2);
rb_btn3=(RadioButton)findViewById(R.id.rb_btn3);
rb_btn4=(RadioButton)findViewById(R.id.rb_btn4);
view1=(View)findViewById(R.id.view1);
view2=(View)findViewById(R.id.view2);
view3=(View)findViewById(R.id.view3);
view4=(View)findViewById(R.id.view4);
rbList.add(rb_btn1);
rbList.add(rb_btn2);
rbList.add(rb_btn3);
rbList.add(rb_btn4);
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
for (int i = 0; i < rbList.size(); i++) {
rbList.get(i).setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rb_btn1:
viewPager.setCurrentItem(0);
break;
case R.id.rb_btn2:
viewPager.setCurrentItem(1);
break;
case R.id.rb_btn3:
viewPager.setCurrentItem(2);
break;
case R.id.rb_btn4:
viewPager.setCurrentItem(3);
break;
default:
break;
}

}
}

package com.bwie.bean;

import java.util.ArrayList;
import java.util.LinkedList;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("oschina")
public class Bean {
public String catalog;
public String newsCount;
@XStreamAlias("newslist")
public NewsList newslist;
public String pagesize;
public class NewsList{
@XStreamImplicit(itemFieldName="news")
public LinkedList<Newss> news;
}
public class Newss{
public String id;
public String title;
public String body;
public String commentCount;
public String author;
public String authorid;
public String pubDate;
public String url;
public NewsType newstype;
}
public class NewsType{
public String type;
public String authoruid2;
public String eventurl;
public String attachment;
}
}

package com.bwie.bean;

import java.util.ArrayList;
import java.util.LinkedList;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("oschina")
public class Bean2 {
public String pagesize;
public Blogs blogs;
public class Blogs{
@XStreamImplicit(itemFieldName="blog")
public LinkedList<Blogss> blog;
}
public class Blogss{
public String id;
public String title;
public String body;
public String url;
public String pubDate;
public String authoruid;
public String authorname;
public String commentCount;
public String documentType;
}
}

package com.bwie.fragment;

import java.util.ArrayList;
import java.util.LinkedList;

import com.bwie.adapter.MyAdapter;
import com.bwie.bean.Bean;
import com.bwie.bean.Bean.Newss;
import com.bwie.test.MainActivity;
import com.bwie.test.R;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment1 extends Fragment {

private View view;
private PullToRefreshListView pull_refresh_list;
private MyAdapter adapter;
private int index=1;
private MainActivity mainActivity;
private ListView listView;
protected LinkedList<Newss> list;
private boolean flag=true;

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag1, container, false);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
pull_refresh_list=(PullToRefreshListView)view.findViewById(R.id.lv_view);
listView = pull_refresh_list.getRefreshableView();
pull_refresh_list.setMode(Mode.PULL_FROM_START);
pull_refresh_list.getLoadingLayoutProxy(false, true).setPullLabel(
"上拉刷新...");
pull_refresh_list.getLoadingLayoutProxy(false, true).setReleaseLabel(
"松开加载...");
pull_refresh_list.getLoadingLayoutProxy(false, true)
.setRefreshingLabel("正在加载...");
//设置上拉刷新文本
pull_refresh_list.getLoadingLayoutProxy(true, false).setPullLabel(
"下拉刷新...");
pull_refresh_list.getLoadingLayoutProxy(true, false).setReleaseLabel(
"放开刷新...");
pull_refresh_list.getLoadingLayoutProxy(true, false)
.setRefreshingLabel("正在加载...");
mainActivity = (MainActivity) getActivity();
// 下拉刷新监听
// Set a listener to be invoked when the list should be refreshed.下拉刷新处理
pull_refresh_list
.setOnRefreshListener(new OnRefreshListener<ListView>() { // 2
@Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
flag=false;
index++;
String label = DateUtils.formatDateTime(
getActivity(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);// 3.刷新界面处理代理,显示新的时间
new GetDataTask().execute(); // 4.执行自定义AsyncTask,进行数据的请求刷新
}

});

pull_refresh_list.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

@Override
public void onLastItemVisible() {
flag=true;
index++;
new GetDataTask().execute();
}
});
getInfo();
super.onActivityCreated(savedInstanceState);
}

private void getInfo() {
HttpUtils utils=new HttpUtils();
utils.send(HttpMethod.GET,"http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="+index+"&pageSize=10",new RequestCallBack<String>() {

@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(mainActivity, "请求失败",0).show();
}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
String xml=arg0.result;
XStream stream=new XStream(new DomDriver());
stream.processAnnotations(Bean.class);
Bean bean = (Bean) stream.fromXML(xml);
if(flag){
if(adapter==null){
list = bean.newslist.news;
adapter=new MyAdapter(mainActivity, list);
listView.setAdapter(adapter);
pull_refresh_list.onRefreshComplete();
}else{
// list.addAll(bean.newslist.news);
list.addAll(bean.newslist.news);
adapter.notifyDataSetChanged();
pull_refresh_list.onRefreshComplete();
}
}else{
if(adapter==null){
list = bean.newslist.news;
adapter=new MyAdapter(mainActivity, list);
listView.setAdapter(adapter);
pull_refresh_list.onRefreshComplete();
}else{
// list.addAll(bean.newslist.news);
LinkedList<Newss> list2 = bean.newslist.news;
for (Newss newss : list2) {
list.addFirst(newss);
}
adapter.notifyDataSetChanged();
pull_refresh_list.onRefreshComplete();
}
}
}
});
}

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

@Override
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String[] result) {
getInfo();
super.onPostExecute(result);
}
}
@Override
public void onStop() {
super.onStop();
if (adapter!=null) {
adapter=null;
}
}
@Override
public void onDestroy() {
super.onDestroy();

}
}

package com.bwie.fragment;

import java.util.ArrayList;
import java.util.LinkedList;

import com.bwie.adapter.MyAdapter;
import com.bwie.bean.Bean;
import com.bwie.bean.Bean.Newss;
import com.bwie.test.MainActivity;
import com.bwie.test.R;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment2 extends Fragment {

private View view;
private PullToRefreshListView pull_refresh_list;
private MyAdapter adapter;
private int index=1;
private MainActivity mainActivity;
private ListView listView;
protected ArrayList<Newss> list;

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag1, container, false);
pull_refresh_list=(PullToRefreshListView)view.findViewById(R.id.lv_view);
listView = pull_refresh_list.getRefreshableView();
getInfo();
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
pull_refresh_list.setMode(Mode.BOTH);
pull_refresh_list.getLoadingLayoutProxy(false, true).setPullLabel(
"上拉刷新...");
pull_refresh_list.getLoadingLayoutProxy(false, true).setReleaseLabel(
"松开加载...");
pull_refresh_list.getLoadingLayoutProxy(false, true)
.setRefreshingLabel("正在加载...");
// 设置上拉刷新文本
pull_refresh_list.getLoadingLayoutProxy(true, false).setPullLabel(
"下拉刷新...");
pull_refresh_list.getLoadingLayoutProxy(true, false).setReleaseLabel(
"放开刷新...");
pull_refresh_list.getLoadingLayoutProxy(true, false)
.setRefreshingLabel("正在加载...");
mainActivity = (MainActivity) getActivity();
// 下拉刷新监听
// Set a listener to be invoked when the list should be refreshed.下拉刷新处理
pull_refresh_list
.setOnRefreshListener(new OnRefreshListener<ListView>() { // 2
@Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
index++;
String label = DateUtils.formatDateTime(
getActivity(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);// 3.刷新界面处理代理,显示新的时间
new GetDataTask().execute(); // 4.执行自定义AsyncTask,进行数据的请求刷新
}
});
super.onActivityCreated(savedInstanceState);
}

private void getInfo() {
HttpUtils utils=new HttpUtils();
utils.send(HttpMethod.GET,"http://www.oschina.net/action/api/news_list?catalog=4&pageIndex="+index+"&pageSize=10&show=week",new RequestCallBack<String>() {

@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(mainActivity, "请求失败",0).show();
}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
String xml=arg0.result;
XStream stream=new XStream(new DomDriver());
stream.processAnnotations(Bean.class);
Bean bean = (Bean) stream.fromXML(xml);
if(adapter==null){
// list = bean.newslist.news;
// adapter=new MyAdapter(mainActivity, list);
listView.setAdapter(adapter);
pull_refresh_list.onRefreshComplete();
}else{
list.addAll(bean.newslist.news);
adapter.notifyDataSetChanged();
pull_refresh_list.onRefreshComplete();
}
}
});
}

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

@Override
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String[] result) {
getInfo();
super.onPostExecute(result);
}
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (adapter!=null) {
adapter=null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (adapter!=null) {
adapter=null;
}
}
}

package com.bwie.fragment;

import java.util.ArrayList;

import com.bwie.adapter.MyAdapter;
import com.bwie.adapter.MyAdapter2;
import com.bwie.bean.Bean;
import com.bwie.bean.Bean.Newss;
import com.bwie.bean.Bean2;
import com.bwie.bean.Bean2.Blogs;
import com.bwie.bean.Bean2.Blogss;
import com.bwie.test.MainActivity;
import com.bwie.test.R;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment3 extends Fragment {

private View view;
private PullToRefreshListView pull_refresh_list;
private MyAdapter2 adapter;
private int index=1;
private MainActivity mainActivity;
private ListView listView;
protected ArrayList<Blogss> list;

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag1, container, false);
pull_refresh_list=(PullToRefreshListView)view.findViewById(R.id.lv_view);
listView = pull_refresh_list.getRefreshableView();
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
pull_refresh_list.setMode(Mode.BOTH);
pull_refresh_list.getLoadingLayoutProxy(false, true).setPullLabel(
"上拉刷新...");
pull_refresh_list.getLoadingLayoutProxy(false, true).setReleaseLabel(
"松开加载...");
pull_refresh_list.getLoadingLayoutProxy(false, true)
.setRefreshingLabel("正在加载...");
// 设置上拉刷新文本
pull_refresh_list.getLoadingLayoutProxy(true, false).setPullLabel(
"下拉刷新...");
pull_refresh_list.getLoadingLayoutProxy(true, false).setReleaseLabel(
"放开刷新...");
pull_refresh_list.getLoadingLayoutProxy(true, false)
.setRefreshingLabel("正在加载...");
mainActivity = (MainActivity) getActivity();
// 下拉刷新监听
// Set a listener to be invoked when the list should be refreshed.下拉刷新处理
pull_refresh_list
.setOnRefreshListener(new OnRefreshListener<ListView>() { // 2
@Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
index++;
String label = DateUtils.formatDateTime(
getActivity(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);// 3.刷新界面处理代理,显示新的时间
new GetDataTask().execute(); // 4.执行自定义AsyncTask,进行数据的请求刷新
}
});
getInfo();
super.onActivityCreated(savedInstanceState);
}

private void getInfo() {
HttpUtils utils=new HttpUtils();
utils.send(HttpMethod.GET,"http://www.oschina.net/action/api/blog_list?type=latest&pageIndex="+index+"&pageSize=10",new RequestCallBack<String>() {

@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(mainActivity, "请求失败",0).show();
}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
String xml=arg0.result;
XStream stream=new XStream(new DomDriver());
stream.processAnnotations(Bean2.class);
Bean2 bean = (Bean2) stream.fromXML(xml);
if(adapter==null){
// list = bean.blogs.blog;
adapter=new MyAdapter2(list, mainActivity);
listView.setAdapter(adapter);
pull_refresh_list.onRefreshComplete();
}else{
list.addAll(bean.blogs.blog);
adapter.notifyDataSetChanged();
pull_refresh_list.onRefreshComplete();
}
}
});
}

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

@Override
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String[] result) {
getInfo();
super.onPostExecute(result);
}
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (adapter!=null) {
adapter=null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (adapter!=null) {
adapter=null;
}
}
}

package com.bwie.fragment;

import java.util.ArrayList;

import com.bwie.adapter.MyAdapter;
import com.bwie.adapter.MyAdapter2;
import com.bwie.bean.Bean;
import com.bwie.bean.Bean.Newss;
import com.bwie.bean.Bean2;
import com.bwie.bean.Bean2.Blogs;
import com.bwie.bean.Bean2.Blogss;
import com.bwie.test.MainActivity;
import com.bwie.test.R;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment4 extends Fragment {

private View view;
private PullToRefreshListView pull_refresh_list;
private MyAdapter2 adapter;
private int index=1;
private MainActivity mainActivity;
private ListView listView;
protected ArrayList<Blogss> list;

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag1, container, false);
pull_refresh_list=(PullToRefreshListView)view.findViewById(R.id.lv_view);
listView = pull_refresh_list.getRefreshableView();
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
pull_refresh_list.setMode(Mode.BOTH);
pull_refresh_list.getLoadingLayoutProxy(false, true).setPullLabel(
"上拉刷新...");
pull_refresh_list.getLoadingLayoutProxy(false, true).setReleaseLabel(
"松开加载...");
pull_refresh_list.getLoadingLayoutProxy(false, true)
.setRefreshingLabel("正在加载...");
// 设置上拉刷新文本
pull_refresh_list.getLoadingLayoutProxy(true, false).setPullLabel(
"下拉刷新...");
pull_refresh_list.getLoadingLayoutProxy(true, false).setReleaseLabel(
"放开刷新...");
pull_refresh_list.getLoadingLayoutProxy(true, false)
.setRefreshingLabel("正在加载...");
mainActivity = (MainActivity) getActivity();
// 下拉刷新监听
// Set a listener to be invoked when the list should be refreshed.下拉刷新处理
pull_refresh_list
.setOnRefreshListener(new OnRefreshListener<ListView>() { // 2
@Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
index++;
String label = DateUtils.formatDateTime(
getActivity(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);// 3.刷新界面处理代理,显示新的时间
new GetDataTask().execute(); // 4.执行自定义AsyncTask,进行数据的请求刷新
}
});
getInfo();
super.onActivityCreated(savedInstanceState);
}

private void getInfo() {
HttpUtils utils=new HttpUtils();
utils.send(HttpMethod.GET,"http://www.oschina.net/action/api/blog_list?type=recommend&pageIndex="+index+"&pageSize=10",new RequestCallBack<String>() {

@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(mainActivity, "请求失败",0).show();
}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
String xml=arg0.result;
XStream stream=new XStream(new DomDriver());
stream.processAnnotations(Bean2.class);
Bean2 bean = (Bean2) stream.fromXML(xml);
if(adapter==null){
// list = bean.blogs.blog;
adapter=new MyAdapter2(list, mainActivity);
listView.setAdapter(adapter);
pull_refresh_list.onRefreshComplete();
}else{
list.addAll(bean.blogs.blog);
adapter.notifyDataSetChanged();
pull_refresh_list.onRefreshComplete();
}
}
});
}

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

@Override
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String[] result) {
getInfo();
super.onPostExecute(result);
}
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (adapter!=null) {
adapter=null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (adapter!=null) {
adapter=null;
}
}
}

package com.bwie.adapter;

import java.util.ArrayList;
import java.util.LinkedList;

import com.bwie.bean.Bean.Newss;
import com.bwie.test.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

private Context context;
private LinkedList<Newss> list;
private TextView tv_title;
private TextView tv_body;

public MyAdapter(Context context, LinkedList<Newss> list2) {
super();
this.context = context;
this.list = list2;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=View.inflate(context, R.layout.frag1_item,null);
tv_title=(TextView)convertView.findViewById(R.id.tv_title);
tv_body=(TextView)convertView.findViewById(R.id.tv_body);
tv_title.setText(list.get(position).title);
tv_body.setText(list.get(position).body);
return convertView;
}

}

package com.bwie.adapter;

import java.util.ArrayList;

import com.bwie.bean.Bean2.Blogs;
import com.bwie.bean.Bean2.Blogss;
import com.bwie.test.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter2 extends BaseAdapter {

protected ArrayList<Blogss> list;
private Context context;
private TextView tv_title;
private TextView tv_body;

public MyAdapter2(ArrayList<Blogss> list, Context context) {
super();
this.list = list;
this.context = context;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=View.inflate(context, R.layout.frag1_item,null);
tv_title=(TextView)convertView.findViewById(R.id.tv_title);
tv_body=(TextView)convertView.findViewById(R.id.tv_body);
tv_title.setText(list.get(position).title);
tv_body.setText(list.get(position).body);
return convertView;
}

}

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwie.test.MainActivity" >

<RadioGroup
android:id="@+id/rg_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/rb_btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="咨询"
android:textColor="#0f0"/>

<RadioButton
android:id="@+id/rb_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="热点" />

<RadioButton
android:id="@+id/rb_btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="博客" />

<RadioButton
android:id="@+id/rb_btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center_horizontal"
android:text="推荐" />
</RadioGroup>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<View
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:background="#0f0"/>

<View
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_weight="1"
android:gravity="center_horizontal" />

<View
android:id="@+id/view3"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_weight="1"
android:gravity="center_horizontal" />

<View
android:id="@+id/view4"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_weight="1"
android:gravity="center_horizontal" />
</LinearLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

frag1_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="20dp">

<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="世界如此"
android:textStyle="bold"/>

<TextView
android:id="@+id/tv_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="世界如此多娇"/>
</LinearLayout>

frag1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/lv_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: