Databinding convert 简单应用 * list
<?xml version="1.0" encoding="utf-8"?> <layout 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"> <data> <import type="java.util.List"></import> <variable name="list" type="List<String>" /> <variable name="index" type="Integer" /> <variable name="user" type="com.example.work12_08.adapter.User" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity2"> <TextView android:textSize="30dp" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.name}"/> <TextView android:textSize="30dp" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{String.valueOf(user.age)}"/> <EditText android:layout_width="match_parent" android:layout_height="60dp" android:id="@+id/et"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{et.text}"/> /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{list.get(index)}"/> </LinearLayout> </layout>
mainBinding简单使用方法 以及BottomNavigationView 底部导航栏
布局
<?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"> <androidx.viewpager.widget.ViewPager android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" android:id="@+id/viewpager"/> <com.google.android.material.bottomnavigation.BottomNavigationView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#9C9898" app:menu="@menu/menu_nav" android:id="@+id/nav"/> </LinearLayout>
java
package com.example.work12_08; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.viewpager.widget.ViewPager; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.example.work12_08.adapter.MyFragmentAdapter; import com.example.work12_08.databinding.ActivityMainBinding; import com.google.android.material.navigation.NavigationBarView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { ActivityMainBinding mainBinding; private MyFragmentAdapter adapter; private List<Fragment> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); mainBinding = ActivityMainBinding.inflate(getLayoutInflater()); View view = mainBinding.getRoot(); setContentView(view); list.add(new HomeFragment()); list.add(new LikeFragment()); list.add(new mainFragment()); adapter = new MyFragmentAdapter(getSupportFragmentManager(),list); mainBinding.viewpager.setAdapter(adapter); mainBinding.viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { MenuItem item = mainBinding.nav.getMenu().getItem(position); item.setChecked(true); } @Override public void onPageScrollStateChanged(int state) { } }); mainBinding.nav.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.nav_home: mainBinding.viewpager.setCurrentItem(0); break; case R.id.nav_like: mainBinding.viewpager.setCurrentItem(1); break; case R.id.nav_nh: mainBinding.viewpager.setCurrentItem(2); break; } return true; } }); } }
Databinding convert 简单应用 * map
布局
<?xml version="1.0" encoding="utf-8"?> <layout 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"> <data> <import type="java.util.Map"/> <variable name="map" type="Map<String , String>" /> <variable name="key" type="String" /> <variable name="presenter" type="com.example.work12_08.Presenter" /> <variable name="userinfo" type="com.example.work12_08.UserInfo" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".LoginActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="25sp" android:textStyle="bold" android:text="@{map.get(key)}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="事件绑定" android:layout_gravity="center" android:textSize="25sp" android:textStyle="bold" android:onClick="@{()->presenter.onSave()}"/> <TextView android:layout_gravity="center" android:textSize="25sp" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{userinfo.name}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{presenter.firstName}" android:layout_gravity="center" android:textSize="25sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="观察者对象" android:layout_gravity="center" android:textSize="25sp" android:textStyle="bold" android:onClick="@{()->presenter.onClink(userinfo)}"/> </LinearLayout> </layout>
java
package com.example.work12_08; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import android.os.Bundle; import android.view.View; import com.example.work12_08.databinding.ActivityLoginBinding; import java.util.IdentityHashMap; import java.util.Map; public class LoginActivity extends AppCompatActivity { ActivityLoginBinding loginBinding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_login); loginBinding = DataBindingUtil.setContentView(this,R.layout.activity_login); Map<String,String> map_login = new IdentityHashMap<>(); map_login.put("111","傻呗胡豪哲"); map_login.put("222","123"); map_login.put("333","12.66"); loginBinding.setMap(map_login); loginBinding.setKey("333"); Presenter presenter = new Presenter("dsaddasda"); loginBinding.setPresenter(presenter); UserInfo pl = new UserInfo("牛掰", 20); loginBinding.setUserinfo(pl); } }
所得所获
这是非常的充实的一天,我们时间首先初步掌握了ActivityMainBinding 这个东西 它使我们解放部分代码 不在需要之前必须的findViewById() 可以省去很多麻烦 比如之前的因未获取id导致的空指针异常。
下午学的什么?对不起 脑子宕机中.....