//主界面
package com.example.mounth_test;
import java.util.ArrayList;
import java.util.List;
import com.example.adapter.FragAdapter;
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.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
public class MainActivity extends FragmentActivity implements OnClickListener,OnPageChangeListener{
//定义控件
private RadioButton radio1,radio2,radio3,radio4;
private ViewPager mViewPager;
private List<Fragment> fragments;
//定义单个fragment控件
private ContentFragment01 mContentFragment01;
private ContentFragment02 mContentFragment02;
private ContentFragment03 mContentFragment03;
private ContentFragment04 mContentFragment04;
private FragAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载fragment控件
intodata();
//获取控件id
inteview();
//设置默认值
mViewPager.setCurrentItem(0);
select(0);
}
private void intodata() {
// TODO Auto-generated method stub
fragments=new ArrayList<Fragment>();
fragments.add(new ContentFragment01());
fragments.add(new ContentFragment02());
fragments.add(new ContentFragment03());
fragments.add(new ContentFragment04());
}
//该方法,,获取控件id
private void inteview() {
// TODO Auto-generated method stub
mViewPager=(ViewPager) findViewById(R.id.myviewpager);
radio1=(RadioButton) findViewById(R.id.radio_btn1);
radio2=(RadioButton) findViewById(R.id.radio_btn2);
radio3=(RadioButton) findViewById(R.id.radio_btn3);
radio4=(RadioButton) findViewById(R.id.radio_btn4);
radio1.setOnClickListener(this);
radio2.setOnClickListener(this);
radio3.setOnClickListener(this);
radio4.setOnClickListener(this);
mViewPager.setOnPageChangeListener(this);
adapter=new FragAdapter(getSupportFragmentManager(),fragments);
//添加到适配器上
mViewPager.setAdapter(adapter);
}
/**
*
* 点击titile 监听
* @param v
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.radio_btn1:
mViewPager.setCurrentItem(0);
break;
case R.id.radio_btn2:
mViewPager.setCurrentItem(1);
break;
case R.id.radio_btn3:
mViewPager.setCurrentItem(2);
break;
case R.id.radio_btn4:
mViewPager.setCurrentItem(3);
break;
}
}
//viewpager页面滑动监听,,滑动页面,,radiobutton的字体颜色变为绿色
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
select(arg0);
}
//滑动页面,改变字体颜色
private void select(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
radio1.setTextColor(Color.GREEN);
radio2.setTextColor(Color.BLACK);
radio3.setTextColor(Color.BLACK);
radio4.setTextColor(Color.BLACK);
break;
case 1:
radio1.setTextColor(Color.BLACK);
radio2.setTextColor(Color.GREEN);
radio3.setTextColor(Color.BLACK);
radio4.setTextColor(Color.BLACK);
break;
case 2:
radio1.setTextColor(Color.BLACK);
radio2.setTextColor(Color.BLACK);
radio3.setTextColor(Color.GREEN);
radio4.setTextColor(Color.BLACK);
break;
case 3:
radio1.setTextColor(Color.BLACK);
radio2.setTextColor(Color.BLACK);
radio3.setTextColor(Color.BLACK);
radio4.setTextColor(Color.GREEN);
break;
}
}
}
//单个fragment控件
package com.example.mounth_test;
import java.util.List;
import com.example.adapter.MyAdapter;
import com.example.vo.New;
import com.example.vo.News;
import com.example.vo.NewsObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.db.sqlite.CursorUtils.FindCacheSequence;
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 android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
public class ContentFragment01 extends Fragment {
//定义控件
private PullToRefreshListView mPullRefreshListView;
private List<News> list_new;
//定义适配器
private MyAdapter adapter;
int index=1;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=LayoutInflater.from(getActivity()).inflate(R.layout.contentfragment, null);
//获取控件id
mPullRefreshListView=(PullToRefreshListView) view.findViewById(R.id.mylistview);
//调用联网方法
getinfo();
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
String label = DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);//3.刷新界面处理代理,显示新的时间
//定义联网请求方法
getinfo();
}
});
//5.设置上拉加载处理
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
getinfo();
}
});
return view;
}
/**
*
*
*联网请求。。获取网络上的xml文件,解析并展示
*
*/
private void getinfo() {
// TODO Auto-generated method stub
HttpUtils httpUtils=new HttpUtils();
//定义接口
String url="http://www.oschina.net/action/api/news_list";
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
String xmldata=arg0.result;
XStream xStream=new XStream();
xStream.processAnnotations(NewsObject.class);
NewsObject newsObject=(NewsObject) xStream.fromXML(xmldata);
list_new=newsObject.getNewslist().getNews();
adapter=new MyAdapter(getActivity(),list_new);
mPullRefreshListView.setAdapter(adapter);
}
});
// adapter.notifyDataSetChanged();
index= index+1;
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
}
}
//布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</RelativeLayout>
//配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mounth_test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="PtR ListView"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>