碎片的使用

碎片是什么
碎片(Fragment)是一种可以嵌入在活动当中的UI片段,(3.0版本开始引入)它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用得非常广泛。
碎片的简单用法
新建一个左侧碎片布局left_fragment.xml
代码如下:
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <Button
  7. android:id="@+id/button"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal"
  11. android:text="Button"/>
  12. </LinearLayout>
新键右侧碎片布局
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#00ff00"
  6. android:orientation="vertical">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal"
  11. android:text="This is right fragment"
  12. android:textSize="20sp"/>
  13. </LinearLayout>
接着新建一个LeftFragment类
   
   
  1. public class LeftFragment extends Fragment {
  2. @Nullable
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
  5. savedInstanceState) {
  6. View view = inflater.inflate(R.layout.left_fragment, null);
  7. return view;
  8. }
  9. }
RightFragment类
   
   
  1. public class RightFragment extends Fragment {
  2. @Nullable
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
  5. savedInstanceState) {
  6. View view = inflater.inflate(R.layout.right_fragment, null);
  7. return view;
  8. }
  9. }
修改activity_main中代码
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="horizontal"
  9. tools:context="com.pengpeng.onenumber_04fragmentsimpleused.MainActivity">
  10. <fragment
  11. android:id="@+id/left_fragment"
  12. android:layout_width="0dp"
  13. android:layout_height="match_parent"
  14. android:name ="com.pengpeng.onenumber_04fragmentsimpleused.fragment.LeftFragment"
  15. android:layout_weight="1"/>
  16. <fragment
  17. android:id="@+id/right_fragment"
  18. android:layout_width="0dp"
  19. android:name ="com.pengpeng.onenumber_04fragmentsimpleused.fragment.RightFragment"
  20. android:layout_height="match_parent"
  21. android:layout_weight="1"/>
  22. </LinearLayout>
如何动态添加碎片
新建another_right_fragment.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#ffff00"
  6. android:orientation="vertical">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal"
  11. android:text="This is another right fragment"
  12. android:textSize="20sp"/>
  13. </LinearLayout>
新建AnotherRightFragment类
   
   
  1. public class AnotherRightFragment extends Fragment {
  2. @Nullable
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
  5. savedInstanceState) {
  6. View view = inflater.inflate(R.layout.another_right_fragment, container, false);
  7. return view;
  8. }
  9. }
修改activity_main代码
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="horizontal"
  9. tools:context="com.pengpeng.onenumber_04fragmentsimpleused.MainActivity">
  10. <fragment
  11. android:id="@+id/left_fragment"
  12. android:name="com.pengpeng.onenumber_04fragmentsimpleused.fragment.LeftFragment"
  13. android:layout_width="0dp"
  14. android:layout_height="match_parent"
  15. android:layout_weight="1"/>
  16. <FrameLayout
  17. android:id="@+id/right_layout"
  18. android:layout_width="0dp"
  19. android:layout_height="match_parent"
  20. android:layout_weight="1">
  21. </FrameLayout>
  22. </LinearLayout>
修改MainActivity中的代码
   
   
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. Button button = (Button) findViewById(R.id.button);
  7. button.setOnClickListener(this);
  8. replaceFragment(new RightFragment());
  9. }
  10. private void replaceFragment(Fragment fragment) {
  11. FragmentManager fragmentManager = getSupportFragmentManager();//获取FragmentManager
  12. FragmentTransaction transaction = fragmentManager.beginTransaction();//开启事务
  13. transaction.replace(R.id.right_layout, fragment);//向容器内添加或替换碎片
  14. transaction.commit();//提交事务
  15. }
  16. @Override
  17. public void onClick(View v) {
  18. switch (v.getId()) {
  19. case R.id.button:
  20. replaceFragment(new AnotherRightFragment());
  21. break;
  22. default:
  23. break;
  24. }
  25. }
  26. }
在碎片中模拟返回栈
按下Back键就会直接退出,按下Back键返回上一个碎片如何实现
FragmentTransaction中提供了了一个addToBackStack()方法,可以用于将一个事务添加到返回栈中,修改MainActivity中的代码
   
   
  1. private void replaceFragment(Fragment fragment) {
  2. FragmentManager fragmentManager = getSupportFragmentManager();//获取FragmentManager
  3. FragmentTransaction transaction = fragmentManager.beginTransaction();//开启事务
  4. transaction.replace(R.id.right_layout, fragment);//向容器内添加或替换碎片
  5. transaction.addToBackStack(null);//接收一个名字描述返回栈的状态,一般传null即可
  6. transaction.commit();//提交事务
  7. }
