kotlin关于主页Fragment(二)

这篇博客介绍了如何使用Kotlin创建一个简单的主页Fragment,包括主界面和三个子Fragment的展示。博主分享了代码实现,并提供了Java版本的代码作为参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.说明:

这是一个kotlin写的,极简的主页fragmrent效果,非常的简洁,因此记录下来,需要完整代码的请留言,还缺少一些资源文件,懒得贴,你自己去别处找找也能补全的,底部栏

BottomNavigationView

2..效果图

2.主界面

package com.ufi.pdioms.ztkotlin

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.fragment.app.FragmentActivity
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : FragmentActivity() {

    lateinit var bottomNavigationView: BottomNavigationView

    /**
     *  companion object 的定义完全属于类的本身,所以
     *  companion object 肯定是不能脱离类而定义在全局之中。
     *  它类似 Java 里的 static 变量
     */
    companion object{
        var selectedId:Int = 10
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bottomNavigationView = findViewById(R.id.bottom_nav)
        bottomNavigationView.selectedItemId = R.id.action_pokemon
        setBottom(bottomNavigationView.selectedItemId)
        bottomNavigationView.setOnNavigationItemSelectedListener { item: MenuItem ->
            setBottom(item.itemId)


        }

    }

    private fun setBottom(itemId: Int): Boolean {
        if (selectedId == itemId){
            return true
        }
        when(itemId){
            R.id.action_pokemon->{
                tvTitle.text="Pokemon"
                val fragment = ListPokemonFragment()
                supportFragmentManager.beginTransaction().replace(R.id.frLayout,fragment,fragment.javaClass.simpleName).commit()
                return true
            }
            R.id.action_disk->{
                tvTitle.text = "Movie"
                val fragmentlist = ListMovielPokemonFragment()
                supportFragmentManager.beginTransaction().replace(R.id.frLayout,fragmentlist,fragmentlist.javaClass.simpleName).commit()
                return true
            }
            R.id.action_itens->{
                tvTitle.text = "Items"
                val fragmentitems = ItemsPokemonFragment()
                supportFragmentManager.beginTransaction().replace(R.id.frLayout,fragmentitems,fragmentitems.javaClass.simpleName).commit()
                return true
            }
        }
        selectedId = itemId
        return false
    }


}

3.主界面ui:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="TextView" />


    <FrameLayout
        android:id="@+id/frLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/margin_small_xxmin"
        android:layout_marginBottom="@dimen/margin_small_xxmin"
        android:layout_weight="1"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:ignore="UselessLeaf" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size_56"
        android:fitsSystemWindows="true"
        android:visibility="visible"
        app:itemBackground="@color/colorBackgroundNavigation"
        app:itemIconTint="@color/text"
        app:itemTextAppearance="@style/TextStyleBNV"
        app:itemTextColor="@color/text"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_view" />
</LinearLayout>

4.fragment子页面:三个fragment,都是这种内容,仅仅是文件名字不同

package com.ufi.pdioms.ztkotlin

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

/**
Created by wrs on 23/12/2019,下午 12:50
projectName: ZTKotlin
packageName: com.ufi.pdioms.ztkotlin
 */

public class ItemsPokemonFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_poke,null)
    }

}

附上-java版本代码:

package com.example.administrator.testz;


import android.os.Bundle;
import android.view.MenuItem;


import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;


public class MainActivity extends AppCompatActivity {

    private ActionBar toolbar;
    CoordinatorLayout mCoordinatorLayoutl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = getSupportActionBar();
        loadFragment(new HomeFragment());
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        toolbar.setTitle("Jadwal");
    }

    private boolean loadFragment(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.frame_container, fragment)
                    .commit();
            return true;
        }
        return false;
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment = null;
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    toolbar.setTitle("Jadwal");
                    fragment = new HomeFragment();
                    break;
                case R.id.navigation_account:
                    toolbar.setTitle("Mata Kuliah");
                    fragment = new AccountFragment();
                    break;
                case R.id.navigation_notifications:
                    toolbar.setTitle("Dosen");
                    fragment = new NotifyFragment();
                    break;
            }
            return loadFragment(fragment);
        }
    };
}

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值