• 碎片的实例


    1.添加依赖因为要用到RecyclerView的控件

    2类--.建立新闻的类(两个字段(title和content))

    public class News {
    private String title;
    private String content;
    public String getTitle()
    {
    return title;
    }
    public void setTitle(String title)
    {
    this.title=title;
    }
    public String getContent()
    {
    return content;
    }
    public void setContent(String content)
    {
    this.content=content;
    }
    }
    3.布局--新建新闻内容的布局
    布局有两块(标题(TextView)和内容(TextView)中间用一个View隔开(装一条线))
    4.类--新建NewContentFrament类继承Frament
    public class NewsContentFragment extends Fragment {
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState)
    {
    view=inflater.inflate(R.layout.news_content_frag,container,false);
    return view;
    }//先找到刚刚建立的布局中的View的界面返回赋给之前定义好的View
    public void refresh(String newsTitle,String newsContent)
    {
    View visibilityLayout=view.findViewById(R.id.visibility_layout);
    visibilityLayout.setVisibility(View.VISIBLE);
    TextView newsTitleText=(TextView)view.findViewById(R.id.new_title);
    TextView newsContentText=(TextView) view.findViewById(R.id.new_content);
    newsTitleText.setText(newsTitle);//刷新新闻的标题
    newsContentText.setText(newsContent);//刷新内容
    }//从View中获得各种需要使用的信息包括,标题和内容
    }
    以上完成双页模式显示
    5.布局--新建一个新活动(NewsContentActivity相应为产生的布局添加一个碎片)
    <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/news_content_fragment"
    android:name="com.example.fragmentbestpractice.NewsContentFragment"//将刚刚制作的布局引进来
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
    </LinearLayout>
    6.修改最开始那个活动的代码
    public class NewsContentActivity extends AppCompatActivity {
    public static void actionStart(Context context, String newsTitle,String newsContent)
    {
    Intent intent=new Intent(context,NewsContentActivity.class);//定义活动传进新闻活动标题和内容
    intent.putExtra("news_title",newsTitle);
    intent.putExtra("newsContent",newsContent);
    context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news_content);
    String newsTitle=getIntent().getStringExtra("news_title");
    String newContent=getIntent().getStringExtra("news_content");
    NewsContentFragment newsContentFragment=(NewsContentFragment)getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
    newsContentFragment.refresh(newsTitle,newContent);
    }
    }
    7.布局--新建新闻列表的布局此处要用到recyclerView的列表布局
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
    android:id="@+id/new_title_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
    </LinearLayout>
    8.布局---新建RecyclerView子项的布局
    <TextureView xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    />
    9.类--新建新闻列表的碎片里面放新闻列表
    public class NewsTitleFragment extends Fragment {
    private boolean isTwoPane;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState)
    {
    View view=inflater.inflate(R.layout.news_title_frag,container,false);
    return view;
    }//传进来布局
    public void onActivityCreated(Bundle saveInstanceState)
    {
    super.onActivityCreated(saveInstanceState);
    if(getActivity().findViewById(R.id.news_content_fragment)!=null)
    {
    isTwoPane=true;
    }
    else
    {
    isTwoPane=false;
    }
    }
    10.新建一个主活动的布局//两个碎片
    <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/news_title_fragment"//碎片一
    android:name="com.example.fragmentbestpractice.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.example.fragmentbestpractice.NewsContentFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

    </FrameLayout>>
    </LinearLayout>
    11.在NewsTitleFragment中通过Recycler将新闻列表展示处理来,需要在其内部放一个适配器,就在里面定义
    class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
    private List<News> mNewList;
    class ViewHolder extends RecyclerView.ViewHolder
    {
    TextView newsTitleText;
    public ViewHolder(View view)
    {
    super(view);
    newsTitleText=(TextView) view.findViewById(R.id.news_title);
    }
    }
    public NewsAdapter(List<News> newsList)
    {
    mNewList=newsList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType)
    {
    View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item,parent,false);
    final ViewHolder holder=new ViewHolder(view);
    view.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onCLick(View v)
    {
    News news=mNewList.get(holder.getAdapterPosition());
    if(isTwoPane)
    {
    NewsContentFragment newsContentFragment=(NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
    newsContentFragment.refresh(news.getTitle(),news.getContent());
    }
    else
    {
    NewsContentActivity.actionStart(getActivity(), news.getTitle(),news.getContent());
    }
    }

    });
    return holder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder,int position)
    {
    News news=mNewList.get(position);
    holder.newsTitleText.setText(news.getTitle());
    }
    @Override
    public int getItemCount()
    {
    return mNewList.size();
    }
    }
    }
    最后向RecycleView中填充数据修改NewsTitleFragement中代码
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState)
    {
    View view=inflater.inflate(R.layout.news_title_frag,container,false);
    RecyclerView newsTitleRecyclerView=(RecyclerView) view.findViewById(R.id.new_title_recycler_view);
    LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
    newsTitleRecyclerView.setLayoutManager(layoutManager);
    NewsAdapter adapter=new NewsAdapter(getNews());
    newsTitleRecyclerView.setAdapter(adapter);
    return view;
    }
    private List<News> getNews()
    {
    List<News> newsList=new ArrayList<>();
    for(int i=1;i<=50;i++)
    {
    News news=new News();
    news.setTitle("This is news title"+i);
    news.setContent(getRandomLengthContent("This is news content"+""));
    newsList.add(news);
    }
    return newsList;
    }
    public String getRandomLengthContent(String content)
    {
    Random random=new Random();
    int length=random.nextInt(20)+1;
    StringBuilder bulider=new StringBuilder();
    for (int i=0;i<length;i++)
    {
    bulider.append(content);
    }
    return bulider.toString();
    }







  • 相关阅读:
    diango-tinymce富文本编译器
    django 1.10以上版本,引入js
    linux中使用vi 打开文件时,能显示行号
    ubuntu 16.04 系统语言汉化
    ubuntu16.04 一些简单软件安装操作
    urllib -- ProxyHandler处理器(代理设置)
    urllib基本使用-Handler和自定义的opener()
    urllib基本使用 urlopen(),Request
    python3
    Ubuntu安装Mysql+Django+MySQLdb
  • 原文地址:https://www.cnblogs.com/1521681359qqcom/p/10078389.html
Copyright © 2020-2023  润新知