碎片和活动之间进行通信
虽然碎片都是嵌入在活动中显示的,可是实际上他们的关系并没有那么亲密,碎片和活动都是各自存在于一个独立的类当中的,它们之间并没有那么明显的方式来直接进行通信,如果想要在活动中直接调用碎片里的方法,或者在碎片中调用活动里的方法,应该如何实现
为了方便碎片和活动之间进行通信,FragmentManager提供了一个类似于findViewById()的方法,专门用于从布局文件中获取碎片的实例,代码如下
   
   
  1. LeftFragment leftFragment = (LeftFragment) getSupportFragmentManager().findFragmentById(R.id.left_fragment);
调用FragmentManager的findFragmentById()方法可以在活动中得到相应的碎片的实例,然后就能轻松调用碎片里的方法了
在碎片中如何调用活动里的方法呢,在每个碎片中都可以通过调用getAcitivity()方法来的到当前碎片相关联的活动实例
代码如下
   
   
  1. MainActivity activity = (MainActivity) getActivity();
另外当碎片中需要使用Context对象时,也可以使用getActivity()方法。
碎片之间通信:
首先在一个碎片中可以得到与他关联的活动,然后在通过这个活动去获取另一个碎片的实例,这样也就实现了不同碎片之间的通信功能。
碎片的生命周期:
和活动一样碎片也有自己的生命周期,并且它和活动的生命周期差不多
碎片的状态和回调:
1:运行状态
当一个碎片是可见的,并且它所关联的活动正处于运行状态时,该碎片也处于运行状态
2:暂停状态
当一个活动进入暂停状态时,(由于另一个未占满屏幕的活动被添加到了栈顶),与他相关的可见碎片就会进入到暂停状态
3:停止状态
当一个活动进入停止状态时,与他相关联的碎片就会进入到停止状态,或者通过调用FragmentTransaction的remove(),replace()方法将碎片从活动中移除,但如果在事务提交之前调用
addToBackStack()方法,这时的碎片也会进入到停止状态,总的来说,碎片对用户来说是完全不可见的,有可能会被系统回收
4:销毁状态
碎片宗师依附于活动而存在的,因此当活动被销毁时,与他相关联的碎片就会进入销毁状态,或者通过调用FragmentTransaction的remove(),replace()方法将碎片从活动中移除,但在事务提交之前调用addToBackStack()方法,这时的碎片也会进入到销毁状态
碎片的几个回调方法:
onAttach():当碎片和活动建立关联的时候调用
onCreateView():当碎片创建视图加载布局的时候调用
onActivityCreated():确保与碎片相关联的活动一定已经创建完毕的时候调用
onDestroyView():当与碎片关联的视图被移除的时候调用
onDetach():当碎片和活动解除关联的时候调用
当碎片第一次被加载到屏幕上时,会依次执行onAttach(),onCreate(),onCreateView(),onActivityCreated(),onStart()和OnResume()方法
替换碎片时,执行onPause(),onStop(),onDestoryView()方法,此时碎片进入停止状态
当然如果在替换的时候没有调用addToBackStack()方法,此时碎片就会进入销毁状态,onDestory()和
onDetach()方法就会得到执行
在碎片中也可以用onSaveInstanceState()方法来保存数据,因为进入停止状态的碎片有可能在系统内存不足的时候被回收,保存下来的数据在onCreate(),onCreateView(),onActivityCreated()这三个方法中都可以重新得到

动态加载布局的技巧:
怎样判断程序应该是使用双页模式还是单页模式这就需要使用限定符(Qulifiers)来实现了
使用限定符
在res目录下新建layout-large文件夹,在这个文件夹下新建一个布局,也叫activity_main.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="horizontal"
  9. tools:context="com.pengpeng.onenumber_04fragmentsimpleused.MainActivity">
  10. <fragment
  11. android:id="@+id/left_fragment"
  12. android:name="com.pengpeng.onenumber_04fragmentsimpleused.fragment.LeftFragment"
  13. android:layout_width="0dp"
  14. android:layout_height="match_parent"
  15. android:layout_weight="1"/>
  16. <fragment
  17. android:id="@+id/right_fragment"
  18. android:name="com.pengpeng.onenumber_04fragmentsimpleused.fragment.RightFragment"
  19. android:layout_width="0dp"
  20. android:layout_height="match_parent"
  21. android:layout_weight="3/>
  22. </LinearLayout>

