志在指尖
用双手敲打未来

新闻app优秀实践

一、使用碎片来进行一个最佳实践,即我们写一个新闻的app
1.首先先建立一个新闻类
packagecom.example.fragmentbestpractice;
publicclassNews{
privateStringtitle;
privateStringcontent;
publicStringgetTitle(){
returntitle;
}
publicvoidsetTitle(Stringtitle){
this.title=title;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
}
2.然后我们设置一个界面,也就是显示新闻的界面
<?xmlversion=”1.0″encoding=”utf-8″?>
<LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”>
<TextView
android:id=”@+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:singleLine=”true”
android:ellipsize=”end”
android:textSize=”18sp”
android:paddingLeft=”10dp”
android:paddingRight=”10dp”
android:paddingTop=”15dp”
android:paddingBottom=”15dp”
/>
</LinearLayout>
这里面有几个新的属性设置是我们之前没有见到过的,首先来看android:singLine设置为true代表的就是TextView只能单行显示;android:ellipse用于设定当文本内容超出控件的宽度的时候,文本的缩略方式,这里指定成end表示在尾部进行缩略?。
3.接下来需要创建一个新闻列表的适配器,让这个适配器继承自ArrayAdapter,并将泛型指定为News类,下面我们新建NewsAdapter
packagecom.example.fragmentbestpractice;
importjava.util.List;
importandroid.content.Context;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.ArrayAdapter;
importandroid.widget.TextView;
publicclassNewsAdapterextendsArrayAdapter<News>{
privateintresourceId;
publicNewsAdapter(Contextcontext,inttextViewResourceId,List<News>objects){
super(context,textViewResourceId,objects);
resourceId=textViewResourceId;
}
@Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
Newsnews=getItem(position);
Viewview;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId,null);
}else{
view=convertView;
}
TextViewnewsTitleText=(TextView)view.findViewById(R.id.news_title);
newsTitleText.setText(news.getTitle());
returnview;
}
}
可以看出来,在getView()方法中,我们获取到了相应位置上的News类,并且让新闻的标题在列表中?进行显示。
4.编写新闻内容部分的代码
<?xmlversion=”1.0″encoding=”utf-8″?>
<RelativeLayoutxmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<LinearLayout
android:id=”@+id/visibility_layout”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
android:visibility=”invisible”>
<TextView
android:id=”@+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:gravity=”center”
android:padding=”10dp”
android:textSize=”20sp”/>
<ImageView
android:layout_width=”match_parent”
android:layout_height=”1dp”
android:scaleType=”fitXY”
android:src=”@drawable/split_line”/>
<TextView
android:id=”@+id/news_content”
android:layout_width=”match_parent”
android:layout_height=”0dp”
android:layout_weight=”1″
android:padding=”15dp”
android:textSize=”18sp”/>
</LinearLayout>
<ImageView
android:layout_width=”1dp”
android:layout_height=”match_parent”
android:layout_alignParentLeft=”true”
android:scaleType=”fitXY”
android:src=”@drawable/split_line_vertical”/>
</RelativeLayout>

未经允许不得转载:IT技术网站 » 新闻app优秀实践
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载