如何给新闻csdn博客客户端端做评论csdn

一.创建一个名为“新闻客户端”的应用程序,相应的布局文件如下(activity_main.xml)
如图所示:
代码如下:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
& & xmlns:tools=&/tools&
& & android:id=&@+id/activity_main&
& & android:layout_width=&match_parent&
& & android:layout_height=&match_parent&
& & android:paddingBottom=&@dimen/activity_vertical_margin&
& & android:paddingLeft=&@dimen/activity_horizontal_margin&
& & android:paddingRight=&@dimen/activity_horizontal_margin&
& & android:paddingTop=&@dimen/activity_vertical_margin&
& & android:orientation=&vertical&
& & tools:context=&com.example.administrator.news.MainActivity&&
& & &FrameLayout
& & & & android:layout_width=&match_parent&
& & & & android:layout_height=&match_parent&&
& & & & &LinearLayout
& & & & & & android:layout_width=&match_parent&
& & & & & & android:layout_height=&match_parent&
& & & & & & android:id=&@+id/loading&
& & & & & & android:visibility=&invisible&
& & & & & & android:gravity=&center&
& & & & & & android:orientation=&vertical&
& & & & & & &
& & & & & & &ProgressBar
& & & & & & & & android:layout_width=&wrap_content&
& & & & & & & & android:layout_height=&wrap_content& /&
& & & & & & &TextView
& & & & & & & & android:layout_width=&wrap_content&
& & & & & & & & android:layout_height=&wrap_content&
& & & & & & & & android:text=&正在加载。。。& /&
& & & & &/LinearLayout&
& & & & &ListView
& & & & & & android:layout_width=&match_parent&
& & & & & & android:layout_height=&match_parent&
& & & & & & android:id=&@+id/lv_news&&
& & & & &/ListView&
& & &/FrameLayout&
&/LinearLayout&
二.创建ListView Item的布局
如图所示:
代码如下:
&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
& & android:layout_width=&match_parent&
& & android:layout_height=&65dp&&
& & &com.loopj.android.image.SmartImageView
& & & & android:layout_width=&80dp&
& & & & android:layout_height=&60dp&
& & & & android:id=&@+id/siv_con&
& & & & android:layout_alignParentLeft=&true&
& & & & android:layout_marginBottom=&5dp&
& & & & android:layout_marginLeft=&5dp&
& & & & android:layout_marginTop=&5dp&
& & & & android:scaleType=&centerCrop&
& & & & android:src=&@mipmap/ic_launcher&
& & & & /&
& & &TextView
& & & & android:id=&@+id/tv_title&
& & & & android:layout_width=&wrap_content&
& & & & android:layout_height=&wrap_content&
& & & & android:layout_marginLeft=&5dp&
& & & & android:layout_marginTop=&10dp&
& & & & android:layout_toRightOf=&@id/siv_con&
& & & & android:text=&我是标题&
& & & & android:maxLength=&20&
& & & & android:singleLine=&true&
& & & & android:ellipsize=&end&
& & & & android:textColor=&#000000&
& & & & android:textSize=&18sp&
& & & & /&
& & &TextView
& & & & android:id=&@+id/tv_description&
& & & & android:layout_width=&wrap_content&
& & & & android:layout_height=&wrap_content&
& & & & android:layout_below=&@id/tv_title&
& & & & android:layout_marginLeft=&5dp&
& & & & android:layout_marginTop=&5dp&
& & & & android:layout_toRightOf=&@id/siv_con&
& & & & android:text=&我是描述&
& & & & android:maxLength=&16&
& & & & android:singleLine=&true&
& & & & android:ellipsize=&end&
& & & & android:textColor=&#&
& & & & android:textSize=&14sp&
& & & & /&
& & &TextView
& & & & android:id=&@+id/tv_type&
& & & & android:layout_width=&wrap_content&
& & & & android:layout_height=&wrap_content&
& & & & android:layout_alignParentBottom=&true&
& & & & android:layout_alignParentRight=&true&
& & & & android:layout_marginBottom=&5dp&
& & & & android:text=&评论&
& & & & android:textColor=&#&
& & & & android:textSize=&12sp&
& & & & /&
&/RelativeLayout&
三.创建NewsInfo类
在适配ListView的eitem布局时用到此对象
代码如下:
package cn.edu.bzu.
&* Created by lenovo on .
public class NewsInfo {
& & & & private String iconP
& & & & private S
& & & & private S
& & & & public String getIconPath() {
& & & & & & return iconP
& & & & public void setIconPath(String iconPath) {
& & & & & & this.iconPath = iconP
& & & & public String getTitle() {
& & & & & &
& & & & public void setTitle(String title) {
& & & & & & this.title =
& & & & public String getDescription() {
& & & & & &
& & & & public void setDescription(String description) {
& & & & & & this.description =
& & & & public int getType() {
& & & & & &
& & & & public void setType(int type) {
& & & & & & this.type =
& & & & public long getComment() {
& & & & & &
& & & & public void setComment(long comment) {
& & & & & & ment =
四.创建工具类解析xml文件
由于从服务器上获取的是一个XML文件,因此需要使用XmlPulParser对象解析出xml文件中的内容并设置到相应的javabean中。将解析操作的逻辑放在工具类NewsInfoService中,代码如下:
package cn.edu..edu.bzu.XML;
import android.util.X
import org.xmlpull.v1.XmlPullP
import java.io.InputS
import java.util.ArrayL
import java.util.L
import cn.edu.bzu.anew.NewsI
public class NewsInfoService {
& & //解析服务器返回的xml信息,获取所有新闻数据实体
& & public static List&NewsInfo& getNewsInfos(InputStream is){
& & & & //获取XmlPullParser对象
& & & & XmlPullParser parser= Xml.newPullParser();
& & & & try{
& & & & & & parser.setInput(is,&utf_8&);
& & & & & & //获取指针
& & & & & & int type=parser.getEventType();
& & & & & & List&NewsInfo& newsInfos=
& & & & & & NewsInfo newsInfo=
& & & & & & //type不是文档结束
& & & & & & while(type!=XmlPullParser.END_DOCUMENT){
& & & & & & & & switch (type){
& & & & & & & & & & case &XmlPullParser.START_TAG:
& & & & & & & & & & & & //拿到标签名并判断
& & & & & & & & & & & & if(&news&.equals(parser.getName())){
& & & & & & & & & & & & & & newsInfos=new ArrayList&NewsInfo&();
& & & & & & & & & & & & }else if(&newsInfo&.equals(parser.getName())){
& & & & & & & & & & & & & & newsInfo=new NewsInfo();
& & & & & & & & & & & & }else if(&icon&.equals(parser.getName())){
& & & & & & & & & & & & & & //获取解析器当前指向元素的下一个文本节点的值
& & & & & & & & & & & & & & String icon=parser.nextText();
& & & & & & & & & & & & & & newsInfo.setIconPath(icon);
& & & & & & & & & & & & }else if(&title&.equals(parser.getName())){
& & & & & & & & & & & & & & String title=parser.nextText();
& & & & & & & & & & & & & & newsInfo.setTitle(title);
& & & & & & & & & & & & }else if(&content&.equals(parser.getName())){
& & & & & & & & & & & & & & String description=parser.nextText();
& & & & & & & & & & & & & & newsInfo.setDescroption(description);
& & & & & & & & & & & & }else if(&type&.equals(parser.getName())){
& & & & & & & & & & & & & & String newsType=parser.nextText();
& & & & & & & & & & & & & & newsInfo.setType(Integer.parseInt(newsType));
& & & & & & & & & & & & }else if(&comment&.equals(parser.getName())){
& & & & & & & & & & & & & & String comment=parser.nextText();
& & & & & & & & & & & & & & newsInfo.setComment(Long.parseLong(comment));
& & & & & & & & & & & & }
& & & & & & & & & & & &
& & & & & & & & & & case XmlPullParser.END_TAG:
& & & & & & & & & & & & if(&newsInfo&.equals(parser.getName())){
& & & & & & & & & & & & & & newsInfos.add(newsInfo);
& & & & & & & & & & & & & & newsInfo=
& & & & & & & & & & & & }
& & & & & & & & & & & &
& & & & & & & & }
& & & & & & & & type=parser.next();
& & & & & & }
& & & & & & return newsI
& & & & }catch (Exception e){
& & & & & & e.printStackTrace();
& & & & & &
五.编写界面交互代码
用于实现获取服务器的NewsInfo.xml文件解析并将解析的信息设置到ListView显示在界面上。下载jar包,将jar文件粘贴在工程目录下的libs文件夹下,然后右击选择add the library即可,急需要以下jar包,可以不去网站自己下载
android-async-http-1.4.8.
android-smart-image-view-1.0.0
httpcore-4.4.4
package cn.edu.bzu.
import android.graphics.C
import android.support.v7.app.AppCompatA
import android.os.B
import android.view.V
import android.view.ViewG
import android.widget.BaseA
import android.widget.LinearL
import android.widget.ListV
import android.widget.TextV
import android.widget.T
import com.loopj.android.http.AsyncHttpC
import com.loopj.android.http.AsyncHttpResponseH
import com.loopj.android.image.SmartImageV
import org.apache.http.H
import java.io.ByteArrayInputS
import java.util.L
import cn.edu..edu.bzu.XML.NewsInfoS
public class MainActivity extends AppCompatActivity {
& & private ListView lv_
& & private LinearL
& & private List&NewsInfo& newsI
//ListView 适配器
& & private class NewsAdapter extends BaseAdapter {
& & //ListView的item数
& & & & public int getCount() {
& & & & & & return newsInfos.size();
& & & & //得到ListView条目视图
& & & & public View getView(int position, View convertView, ViewGroup parent) {
& & & & & & View view = View.inflate(MainActivity.this, R.layout.activity_main, null);
& & & & & & SmartImageView siv = (SmartImageView) view.findViewById(R.id.siv_con);
& & & & & & TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
& & & & & & TextView tv_description = (TextView) view.findViewById(R.id.tv_description);
& & & & & & TextView tv_type = (TextView) view.findViewById(R.id.tv_type);
& & & & & & NewsInfo newsInfo = newsInfos.get(position);
& & & & & & // &SmartImageView加载指定路径图片
& & & & & & siv.setImageUrl(newsInfo.getIconPath(), R.drawable.ab, R.mipmap.ic_launcher);
& & & & & & //设置新闻标题
& & & & & & tv_title.setText(newsInfo.getTitle());
& & & & & & //设置新闻描述
& & & & & & tv_description.setText(newsInfo.getDescription());
& & & & & & int type = newsInfo.getType();//1一般新闻2.专题3.live
& & & & & & //不同的新闻类型设置不同的颜色和不同的内容
& & & & & & switch (type) {
& & & & & & & & case 1:
& & & & & & & & & & tv_type.setText(&评论:& + newsInfo.getComment());
& & & & & & & & & &
& & & & & & & & case 2:
& & & & & & & & & & tv_type.setTextColor(Color.RED);
& & & & & & & & & & tv_type.setText(&专题&);
& & & & & & & & & &
& & & & & & & & case 3:
& & & & & & & & & & tv_type.setTextColor(Color.BLUE);
& & & & & & & & & & tv_type.setText(&LIVE&);
& & & & & & & & & &
& & & & & & }
& & & & & &
& & & & //条目对象
& & & & public Object getItem(int position) {
& & & & & &
& & & & //条目id
& & & & public long getItemId(int position) {
& & & & & & return 0;
& & @Override
& & protected void onCreate(Bundle savedInstanceState) {
& & & & super.onCreate(savedInstanceState);
& & & & setContentView(R.layout.activity_main);
& & & & lv_news = (ListView) findViewById(R.id.lv_news);
& & & & loading = (LinearLayout) findViewById(R.id.loading);
& & & & fillData2();
//使用AsyncHttpClient访问网络
& & private void fillData2() {
& & & & //创建AsyncHttpClient的实例
& & & & AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
& & & & //使用get方式请求
& & & & asyncHttpClient.get(getString(R.string.serverurl), new AsyncHttpResponseHandler() {
& & & & & & public void onSuccess(String content) {
& & & & & & & & //访问成功
& & & & & & & super.onSuccess(content);
& & & & & & &//将字符串转换成数组
& & & & & & & & byte[] bytes = content.getBytes();
& & & & & & & & //将数组转换成输入流
& & & & & & & & ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
& & & & & & & & //调用NewsInfoService工具类解析xml文件
& & & & & & & & newsInfos = NewsInfoService.getNewsInfos(bais);
& & & & & & & & if (newsInfos == null) {
& & & & & & & & & & //解析失败 弹出toast
& & & & & & & & & & Toast.makeText(MainActivity.this, &解析失败&, Toast.LENGTH_LONG).show();
& & & & & & & & } else {
& & & & & & & & & & //更新界面
& & & & & & & & & & loading.setVisibility(View.INVISIBLE);
& & & & & & & & & & lv_news.setAdapter(new NewsAdapter());
& & & & & & & & }
& & & & & & }
& & & & & & //请求失败
& & & & & & public void onFailure( Throwable error,String content) {
& & & & & & & &// super.onFailure(error, content);
& & & & & & & & Toast.makeText(MainActivity.this, &请求失败&, Toast.LENGTH_LONG).show();
& & & & & & }
& & & & });
六.配置服务器
因为需要从服务器上下载一个XML,因此需要开启Tomcat服务器。在Tomcat根目录下找到bin文件夹,运行该文件夹下的startup.bat文件即可开启服务器,然后在Tomcat的安装目录下打开webapps文件夹,将NewsInfo.xml文件放置在root文件夹下,代码如下:
&?xml version=&1.0&?&
-&newsInfo&
&icon&localhost:8080/img/b.png&/icon&
&title&《神武》&/title&
&content&新美术资源盘点,视觉新体验&/content&
&type&2&/type&
&comment&35&/comment&
&/newsInfo
-&newsInfo&
&icon&localhost:8080/img/c.png&/icon&
&title&南北车正式公布合并&/title&
&content&南北车将于今日正式公布合并&/content&
&type&3&/type&
&comment&2&/comment&
&/newsInfo&
-&newsInfo&
&icon&localhost:8080/img/d.png&/icon&
&title&呆了!汪星人抱玩偶酣睡&/title&
&content&汪星人抱玩偶酣睡,萌翻网友&/content&
&type&1&/type&
&comment&25&/comment&
&/newsInfo&
-&newsInfo&
&icon&localhost:8080/img/e.png&/icon&
&title&风力发电进校园&/title&
&content&风力发电普进校园&/content&
&type&2&/type&
&comment&26&/comment&
&/newsInfo&
-&newsInfo&
&icon&localhost:8080/img/f.png&/icon&
&title&地球一小时&/title&
&content&地球熄灯一小时&/content&
&type&1&/type&
&comment&23&/comment&
&/newsInfo&
-&newsInfo&
&icon&localhost:8080/img/g.png&/icon&
&title&最美公路&/title&
&content&最美公路,难以想象&/content&
&type&1&/type&
&comment&23&/comment&
&/newsInfo&
七.在value文件夹下创建一个config.xml
代码如下:
&?xml version=&1.0& encoding=&utf-8&?&
&resources&
& & &string name=&serverurl&&http://10.61.6.212:8080/newInfo.xml&/string&
&/resources&
八.添加权限
因为需要访问网络因此需要在androidMainfest.xml里面配置相应的权限
&?xml version=&1.0& encoding=&utf-8&?&
&manifest xmlns:android=&/apk/res/android&
& & package=&cn.edu.bzu.anew&&
&uses-permission android:name=&android.permission.INTERNET&&&/uses-permission&
& & &application
& & & & android:allowBackup=&true&
& & & & android:icon=&@mipmap/ic_launcher&
& & & & android:label=&@string/app_name&
& & & & android:supportsRtl=&true&
& & & & android:theme=&@style/AppTheme&&
& & & & &activity android:name=&.MainActivity&&
& & & & & & &intent-filter&
& & & & & & & & &action android:name=&android.intent.action.MAIN& /&
& & & & & & & & &category android:name=&android.intent.category.LAUNCHER& /&
& & & & & & &/intent-filter&
& & & & &/activity&
& & &/application&
&/manifest&
九.完成后如下所示:
代码有点小瑕疵,建议看看就好!!
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:705次
排名:千里之外
原创:14篇
(3)(5)(2)(4)48443人阅读
Android开发(128)
第二种主界面风格则是以网易新闻、凤凰新闻以及新推出的新浪博客(阅读版)为代表,使用ViewPager+Fragment,即ViewPager里适配器里放的不是一般的View,而是Fragment。所以适配器不能继承PagerAdapter,而要继承FragmentPagerAdapter,这是在android.support.v4.app.FragmentPagerAdapter包里的。有点奇葩的是,FragmentPagerAdapter只在这个包里有,在android.app.*这个包下面么有。到后面会发现,只能用android.support.v4.app.*包下面的东西。两个包里的FragmentManager是不通用的,而且两个包里提供的Fragment也不大一样。如果继承android.app.*下的Fragment,则不能重新写构造函数,只能用默认的。v4的包里么有这个限制。 下图是网易新闻、凤凰新闻、新浪博客的截图: 关于仿网易新闻客户端代码已经很多了,本人主要根据开源的这个的制作,准备一步步搞下。这本是一个大牛之作发在oschina上,参考链接里分5步去实现。我看了它的代码,是染在一起的。比如要完这个导航不需要额外的三个包,而他的资源里是弄一起的。所以准备自己玩玩,顺便记录开发中的问题。第一步:最上面的导航栏 即有“网易新闻”四个大字这一栏。布局文件head_title_panel.xml:&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:background=&@color/light_blue&
android:orientation=&horizontal& &
&ImageView
android:id=&@+id/headIcon&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:layout_marginLeft=&8dp&
android:layout_marginRight=&4dp&
android:src=&@drawable/biz_navigation_tab_news_pressed& /&
&ImageView
android:id=&@+id/headDivider&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:layout_marginLeft=&4dp&
android:layout_marginRight=&4dp&
android:src=&@drawable/base_action_bar_back_divider& /&
android:id=&@+id/headTV&
android:layout_width=&0dp&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:layout_marginLeft=&4dp&
android:layout_weight=&1&
android:text=&CSDN资讯&
android:textColor=&@color/white&
android:textSize=&21sp&
android:textStyle=&bold&&
&/TextView&
&/LinearLayout&为了日后操作上的方便,我将它映射成一个HeadTitlePanel.java文件,可以看到这种写法跟&上下panel的定义是有点区别的。package org.yanzi.
import org.yanzi.csdnproject.R;
import android.content.C
import android.util.AttributeS
import android.view.LayoutI
import android.view.V
import android.widget.ImageV
import android.widget.RelativeL
import android.widget.TextV
public class HeadTitlePanel extends RelativeLayout {
private Context mC
private TextView mHeadTV;
private ImageView mHeadI
private ImageView mHeadD
public HeadTitlePanel(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mContext =
View parent = LayoutInflater.from(mContext).inflate(R.layout.head_title_panel, this, true);
mHeadTV = (TextView) parent.findViewById(R.id.headTV);
第二步:ViewPager的导航栏 这个本来我是准备自己封装个的,网上也有用Radiobutton封装的,考虑到它这个导航栏还是不固定长度可以滑动的,时间原因暂时不封装了,使用开源Android-ViewPagerIndicator-master.zip 这个包,这个人的github链接:&将其中的library文件夹改名ViewPagerIndicator_library导进来。这个里面有好几种Indicator,我们主要用TabPageIndicator这个。第三步:MainActivity的布局:activity_main.xml&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:background=&#eee&
tools:context=&.MainActivity& &
&org.yanzi.ui.HeadTitlePanel
android:id=&@+id/head_title_panel&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_alignParentTop=&true& /&
&com.viewpagerindicator.TabPageIndicator
android:id=&@+id/page_indicator&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_below=&@id/head_title_panel&
android:background=&@color/transparentblue& /&
&android.support.v4.view.ViewPager
android:id=&@+id/view_pager&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:layout_below=&@id/page_indicator&
&/RelativeLayout&MainActivity.javapackage org.yanzi.
import org.yanzi.viewpager.adapter.TabA
import com.viewpagerindicator.TabPageI
import android.os.B
import android.app.A
import android.app.FragmentM
import android.view.M
import android.support.v4.app.FragmentA
import android.support.v4.app.FragmentPagerA
import android.support.v4.view.ViewP
public class MainActivity extends FragmentActivity {
private TabPageIndicator mPageI
private ViewPager mViewP
private FragmentPagerAdapter fragPagerA
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragPagerAdapter = new TabAdapter(getSupportFragmentManager());
mViewPager.setAdapter(fragPagerAdapter);
mPageIndicator.setViewPager(mViewPager, 0);
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
private void initUI(){
mPageIndicator = (TabPageIndicator)findViewById(R.id.page_indicator);
mViewPager = (ViewPager)findViewById(R.id.view_pager);
出奇的简单,比单纯用Fragment还简单,原因是只需把Fragment塞到适配器里就ok了,适配器为我们做了Fragment的切换等工作,我们能做的也就是在适配器里new Fragment的时候判断是否已存在。以下几点需要注意:1、在styles.xml里它定义了样式:
&style name=&MyTheme& parent=&AppBaseTheme&&
&item name=&vpiTabPageIndicatorStyle&&@style/MyWidget.TabPageIndicator&/item&
&item name=&android:windowBackground&&@drawable/init_pic&/item&
&item name=&android:windowNoTitle&&true&/item&
&item name=&android:animationDuration&&5000&/item&
&item name=&android:windowContentOverlay&&@null&/item&
&style name=&MyWidget.TabPageIndicator& parent=&Widget&&
&item name=&android:gravity&&center&/item&
&item name=&android:background&&@drawable/vpi__tab_indicator&/item&
&item name=&android:paddingLeft&&22dip&/item&
&item name=&android:paddingRight&&22dip&/item&
&item name=&android:paddingTop&&8dp&/item&
&item name=&android:paddingBottom&&8dp&/item&
&item name=&android:textAppearance&&@style/MyTextAppearance.TabPageIndicator&/item&
&item name=&android:textSize&&16sp&/item&
&item name=&android:maxLines&&1&/item&
&style name=&MyTextAppearance.TabPageIndicator& parent=&Widget&&
&item name=&android:textStyle&&bold&/item&
&item name=&android:textColor&&@color/black&/item&
&/style&这个是依赖于导进去的包的。2、它这里用了android:windowBackground的属性,所以app开启瞬间会有图片弹出,之后设置MainActivity的布局背景为android:background=&#eee&,又把图片替换了。如果不设android:background=&#eee& 会一直看到这个图片不消失。3、因为开篇讲的原因,MainActivity只能继承自FragmentActivity。第四步:MainFragment.java,此类继承Fragment,且是android.support.v4.app.Fragment下的。package org.yanzi.
import org.yanzi.csdnproject.R;
import android.os.B
import android.support.v4.app.F
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.TextV
public class MainFragment extends Fragment {
private int mNewsType = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.tab_item_fragment_main, null);
TextView tip = (TextView) v.findViewById(R.id.id_tip);
Bundle b = getArguments();
String title = b.getString(&TITLES&);
tip.setText(title);
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
就是弄了一个布局,然后从中取得参数并显示。第五步:ViewPager的适配器TabAdapter.javapackage org.yanzi.viewpager.
import org.yanzi.constant.C
import org.yanzi.fragment.MainF
import android.os.B
import android.support.v4.app.F
import android.support.v4.app.FragmentM
import android.support.v4.app.FragmentPagerA
public class TabAdapter extends FragmentPagerAdapter {
public TabAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
public Fragment getItem(int position) {
// TODO Auto-generated method stub
MainFragment fragment = new MainFragment();
Bundle b = new Bundle();
String title = Constant.TITLES[position];
b.putString(&TITLES&, title);
fragment.setArguments(b);
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return Constant.TITLES[position];
public int getCount() {
// TODO Auto-generated method stub
return Constant.TITLES.
主要是重写getItem,在这里实例化Fragment,同时设传递的参数。其实这里可以通过FragmentManager按照Tag查找对应的Fragment是否存在,再进行实例化。另外,getPageTitle这个接口必须重写,其实看TabPageIndicator.java这个类的实现可以发现,它需要传进去一个ViewPager,然后获得适配器,从适配器里得到Title。因为用了这个开源包,不能再用ViewPager的setOnPageChangeListener接口,只能用mPageIndicator.setOnPageChangeListener(listener)进行监听。 完毕,源码链接:链接:/s/1bn4EFbp 密码:dx4s 效果如下:
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4994430次
积分:36634
积分:36634
排名:第112名
原创:374篇
转载:12篇
评论:2959条
文章:23篇
阅读:547615
(5)(1)(1)(1)(3)(1)(3)(5)(5)(5)(8)(4)(3)(3)(7)(10)(4)(2)(3)(1)(3)(7)(2)(1)(4)(1)(3)(17)(14)(7)(8)(7)(6)(3)(7)(7)(3)(3)(7)(5)(6)(11)(24)(6)(11)(10)(13)(11)(34)(39)(2)(23)(6)

我要回帖

更多关于 csdn客户端 的文章

 

随机推荐