原布局文件修改如下
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal">
  6. <fragment
  7. android:id="@+id/left_fragment"
  8. android:name="com.pengpeng.onenumber_04fragmentsimpleused.fragment.LeftFragment"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"/>
  11. </LinearLayout>
Android中一些常见的限定符
大小来作为特征
small:提供给小屏幕设备的资源
normal:提供给中等屏幕设备的资源
large:提供给大屏幕设备的资源
xlarge:提供给超大屏幕设备的资源
分辨率作为特征
ldpi:提供给低分辨率设备的资源120dpi以下
mdpi:提供给中等分辨率设备的资源120dpi-160dpi
hdpi:提供给高分辨率设备的资源160dpi-240dpi
xhdpi:提供给超高分辨率设备的资源240dpi-320dpi
xxhdpi:提供给超高分辨率设备的资源 320dpi-480dpi
方向作为特征
land:提供给横屏设备的资源
port:提供给竖屏设备的资源

使用最小宽度限定符
有的时候我们希望可以更加灵活的为不同设备加载布局,不管他们是不是被系统认定为large
这时就可以使用最小宽度限定符
在res目录下新建layout-sw600dp文件夹,然后在这个文件加下新建activity_main.xml
代码如下:
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="horizontal"
  9. tools:context="com.pengpeng.onenumber_04fragmentsimpleused.MainActivity">
  10. <fragment
  11. android:id="@+id/left_fragment"
  12. android:name="com.pengpeng.onenumber_04fragmentsimpleused.fragment.LeftFragment"
  13. android:layout_width="0dp"
  14. android:layout_height="match_parent"
  15. android:layout_weight="1"/>
  16. <fragment
  17. android:id="@+id/right_fragment"
  18. android:name="com.pengpeng.onenumber_04fragmentsimpleused.fragment.RightFragment"
  19. android:layout_width="0dp"
  20. android:layout_height="match_parent"
  21. android:layout_weight="3/>
  22. </LinearLayout>
这就意味着,当程序运行在屏幕宽度大于600dp的设备上时,会加载layout-sw600dp/activity_main布局
,当程序运行屏幕宽度在小于600dp的设备上时,则仍然会加载默认的layout/activity_main布局。

一个简易版的简单应用:
   
   
  1. dependencies {
  2. compile fileTree(dir: 'libs', include: ['*.jar'])
  3. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  4. exclude group: 'com.android.support', module: 'support-annotations'
  5. })
  6. compile 'com.android.support:appcompat-v7:24.2.1'
  7. compile 'com.android.support:recyclerview-v7:24.2.1'
  8. testCompile 'junit:junit:4.12'
  9. }
新建一个News类
   
   
  1. public class News {
  2. private String title;//新闻标题
  3. private String content;//新闻内容
  4. public String getTitle() {
  5. return title;
  6. }
  7. public void setTitle(String title) {
  8. this.title = title;
  9. }
  10. public String getContent() {
  11. return content;
  12. }
  13. public void setContent(String content) {
  14. this.content = content;
  15. }
  16. }
新建布局文件
news_content_frag.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <LinearLayout
  6. android:id="@+id/visibility_layout"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:orientation="vertical"
  10. android:visibility="invisible">
  11. <TextView
  12. android:id="@+id/news_title"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:gravity="center"
  16. android:padding="10dp"
  17. android:textSize="20sp"/>
  18. <View
  19. android:layout_width="match_parent"
  20. android:layout_height="10dp"
  21. android:background="#000"
  22. />
  23. <TextView
  24. android:id="@+id/news_content"
  25. android:layout_width="match_parent"
  26. android:layout_height="0dp"
  27. android:layout_weight="1"
  28. android:padding="15dp"
  29. android:textSize="18sp"/>
  30. </LinearLayout>
  31. <View
  32. android:layout_width="1dp"
  33. android:layout_height="match_parent"
  34. android:layout_alignParentLeft="true"
  35. android:background="#000"/>
  36. </RelativeLayout>
