碎片最佳实践-简易版的新闻应用

先上成果图

小屏幕小屏幕

点击后点击后
大屏幕Nexus7大屏幕Nexus7
点击后点击后

主体思想

写两个activity_main布局文件,一个在layout文件夹下,另一个置于layout-sw600dp下。前者只有一个titleFragment碎片,后者还会加上contentFragment碎片,系统根据设备大小加载不同的activity_main。但是也由于两种布局下都包含titleFragment碎片,在点击ListView时,面临两种选择。其一,若为单页模式,就跳转到包含内容布局的活动。若为双页,获取右碎片实例(getFragmentManager.findFragmentById),refresh下即可。至于如何判断当前处于单页还是双页,两个activity_main不同的就是大屏幕的多了一个右侧碎片,给这个碎片加上id,在titleFragment中的onActivityCreated(活动加载完成才执行的函数) 用getActivity().findViewById(右侧碎片的id)来判断。getActivity是获取与该碎片相关联的活动

遇到的问题

  1. 继承自android.app.Fragment的碎片类,里面的onAttach(Context context) 在API23中
    不会被调用,导致里面的初始化操作不会执行

解决办法: 将onAttach()中代码转移进onCreateView()

  1. 代码无异常,运行时抛出ActivityNotFound异常

解决办法:在AndroidManifest下注册活动

上代码

代码结构

结构结构

News.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class News {

private String title;
private String 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;
}
}

NewsAdapter.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class NewsAdapter extends ArrayAdapter<News>{

private int resourceId;

private View view;

private ViewHolder viewHolder;

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);
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(resourceId,null);
viewHolder = new ViewHolder();
viewHolder.titleTextView = (TextView) view.findViewById(R.id.title_item_tv);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}

viewHolder.titleTextView.setText(news.getTitle());
return view;
}

class ViewHolder{
TextView titleTextView;
}
}

NewsContentFragment.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class NewsContentFragment extends Fragment{

private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.content_frag,container,false);
return view;
}

public void refresh(String title, String content){
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.content);
linearLayout.setVisibility(View.VISIBLE);
TextView title_tv = (TextView) view.findViewById(R.id.tv_title);
TextView content_tv = (TextView) view.findViewById(R.id.tv_content);
title_tv.setText(title);
content_tv.setText(content);
}

}

NewsTitleFragment.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class NewsTitleFragment extends Fragment implements AdapterView.OnItemClickListener{

private List<News> newsList = new ArrayList<News>();

private Boolean twoPane;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
initData();
NewsAdapter adapter = new NewsAdapter(getActivity(),R.layout.title_item,newsList);
View view = inflater.inflate(R.layout.newstitle_frag,container,false);
ListView NewsTitleListView = (ListView) view.findViewById(R.id.title_list_view);
NewsTitleListView.setAdapter(adapter);
NewsTitleListView.setOnItemClickListener(this);
return view;
}

public void initData(){
News news1 = new News();
news1.setTitle("Google will accept Gaby to be the CEO");
news1.setContent("To reduce housing inventories, more migrant rural workers should be issued with urban-residency permits, which will allow them to purchase housing in cities, according to a statement released after a meeting of the Political Bureau of the CPC Central Committee presided over by President ***.");
newsList.add(news1);

News news2 = new News();
news2.setTitle("High-tech products at Light of the Internet Expo Baidu 'self-driving' cars to hit roads in 3 years");
news2.setContent("Baidu indicated that the new business unit will focus on research and development of self-driving technology as well as build the industrial chain to support the production of next-generation cars.");
newsList.add(news2);

News news3 = new News();
news3.setContent("In the future, vehicles could be self-driven and controlled by smart systems. We have set a target to commercialize our operations in the next three years,\" he said, adding that in 10 years, about 80 percent of the newly produced vehicles would be equipped with self-driving technologies");
news3.setTitle("Reasonable economomic growth rate targeted");
newsList.add(news3);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.content_frag_layout) == null){
twoPane = false;
} else{
twoPane = true;
}
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News news = newsList.get(position);
if(twoPane){
NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.content_frag_layout);
newsContentFragment.refresh(news.getTitle(), news.getContent());
} else{
contentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
}
}
}

content_frag.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="invisible">

<TextView
android:id="@+id/tv_title"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:src="@drawable/vandh"
android:scaleType="fitXY"/>

<TextView
android:id="@+id/tv_content"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="15dp"/>
</LinearLayout>

<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:src="@drawable/vandh"
android:layout_alignParentLeft="true"/>
</RelativeLayout>

newsTitle_frag.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/title_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout

title_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/title_item_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="true"
android:ellipsize="end"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"/>
</LinearLayout>

content.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/content_alone"
android:name="com.example.gaby.fragmentbestpacticey.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout

layout-anctivity_main

1
2
3
4
5
6
7
8
9
10
11
12
<?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">

<fragment
android:id="@+id/title_frag"
android:name="com.example.gaby.fragmentbestpacticey.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

layout-sw600dp-activity_main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<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.example.gaby.fragmentbestpacticey.NewsTitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>

<fragment
android:id="@+id/content_frag_layout"
android:name="com.example.gaby.fragmentbestpacticey.NewsContentFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
/>

</LinearLayout>
posted @ 2016-03-20 22:03  Gabyler  阅读(484)  评论(0编辑  收藏  举报