关闭

viewpager+fragment

标签: viewPager+fragment
88人阅读 评论(0) 收藏 举报
分类:

activity中的代码


package com.bwei.test;


import com.bwei.adapter.MyViewPageAdapter;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
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.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;




public class MainActivity extends FragmentActivity {


    private ViewPager vp;
private RadioGroup rg;
private RadioButton rbt1;
private RadioButton rbt2;
private RadioButton rbt3;
private RadioButton rbt4;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        vp.setAdapter(new MyViewPageAdapter(getSupportFragmentManager()));
        vp.setOffscreenPageLimit(4);
        radioGroupListener();
        vpListener();
        
    }


 private void vpListener() {
vp.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
rbt1.setTextColor(Color.GREEN);
rbt2.setTextColor(Color.BLACK);
rbt3.setTextColor(Color.BLACK);
rbt4.setTextColor(Color.BLACK);
break;


case 1:
rbt2.setTextColor(Color.GREEN);
rbt1.setTextColor(Color.BLACK);
rbt3.setTextColor(Color.BLACK);
rbt4.setTextColor(Color.BLACK);
break;


case 2:
rbt3.setTextColor(Color.GREEN);
rbt2.setTextColor(Color.BLACK);
rbt1.setTextColor(Color.BLACK);
rbt4.setTextColor(Color.BLACK);
break;


case 3:
rbt4.setTextColor(Color.GREEN);
rbt2.setTextColor(Color.BLACK);
rbt3.setTextColor(Color.BLACK);
rbt1.setTextColor(Color.BLACK);
break;



}

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}
});

}


private void radioGroupListener() {
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.rbt1:
vp.setCurrentItem(0);
break;
case R.id.rbt2:
vp.setCurrentItem(1);
break;


case R.id.rbt3:
vp.setCurrentItem(2);
break;


case R.id.rbt4:
vp.setCurrentItem(3);
break;


}
}
});

}


public void init(){
vp = (ViewPager) findViewById(R.id.vp);
     rg = (RadioGroup) findViewById(R.id.rg);
     rbt1 = (RadioButton) findViewById(R.id.rbt1);
     rbt2 = (RadioButton) findViewById(R.id.rbt2);
     rbt3 = (RadioButton) findViewById(R.id.rbt3);
     rbt4 = (RadioButton) findViewById(R.id.rbt4);
 }
 
}


两个适配器viewPagerAdapter    listviewadapter

viewPagerAdapter    ==========================================

package com.bwei.adapter;


import com.bwei.fragment.MyFragment;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


public class MyViewPageAdapter extends FragmentPagerAdapter {


public MyViewPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}


@Override
public Fragment getItem(int arg0) {
MyFragment fragment=new MyFragment(arg0);
return fragment;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}


}

listviewadapter=======================================

package com.bwei.adapter;


import java.util.List;


import com.bwei.bean.MyNews;
import com.bwei.test.R;


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


public class MyLvAdapter extends BaseAdapter {
private Context context;
private List<MyNews> list;
public MyLvAdapter(Context context,List<MyNews> list) {
super();
// TODO Auto-generated constructor stub
this.context=context;
this.list=list;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}


@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}


@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder vh;
if(arg1==null){
vh=new ViewHolder();
arg1=View.inflate(context, R.layout.item, null);
vh.tv_title=(TextView) arg1.findViewById(R.id.tv_title);
vh.tv_neirong=(TextView) arg1.findViewById(R.id.tv_neirong);
vh.tv_name=(TextView) arg1.findViewById(R.id.tv_name);
vh.tv_time=(TextView) arg1.findViewById(R.id.tv_time);
vh.tv_zan=(TextView) arg1.findViewById(R.id.tv_zan);
arg1.setTag(vh);
}else{
vh=(ViewHolder) arg1.getTag();
}
vh.tv_title.setText(list.get(arg0).getTitle());
vh.tv_neirong.setText(list.get(arg0).getBody());
vh.tv_name.setText(list.get(arg0).getAuthor());
vh.tv_time.setText(list.get(arg0).getPubDate());
vh.tv_zan.setText(list.get(arg0).getCommentCount());
return arg1;
}
class ViewHolder{
TextView tv_title;
TextView tv_neirong;
TextView tv_name;
TextView tv_time;
TextView tv_zan;
}
public void update(List<MyNews> list){
this.list=list;
this.notifyDataSetChanged();
}
}

bean类

package com.bwei.bean;


public class MyNews {
public String title;
public String body;
public String author;
public String pubDate;
public String commentCount;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getCommentCount() {
return commentCount;
}
public void setCommentCount(String commentCount) {
this.commentCount = commentCount;
}
@Override
public String toString() {
return "MyNews [title=" + title + ", body=" + body + ", author=" + author
+ ", pubDate=" + pubDate + ", commentCount=" + commentCount + "]";
}


}

