我的第一行Android代码-Fragment

目录

1.Fragment

2.生命周期

3.两种加载方式

1.Fragment

当前的Activity中包含多个fragment

2.生命周期

Fragment并不能单独使用,需要嵌套在Activity 中使用,且生命周期会受到Activity的影响

3.两种加载方式

1.静态加载fragment

  • 定义自己的fragment继承Fragment
  • 定义自己的fragment布局文件
  • 在需要启动的activity中添加fragment标签
  • 在需要启动的activity中编写代码
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class ListFragment : Fragment() {
    lateinit var argments: Bundle

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view=inflater.inflate(R.layout.fragment_list,container,false)
        return view
    }
}

定义布局文件fragment_list

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="153dp"
        android:layout_height="81dp"
        android:text="List Fragment"
        tools:layout_editor_absoluteX="129dp"
        tools:layout_editor_absoluteY="218dp"
        tools:ignore="MissingConstraints" />


</androidx.constraintlayout.widget.ConstraintLayout>

在需要加载Fragment的Activity对应的布局文件中添加fragment的标签

 <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fa"
        android:name="com.example.xiaoxiaole.ListFragment"
        />

 

 

2.动态加载fragment

自己的fragment文件和布局文件和上面是一样的,主要不一样的是activity_main.xml文件,在这里是添加了FrameLayout标签

<?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">

    <!--    android:background="@color/purple_200"设置文本的背景色
             android:gravity="center_horizontal"       // 文本中的文字对齐方式
             android:paddingTop="100dp"                // 文本的上边内间距
             android:text="layout_gravity:center"      // 现实的文本内容
             android:textSize="30dp" />                // 文本字号大小-->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity代码: 

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val listFragment =  ListFragment()
        val bundle = Bundle()

        //bundle.putInt("key_int",100)//通过bundle传递数据
        listFragment.argments= bundle
        val ft = supportFragmentManager.beginTransaction()
        ft.replace(R.id.container, listFragment)
        //ft.show(listFragment)
        ft.commit()

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我们可以按照以下步骤实现一个简易版的新闻应用: 1. 创建一个新的 Android 项目,并在 MainActivity 中添加用于显示 Fragment 的布局: ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } ``` ```xml <!-- activity_main.xml --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 创建 NewsFragment 类,并在其中添加用于显示新闻列表的 RecyclerView: ```java public class NewsFragment extends Fragment { private RecyclerView mRecyclerView; private NewsAdapter mAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_news, container, false); mRecyclerView = view.findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); mAdapter = new NewsAdapter(); mRecyclerView.setAdapter(mAdapter); return view; } private class NewsHolder extends RecyclerView.ViewHolder { private TextView mTitleTextView; private TextView mDateTextView; public NewsHolder(LayoutInflater inflater, ViewGroup parent) { super(inflater.inflate(R.layout.list_item_news, parent, false)); mTitleTextView = itemView.findViewById(R.id.news_title); mDateTextView = itemView.findViewById(R.id.news_date); } public void bind(News news) { mTitleTextView.setText(news.getTitle()); mDateTextView.setText(news.getDate()); } } private class NewsAdapter extends RecyclerView.Adapter<NewsHolder> { private List<News> mNewsList = new ArrayList<>(); public void setNewsList(List<News> newsList) { mNewsList = newsList; } @Override public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); return new NewsHolder(layoutInflater, parent); } @Override public void onBindViewHolder(NewsHolder holder, int position) { News news = mNewsList.get(position); holder.bind(news); } @Override public int getItemCount() { return mNewsList.size(); } } } ``` ```xml <!-- fragment_news.xml --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` ```xml <!-- list_item_news.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/news_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:textAppearanceLarge" /> <TextView android:id="@+id/news_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:textAppearanceSmall" /> </LinearLayout> ``` 3. 创建 News 类,用于保存新闻的标题和日期: ```java public class News { private String mTitle; private String mDate; public News(String title, String date) { mTitle = title; mDate = date; } public String getTitle() { return mTitle; } public String getDate() { return mDate; } } ``` 4. 在 MainActivity 中添加用于显示 NewsFragment代码: ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.container); if (fragment == null) { fragment = new NewsFragment(); fm.beginTransaction() .add(R.id.container, fragment) .commit(); } } } ``` 5. 在 NewsFragment 中添加一个用于生成假数据的方法,并在 onCreateView 方法中调用它: ```java public class NewsFragment extends Fragment { // ... private void generateNewsList() { List<News> newsList = new ArrayList<>(); newsList.add(new News("Title 1", "2021-01-01")); newsList.add(new News("Title 2", "2021-01-02")); newsList.add(new News("Title 3", "2021-01-03")); newsList.add(new News("Title 4", "2021-01-04")); newsList.add(new News("Title 5", "2021-01-05")); mAdapter.setNewsList(newsList); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_news, container, false); mRecyclerView = view.findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); mAdapter = new NewsAdapter(); mRecyclerView.setAdapter(mAdapter); generateNewsList(); return view; } // ... } ``` 现在,我们可以运行应用程序,看到一个简单的新闻列表。这只是一个很简单的例子,但它展示了如何使用 Fragment 和 RecyclerView 构建一个 Android 应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值