新建一个NewsContentFragment类
   
   
  1. public class NewsContentFragment extends Fragment {
  2. private View view;
  3. @Nullable
  4. @Override
  5. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
  6. savedInstanceState) {
  7. view = inflater.inflate(R.layout.news_content_frag,container,false);
  8. return view;
  9. }
  10. public void refresh(String newsTitle,String newsContent){
  11. View visibilityLayout = view.findViewById(R.id.visibility_layout);
  12. visibilityLayout.setVisibility(View.VISIBLE);
  13. TextView newsTitleText = (TextView) view.findViewById(R.id.news_title);
  14. TextView newsContentText = (TextView) view.findViewById(R.id.news_content);
  15. newsTitleText.setText(newsTitle);//刷新新闻的标题
  16. newsContentText.setText(newsContent);//刷新新闻的内容
  17. }
  18. }
如果想在单页模式中使用的话,我们还需再创建一个活动,新建一个NewsContentActivity,并修改new_content.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. >
  8. <fragment
  9. android:id="@+id/news_content_fragment"
  10. android:name="com.pengpeng.onenumber_04fragmentbestpractice.fragment.NewsContentFragment"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"/>
  13. </LinearLayout>
    
    
  1. public class NewsContentActivity extends AppCompatActivity {
  2. //重点******
  3. public static void actionStart(Context context, String newsTitle, String newsContent) {
  4. Intent intent = new Intent(context, NewsContentActivity.class);
  5. intent.putExtra("news_title", newsTitle);
  6. intent.putExtra("news_content", newsContent);
  7. context.startActivity(intent);
  8. }
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.news_content);
  13. String newsTitle = getIntent().getStringExtra("news_title");//获取传入的新闻标题
  14. String newsContent = getIntent().getStringExtra("news_content");//获取传入的新闻内容
  15. NewsContentFragment newsContentFragment = (NewsContentFragment) getSupportFragmentManager().findFragmentById
  16. (R.id.news_content_fragment);
  17. newsContentFragment.refresh(newsTitle, newsContent);//刷新NewsContentFragment界面
  18. }
创建一个用于显示新闻列表的布局,新建news_title_frag.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <android.support.v7.widget.RecyclerView
  7. android:id="@+id/news_title_recycler_view"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. </android.support.v7.widget.RecyclerView>
  11. </LinearLayout>
接下来新建news_item.xml作为RecyclerView的子项的布局
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/news_title"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:ellipsize="end"//设定当文本内容超出控件宽度时,文本的缩略方式,这里表示在尾部缩略
  7. android:maxLine="true"//单行显示
  8. android:paddingBottom="15dp"
  9. android:paddingLeft="10dp"
  10. android:paddingRight="10dp"
  11. android:paddingTop="15dp"//表示给控件的周围加上补白,不至于让文本内容会紧靠边缘上
  12. android:textSize="18sp"
  13. >
  14. </TextView>
新建一个用于显示新闻列表的碎片
NewsTitleFragment
   
   
  1. public class NewsTitleFragment extends Fragment {
  2. private boolean isTwoPane;//是否加载单双页模式
  3. @Nullable
  4. @Override
  5. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
  6. savedInstanceState) {
  7. View view = inflater.inflate(R.layout.news_title_frag, container, false);
  8. return view;
  9. }
  10. @Override
  11. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  12. super.onActivityCreated(savedInstanceState);
  13. if (getActivity().findViewById(R.id.news_content_layout) != null) {
  14. isTwoPane = true;//可以找到news_content_layout布局时,为双页模式
  15. } else {
  16. isTwoPane = false;//找不到news_content_layout布局时,为单页模式
  17. }
  18. }
  19. }
修改activity_main.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/news_title_layout"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <fragment
  8. android:id="@+id/news_title_fragment"
  9. android:name="com.pengpeng.onenumber_04fragmentbestpractice.fragment.NewsTitleFragment"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"/>
  12. </FrameLayout>
在单页模式下,只会加载一个新闻标题的碎片


