鱼C论坛

 找回密码
 立即注册
查看: 1741|回复: 0

[学习笔记] android 编程 4.5

[复制链接]
发表于 2017-10-6 12:43:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
注:以下新建文件所有.java的全在mainactivity.java旁边,xml文件全在layout文件夹下。
先在gradle里添加依赖
  1. compile 'com.android.support:recyclerview-v7:26.+'
复制代码

同步以后再新建个储存数据的类News.java
  1. package com.example.xinwei.fragmentbestpractise;

  2. /**
  3. * Created by xinwei on 2017/10/6.
  4. */

  5. public class News {
  6.     public String getTitle() {
  7.         return title;
  8.     }

  9.     public String getContent() {
  10.         return content;
  11.     }

  12.     public void setTitle(String title) {
  13.         this.title = title;
  14.     }

  15.     public void setContent(String content) {
  16.         this.content = content;
  17.     }

  18.     private String title;
  19.     private String content;
  20.    
  21. }
复制代码

然后新建个布局文件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:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     android:id="@+id/visibility_layout"
  9.     android:orientation="vertical"
  10.     android:visibility="invisible">
  11.     <TextView
  12.         android:id="@+id/news_title"
  13.         android:gravity="center"
  14.         android:padding="10dp"
  15.         android:textSize="20sp"
  16.         android:layout_width="match_parent"
  17.         android:layout_height="wrap_content" />

  18.     <View
  19.         android:layout_width="match_parent"
  20.         android:layout_height="1dp"
  21.         android:background="#000" />
  22.     <TextView
  23.         android:id="@+id/news_content"
  24.         android:padding="15dp"
  25.         android:textSize="18sp"
  26.         android:layout_width="match_parent"
  27.         android:layout_height="0dp"
  28.         android:layout_weight="1"/>
  29. </LinearLayout>

  30.     <View
  31.         android:layout_width="1dp"
  32.         android:layout_height="match_parent"
  33.         android:layout_alignParentLeft="true"
  34.         android:background="#000" />
  35. </RelativeLayout>
复制代码

再新建个NewsContentFragment.java文件
  1. package com.example.xinwei.fragmentbestpractise;

  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.TextView;

  9. /**
  10. * Created by xinwei on 2017/10/6.
  11. */

  12. public class NewsContentFragment extends Fragment {
  13.     private View view;

  14.     @Nullable
  15.     @Override
  16.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  17.         view = inflater.inflate(R.layout.news_content_frag,container,false);
  18.         return view;
  19.     }
  20.     public void refresh(String newsTitle, String newsContent){
  21.         View visibilityLayout=view.findViewById(R.id.visibility_layout);
  22.         visibilityLayout.setVisibility(View.VISIBLE);
  23.         TextView newsTitleText=view.findViewById(R.id.news_title);
  24.         TextView newsContentText=view.findViewById(R.id.news_content);
  25.         newsTitleText.setText(newsTitle);
  26.         newsContentText.setText(newsContent);
  27.     }
  28. }
复制代码

然后新建个NewsContentActivity.java的活动,把自动创建的布局名设置为news_content,把news_content.xml文件修改为
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     >

  7.     <fragment
  8.         android:layout_width="match_parent"
  9.         android:layout_height="match_parent"
  10.         android:id="@+id/news_content_fragment"
  11.         android:name="com.example.xinwei.fragmentbestpractise.NewsContentFragment" />
  12. </LinearLayout>
复制代码

然后修改NewsContentActivity.java
  1. package com.example.xinwei.fragmentbestpractise;

  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;

  6. public class NewsContentActivity extends AppCompatActivity {

  7.     public static void actionStart(Context context,String newsTitle,String newsContent){
  8.         Intent intent = new Intent(context,NewsContentActivity.class);
  9.         intent.putExtra("news_title",newsTitle);
  10.         intent.putExtra("news_content",newsContent);
  11.         context.startActivity(intent);
  12.     }
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.news_content);
  17.         String newsTitle=getIntent().getStringExtra("news_title");
  18.         String newsContent=getIntent().getStringExtra("news_content");
  19.         NewsContentFragment newsContentFragment=(NewsContentFragment)getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
  20.         newsContentFragment.refresh(newsTitle,newsContent);
  21.     }
  22. }
复制代码

继续创建个布局文件news_title_frag.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:orientation="vertical" android:layout_width="match_parent"
  3.     android:layout_height="match_parent">

  4.     <android.support.v7.widget.RecyclerView
  5.         android:layout_width="match_parent"
  6.         android:layout_height="match_parent"
  7.         android:id="@+id/news_title_recycler_view"/>
  8. </LinearLayout>
复制代码

在新建个news_item.xml布局文件作为recyclerveiw的子项
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="wrap_content"
  5.     android:id="@+id/news_title"
  6.     android:singleLine="true"
  7.     android:ellipsize="end"
  8.     android:textSize="18sp"
  9.     android:paddingLeft="10dp"
  10.     android:paddingRight="10dp"
  11.     android:paddingTop="15dp"
  12.     android:paddingBottom="15dp" />
复制代码