fragment============================

package com.bwei.fragment;


import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;


import com.bwei.adapter.MyLvAdapter;
import com.bwei.bean.MyNews;
import com.bwei.test.R;
import com.bwei.zidingyiview.XListView;
import com.bwei.zidingyiview.XListView.IXListViewListener;
import com.google.gson.Gson;
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 android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MyFragment extends Fragment implements IXListViewListener {
private int i;
public int OK;
private int page = 1;
private int pageSize = 10;
public List<MyNews> list = new ArrayList<MyNews>();
public List<MyNews> l = new ArrayList<MyNews>();
private MyLvAdapter adapter;
private String[] path = {
"http://www.oschina.net/action/api/news_list?catalog=1&&pagelndex="
+ page + "&&pageSize=" + pageSize,
"http://www.oschina.net/action/api/news_list?catalog=4&&pagelndex="
+ page + "&&pageSize=" + pageSize + "&&show=week",
"http://www.oschina.net/action/api/blog_list?type=latest&&pageIndex="
+ page + "&&pageSize=" + pageSize,
"http://www.oschina.net/action/api/blog_list?type=recommend&&pageIndex="
+ page + "&&pageSize=" + pageSize, };
private XListView xlv;


public MyFragment(int i) {
super();
// TODO Auto-generated constructor stub
this.i = i;
l.clear();
System.out.println("调用构造方法");
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, null);
System.out.println("开始运行");
xlv = (XListView) view.findViewById(R.id.xlv);
xlv.setXListViewListener(this);
xlv.setPullLoadEnable(true);
xlv.setPullRefreshEnable(true);
getData();
return view;
}


private void getData() {
if (i == 0) {
http(path[0]);
System.out.println("确定接口1");
} else if (i == 1) {
http(path[1]);
System.out.println("确定接口2");
} else if (i == 2) {
http(path[2]);
System.out.println("确定接口3");
} else {
http(path[3]);
System.out.println("确定接口4");
}
}


public void http(final String path1) {
System.out.println("获取网上数据");
new Thread(new Runnable() {


@Override
public void run() {
// TODO Auto-generated method stub
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, path1,
new RequestCallBack<String>() {


@Override
public void onFailure(HttpException arg0,
String arg1) {
// TODO Auto-generated method stub


}


@Override
public void onSuccess(ResponseInfo<String> arg0) {
String str = arg0.result;
System.out.println("获取到数据=" + str);
Pull(str);

System.out.println("看看 有没有数据");
  
// adapter = new MyLvAdapter(getActivity(), l);
// xlv.setAdapter(adapter);
list.addAll(l);
setAdapter(list);


}
});
}
}).start();
}


public void Pull(String ss) {
XmlPullParser xpp = Xml.newPullParser();
try {
System.out.println("开始解析");
InputStream is = new ByteArrayInputStream(ss.getBytes());
// InputStreamReader is=new InputStreamReader(new
// FileInputStream(ss));
xpp.setInput(is, "utf-8");
int event = xpp.getEventType();


MyNews news = null;
int i = 0;
boolean flag = true;
while (flag) {
String name = xpp.getName();
switch (event) {
case XmlPullParser.START_DOCUMENT:

break;
case XmlPullParser.START_TAG:

if (name.equals("news")) {
news = new MyNews();


}
if(name.equals("blog")){
news = new MyNews();
}
if (name.equals("title")) {
news.setTitle(xpp.nextText());
}
if (name.equals("body")) {
news.setBody(xpp.nextText());


}
if (name.equals("commentCount")) {
news.setCommentCount(xpp.nextText());


}
if (name.equals("author")) {
news.setAuthor(xpp.nextText());


}
if (name.equals("pubDate")) {
news.setPubDate(xpp.nextText());


}
break; 


case XmlPullParser.END_TAG:


if (name.equals("news")) {
l.add(news);
}
if (name.equals("blog")) {
l.add(news);
}


break;
case XmlPullParser.END_DOCUMENT:
flag = false;
break;
}
event = xpp.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public void setAdapter(List<MyNews> lll) {
if (adapter == null) {


adapter = new MyLvAdapter(getActivity(), lll);
xlv.setAdapter(adapter);
} else {
adapter.update(lll);
}



@Override
public void onRefresh() {
page++;
pageSize+=5;
list.clear();
getData();
load();
}


@Override
public void onLoadMore() {
// TODO Auto-generated method stub
page++;
pageSize+=5;
getData();
load();
}


public void load() {
xlv.stopLoadMore();
xlv.stopRefresh();
xlv.setRefreshTime("刚刚");
}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3014次
    • 积分:223
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档