新闻界面的Fragment实现
先添加一个准备好的新闻实体类,News。
public calss News{
pirvate String title;
private String content;
public News(String title, String content){
this.title = title;
this.content = content;
}
public void setTitle(String title){
this.title = title;
}
public void setContent(String content){
this.content = content;
}
public String getTitle(){
return title;
}
public String getContent(){
return content;
}
}
因为标题肯定是一个列表形式的存在所以要用listview显示内容,接下来先配置子项的标题布局,news_item.xml:
<LinearLayout xmlns: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:text="18sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"/>
</LinearLayout>
其中android:padding 表示给控件的周围加上补白,这样不至于内容会紧靠在边缘上。
android:singleLine 设置为true表示让这个TextView只能单行显示。
android:ellipsize 用于设定当文本内容超过控件宽度时,文本的缩略方式,这里指定成end表示在尾部进行缩略。
接下来创建新闻列表的适配器,因为新闻列表的格式是listview,需要用适配器去配置内容(适配器是配置能给用户看得见的内容,意会,因为我总是不能理解适配器的主要作用)。
public class NewsAdapter extends ArrayAdapter<News>{
private int resourceId;
public NewsAdapter(Context context, int textViewResourceId, List<News> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
News news = getItem(position);
View view;
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
}else{
view = convertView;
}
TextView newTitleText = (TextView)view.findViewById(R.id.news_title);
newTitleText.setText(news.getTitle());
return view;
}
}
注意这里的指定泛型要是News.
接下来是新闻内容的代码,新建布局news_content_frag.xml:
<RelativeLayout xmlns: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/line"/>
<TextView
android:id="@+id/new_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/line"/>
</RelativeLayout>
这里要注意android:scaleType属性设置为fitXY,表示让这张图片充满整个控件的大小。
接下来新建一个NewsContentFragment类用来获取新闻内容布局的实例,并做刷新。
public class NewsContentFragment extends Fragment{
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.news_content_frag, container, false);
return view;
}
/**
* 首先在 onCreateView()方法里加载了我们刚刚创建的 news_content_frag 布局,这个没什
么好解释的。接下来又提供了一个 refresh()方法,这个方法就是用于将新闻的标题和内容显
示在界面上的。可以看到,这里通过 findViewById()方法分别获取到新闻的标题和内容控件,
然后将方法传递进来的参数设置进去
* @param newtitle
* @param newsContent
*/
public void refresh(String newtitle, String newsContent){
View visibilityLayout = view.findViewById(R.id.visibility_layout);
visibilityLayout.setVisibility(View.VISIBLE);
TextView newTitleText = (TextView)view.findViewById(R.id.news_title);
TextView newContentText = (TextView)view.findViewById(R.id.new_content);
newTitleText.setText(newtitle);
newContentText.setText(newsContent);
}
}
接下来新建新闻内容fragment的布局news_content.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/news_content_fragment"
android:name="com.zhaojy.androidstu_fragmentnewspriacice.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
接下来新建NewsContentActivity作为显示新闻内容的活动:
public class NewsContentActivity extends Activity{
public static void actionStart(Context context, String newsTitle, String newsContent){
Intent intent = new Intent(context, NewsContentActivity.class);
intent.putExtra("news_title", newsTitle);
intent.putExtra("news_content", newsContent);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.news_content);
String newsTitle = getIntent().getStringExtra("news_title");// 获取传入的新闻标题
String newsContent = getIntent().getStringExtra("news_content");// 获取传入的新闻内容
NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager()
.findFragmentById(R.id.news_content_fragment);//活动获取碎片信息的方式,通过manager的方式获取实例
newsContentFragment.refresh(newsTitle, newsContent);//获取到了实例以后就可以调用碎片里面的方法,刷新NewsContentFragment界面
}
}
接下来创建一个用于显示新闻列表的布局,news_title_frag.xml”
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/news_title_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
为以上的listView设置使用的对象,该对象是一个碎片,接下来创建一个碎片加载该布局:
public class NewsTitleFragment extent Fragment implement OnItemClickListener{
private ListView newsTitleView;
private List<News> newsList;
private NewsAdapter adapter;
private boolean isTwoPane;
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
newsList = getNews();//初始化新闻标题、内容的数据
adapter = new NewsAdapter(activity, R.layout.news_item, newsList);
}
@Override
public view onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
View view = infalter.inflate(R.layout.news_title_frag, container, false);
newsTitleListView = (ListView)view.findViewById(R.id.news_title_list_view);
newsTitleListView = setAdapter(adapter);
newsTitleListView.setOnItemClickListener(this);
return view;
}
@Override
public void onActivityCreate(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.news_content_layout) != null){
isTwoPane = true;
}else{
isTwoPane = false;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
News news = newsList.get(position);
if(isTwoPane){
NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager().findFragmentById(R.is.news_content_fragment);
}else{
NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent());
}
}
private List<News> getNews() {
List<News> newsList = new ArrayList<News>();
News news1 = new News("Succeed in College as a Learning Disabled Student","College freshmen will soon learn to live with a"
+ "roommate, adjust to a new social scene and survive less-than-stellar"
+ "dining hall food. Students with learning disabilities will face these"
+ "transitions while also grappling with a few more hurdles.");
// news1.setTitle("Succeed in College as a Learning Disabled Student");
// news1.setContent("College freshmen will soon learn to live with a"
// + "roommate, adjust to a new social scene and survive less-than-stellar"
// + "dining hall food. Students with learning disabilities will face these"
// + "transitions while also grappling with a few more hurdles.");
newsList.add(news1);
News news2 = new News("Google Android exec poached by China's Xiaomi","China's Xiaomi has poached a key Google executive"
+ "involved in the tech giant's Android phones, in a move seen as a coup"
+ "for the rapidly growing Chinese smartphone maker.");
// news2.setTitle("Google Android exec poached by China's Xiaomi");
// news2.setContent("China's Xiaomi has poached a key Google executive"
// + "involved in the tech giant's Android phones, in a move seen as a coup"
// + "for the rapidly growing Chinese smartphone maker.");
newsList.add(news2);
return newsList;
}
该类用到了几个fragment的生命周期,其中onAttach()方法最先执行的时候做数据的初始化操作,getNews()获取模拟新闻的数据,以及完成NewsAdapter的创建。在onCreateView()这个我们很熟悉的方法当中加载碎片布局,并给新闻列表注册点击事件。接下来在onActivityCreate()方法中通过寻找单双页的一个标志news_content_layout的View来判断单双页模式,如果是双页,就通过getFragmentManager()找到碎片的id并实例化,调用刷新方法去刷新界面,如果是单页就启动另一个显示的活动。
接下来修改activity_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">
<fragment
android:id="@+id/news_title_fragment"
android:name="com.zhaojy.androidstu_fragmentnewspriacice.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
为双页模式设置动态加载布局,建立layout-sw600dp的文件夹,里面同样建立activity_main的xml布局文件,但是代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/news_title_fragment"
android:name="com.zhaojy.androidstu_fragmentnewspriacice.NewsTitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/news_content_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3">
<fragment
android:id="@+id/news_content_fragment"
android:name="com.zhaojy.androidstu_fragmentnewspriacice.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
这样就完成了一个可以自动适配屏幕的新闻查看器。