然后再新建个NewsTitleFragment.java文件
  1. package com.example.xinwei.fragmentbestpractise;

  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;

  8. /**
  9. * Created by xinwei on 2017/10/6.
  10. */

  11. public class NewsTitleFragment extends Fragment {
  12.     private boolean isTwoPane;

  13.     @Override
  14.     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  15.         super.onActivityCreated(savedInstanceState);
  16.         if(getActivity().findViewById(R.id.news_content_layout)!=null){
  17.             isTwoPane=true;
  18.         }else{
  19.             isTwoPane=false;
  20.         }
  21.     }

  22.     @Nullable
  23.     @Override
  24.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  25.         View view=inflater.inflate(R.layout.news_title_frag,container,false);
  26.         return view;
  27.     }
  28. }
复制代码

修改activity_main.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/news_title_layout"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     >

  7.     <fragment
  8.         android:id="@+id/news_title_fragment"
  9.         android:name="com.example.xinwei.fragmentbestpractise.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:orientation="horizontal" android:layout_width="match_parent"
  4.     android:layout_height="match_parent">

  5.     <fragment
  6.         android:id="@+id/news_title_fragment"
  7.         android:name="com.example.xinwei.fragmentbestpractise.NewsTitleFragment"
  8.         android:layout_width="0dp"
  9.         android:layout_weight="1"
  10.         android:layout_height="match_parent" />
  11.     <FrameLayout
  12.         android:id="@+id/news_content_layout"
  13.         android:layout_width="0dp"
  14.         android:layout_weight="3"
  15.         android:layout_height="match_parent">

  16.         <fragment
  17.             android:id="@+id/news_content_fragment"
  18.             android:name="com.example.xinwei.fragmentbestpractise.NewsContentFragment"
  19.             android:layout_width="match_parent"
  20.             android:layout_height="match_parent" />
  21.     </FrameLayout>
  22. </LinearLayout>
复制代码

然后再修改NewsTitleFragment.java文件
  1. package com.example.xinwei.fragmentbestpractise;

  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v7.widget.LinearLayoutManager;
  6. import android.support.v7.widget.RecyclerView;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.TextView;

  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Random;

  14. /**
  15. * Created by xinwei on 2017/10/6.
  16. */

  17. public class NewsTitleFragment extends Fragment {
  18.     private boolean isTwoPane;

  19.     @Override
  20.     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  21.         super.onActivityCreated(savedInstanceState);
  22.         if(getActivity().findViewById(R.id.news_content_layout)!=null){
  23.             isTwoPane=true;
  24.         }else{
  25.             isTwoPane=false;
  26.         }
  27.     }

  28.     @Nullable
  29.     @Override
  30.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  31.         View view=inflater.inflate(R.layout.news_title_frag,container,false);
  32.         RecyclerView newsTitleRecyclerView=(RecyclerView)view.findViewById(R.id.news_title_recycler_view);
  33.         LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
  34.         newsTitleRecyclerView.setLayoutManager(layoutManager);
  35.         NewsAdapter newsAdapter=new NewsAdapter(getNews());
  36.         newsTitleRecyclerView.setAdapter(newsAdapter);
  37.         return view;
  38.     }
  39.     private List<News> getNews(){
  40.         List<News> newsList=new ArrayList<>();
  41.         for(int i=1;i<=50;i++){
  42.             News news=new News();
  43.             news.setTitle("This is news title"+i);
  44.             news.setContent(getRandomLengthContent("This is news content"+i+"."));
  45.             newsList.add(news);
  46.         }
  47.         return newsList;
  48.     }
  49.     private String getRandomLengthContent(String content){
  50.         Random random=new Random();
  51.         int length=random.nextInt(20)+1;
  52.         StringBuilder builder=new StringBuilder();
  53.         for(int i=0;i<length;i++){
  54.             builder.append(content);
  55.         }
  56.         return builder.toString();
  57.     }
  58.     class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{
  59.         private List<News> mNewsList;
  60.         class ViewHolder extends RecyclerView.ViewHolder{
  61.             TextView newsTitleText;
  62.             public ViewHolder(View itemView) {
  63.                 super(itemView);
  64.                 newsTitleText=itemView.findViewById(R.id.news_title);
  65.             }
  66.         }
  67.         public NewsAdapter(List<News> newsList){
  68.             mNewsList=newsList;
  69.         }
  70.         @Override
  71.         public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  72.             View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item,parent,false);
  73.             final ViewHolder holder = new ViewHolder(view);
  74.             view.setOnClickListener(new View.OnClickListener() {
  75.                 @Override
  76.                 public void onClick(View view) {
  77.                     News news=mNewsList.get(holder.getAdapterPosition());
  78.                     if(isTwoPane){
  79.                         NewsContentFragment newsContentFragment=(NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);
  80.                         newsContentFragment.refresh(news.getTitle(),news.getContent());
  81.                     }else{
  82.                         NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
  83.                     }
  84.                 }
  85.             });
  86.             return holder;
  87.         }

  88.         @Override
  89.         public void onBindViewHolder(NewsAdapter.ViewHolder holder, int position) {
  90.             News news=mNewsList.get(position);
  91.             holder.newsTitleText.setText(news.getTitle());
  92.         }

  93.         @Override
  94.         public int getItemCount() {
  95.             return mNewsList.size();
  96.         }
  97.     }
  98. }
复制代码

哎!终于写完了,这个其实不用自己写,太麻烦了,我就写了一上午。只要大概看懂了什么意思就行了,我觉得主要是理解。效果图为:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 02:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表