-
- 根据创建 navigation 类型项目
在res/layout目录节点上“右键”->New->XML->layout XML file.命名布局方名为fr_settings,root tag设置为ContraintLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
创建Fragment类
新建类settings,基类为Fragment:
加入构造函数:
package com.hhiot.hhvideo.ui.settings; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import com.hhiot.hhvideo.R; public class settings extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fr_settings, container, false); return root; } }
在构造函数中加载对应的layout(fr_settings)
打开res/navigation/mobile_navigation.xml, 加入一个新条目:
<fragment android:id="@+id/nav_Settings" android:name="com.hhiot.hhvideo.ui.settings.settings" android:label="参数设置" tools:layout="@layout/fr_settings" />
注意,此处设置了4个参数:
Id:新的fragment的ID;
Name:对应的Fragment的类名;
Label:将来显示在标题栏上的文字;
Layout:对应的布局文件的名字
打开文件res/menu/bottom_nav_menu.xml.新增加一个菜单条目(底部显示的按钮):
<item android:id="@+id/nav_Settings" android:icon="@drawable/ic_notifications_black_24dp" android:title="参数设置" />
这里的ID与res/navigation/mobile_navigation.xml中设置的ID一致。
-
- 在MainActivity中加入引用
在onCreate中加入新的节点的ID:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView navView = findViewById(R.id.nav_view); // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.nav_VideoList, R.id.nav_VideoLive,R.id.nav_Desktop, R.id.nav_Settings, R.id.nav_Account) .build(); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupWithNavController(navView, navController); }
通过http获取数据,反序列化成List<Map<String, String>>对象。向主线程发送消息。
void getDatas( ){ try{ commHttpUtils.doGetAsyn(dateUrl, new CallBack() { @Override public void onRequestComplete(String result) { List<Map<String, String>> items = new ArrayList<Map<String, String>>(); items = ( new Gson()).fromJson(result , items.getClass()); if( msgHandler!=null){ try{ Message msg = new Message(); msg.what = 1000; msg.obj = items; msgHandler.sendMessage(msg); } catch (Exception eer){ Log.e(TAG, "onRequestComplete: "+ eer.getMessage() ); } } } }); } catch(Exception er){ Log.e("videoList error.", er.getMessage() ); } }