双页模式:
在res目录下新建layout-sw600dp文件夹,在这个文件夹下新建一个 activity_main.xml文件,代码如下:
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal">
  6. <fragment
  7. android:id="@+id/news_title_fragment"
  8. android:name="com.pengpeng.onenumber_04fragmentbestpractice.fragment.NewsTitleFragment"
  9. android:layout_width="0dp"
  10. android:layout_height="match_parent"
  11. android:layout_weight="1"
  12. />
  13. <FrameLayout
  14. android:id="@+id/news_content_layout"
  15. android:layout_width="0dp"
  16. android:layout_height="match_parent"
  17. android:layout_weight="3">
  18. <fragment
  19. android:id="@+id/news_content_fragment"
  20. android:name="com.pengpeng.onenumber_04fragmentbestpractice.fragment.NewsContentFragment"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"/>
  23. </FrameLayout>
  24. </LinearLayout>
双页模式下同时引入了两个碎片并将新闻内容的碎片放在了FrameLayout下,而这个布局的id是news_content_layout因此能够找到这个id的时候就是双页模式,否则就是单页模式
NewsTitleFragment填充数据
   
   
  1. public class NewsTitleFragment extends Fragment {
  2. private boolean isTwoPane;//是否加载单双页模式
  3. @Nullable
  4. @Override
  5. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
  6. savedInstanceState) {
  7. View view = inflater.inflate(R.layout.news_title_frag, container, false);
  8. RecyclerView newsTitleRecyclerView = (RecyclerView) view.findViewById(R.id.news_title_recycler_view);
  9. LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
  10. newsTitleRecyclerView.setLayoutManager(layoutManager);
  11. NewsAdapter adapter = new NewsAdapter(getNews());
  12. newsTitleRecyclerView.setAdapter(adapter);
  13. return view;
  14. }
  15. /**
  16. * 填充数据
  17. *
  18. * @return
  19. */
  20. private List<News> getNews() {
  21. List<News> newsList = new ArrayList<News>();
  22. for (int i = 0; i < = 50; i++) {
  23. News news = new News();
  24. news.setTitle("This is news Title" + i);
  25. news.setContent(getRandomLengthContent("This is news content" + i + "."));
  26. newsList.add(news);
  27. }
  28. return newsList;
  29. }
  30. private String getRandomLengthContent(String content) {
  31. Random random = new Random();
  32. int length = random.nextInt(20) + 1;
  33. StringBuilder builder = new StringBuilder();
  34. for (int i = 0; i < length; i++) {
  35. builder.append(content);
  36. }
  37. return builder.toString();
  38. }
  39. @Override
  40. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  41. super.onActivityCreated(savedInstanceState);
  42. if (getActivity().findViewById(R.id.news_content_layout) != null) {
  43. isTwoPane = true;//可以找到news_content_layout布局时,为双页模式
  44. } else {
  45. isTwoPane = false;//找不到news_content_layout布局时,为单页模式
  46. }
  47. }
  48. class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
  49. private List<News> mNewsList;
  50. class ViewHolder extends RecyclerView.ViewHolder {
  51. TextView newsTitleText;
  52. public ViewHolder(View itemView) {
  53. super(itemView);
  54. newsTitleText = (TextView) itemView.findViewById(R.id.news_title);
  55. }
  56. }
  57. public NewsAdapter(List<News> newsList) {
  58. mNewsList = newsList;
  59. }
  60. @Override
  61. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  62. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
  63. final ViewHolder holder = new ViewHolder(view);
  64. view.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67. News news = mNewsList.get(holder.getAdapterPosition());
  68. if (isTwoPane) {
  69. //如果是双页模式,则刷新NewsContentFragment中的内容
  70. NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);//activity中用getSupportFragmentManager,其他地方可以用getFragmentManager
  71. newsContentFragment.refresh(news.getTitle(),
  72. news.getContent());
  73. } else {
  74. //如果是单页模式,则直接启动NewsContentActivity
  75. NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent());
  76. }
  77. }
  78. });
  79. return holder;
  80. }
  81. @Override
  82. public void onBindViewHolder(ViewHolder holder, int position) {
  83. News news = mNewsList.get(position);
  84. holder.newsTitleText.setText(news.getTitle());
  85. }
  86. @Override
  87. public int getItemCount() {
  88. return mNewsList.size();
  89. }
  90. }
  91. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值