I. Introduction
Yesterday, I completed the design of the loading page and the planet page, and learned Fragment to realize the bottom navigation bar.
Today, combining Cardview with sensors, the release table can be shaken with the phone. Learned the nesting of Fragment to switch between "My Post" and "My Reply" on my page. Difficulties encountered: conflicts between some controls or versions can be solved by referring to the data and modifying the package; Fragment does not have a good understanding, which leads to difficulties in nesting. Nesting is achieved by referring to the data and re-learning Fragment There is a problem with the UI page, I don't know how to go on.
Tomorrow beautify the login and registration page, and learn how to use the header bar.
2. Achievement display
Third, the code
SendActivity.java
package com.example.myteamapplication.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.WindowManager; import android.widget.FrameLayout; import androidx.appcompat.app.AppCompatActivity; import androidx.cardview.widget.CardView; import com.example.myteamapplication.R; public class SendActivity extends AppCompatActivity implementsSensorEventListener { private SensorManager sensorManager; private Sensor defaultSensor; private CardView cv; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate (savedInstanceState); // Remove the top title getSupportActionBar (). Hide (); // Remove the top time , Power, etc. getWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView (R.layout.activity_send); initView (); sensorManager= (SensorManager) getSystemService (SENSOR_SERVICE); // Get sensor management defaultSensor = sensorManager.getDefaultSensor (Sensor.TYPE_ORIENTATION); // Set type } @Override protected void onResume () { super .onResume (); sensorManager.registerListener ( this , defaultSensor, SensorManager.SENSOR_DELAY_GAME); // Register the sensor } @Override protected void onDestroy () { super .onDestroy (); sensorManager.unregisterListener ( this ); // Unregister the sensor } @Override public void onSensorChanged (SensorEvent event) { changeLocation (event.values [ 1], event.values [2 ]); } @Override public void onAccuracyChanged (Sensor sensor, int accuracy) { } private void changeLocation ( float y, float z) ( FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) cv.getLayoutParams (); layoutParams.setMargins (( int ) z * 5, ( int ) y * 5, 0, 0); // The effect of multiplying 2 is to Make the effect obvious cv.setLayoutParams(layoutParams); } private void initView() { cv = (CardView) findViewById(R.id.cv); } }
activity_send.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg"> <androidx.cardview.widget.CardView android:id="@+id/cv" android:layout_width="300dp" android:layout_height="400dp" android:layout_gravity="center" app:cardCornerRadius="10dp" app:contentPadding="20dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="请选择您发布内容的类别:" /> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="40dp" android:gravity="center"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吐槽"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="表白"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="交友"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="其他"/> </RadioGroup> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入您要发布的内容:" android:layout_marginTop="75dp"/> <EditText android:id="@+id/et_data_upass" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="80dp" /> </androidx.cardview.widget.CardView> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" app:srcCompat="@android:drawable/ic_input_add" android:layout_marginBottom="50dp"/> </FrameLayout>
Fragment_My.java
package com.example.myteamapplication.Fragment; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.RadioButton; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import com.example.myteamapplication.Activity.MainActivity; import com.example.myteamapplication.Activity.SendActivity; import com.example.myteamapplication.R; public class Fragment_My extends Fragment implements View.OnClickListener { private FragmentTransaction transaction; private FragmentManager manager; private RadioButton my_tab_send,my_tab_receive; private Context MainActivity; private LayoutInflater inflater; // @Override // public void onActivityCreated(Bundle savedInstanceState) { // super.onActivityCreated(savedInstanceState); // MainActivity = getActivity(); // inflater = LayoutInflater.from(getActivity()); // } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); manager = getChildFragmentManager(); transaction = manager.beginTransaction(); transaction.add(R.id.fragment_container_my,new Fragment_My_send()); transaction.commit(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_my, container, false); my_tab_send = rootView.findViewById(R.id.my_tab_send); my_tab_receive = rootView.findViewById(R.id.my_tab_receive); my_tab_send.setOnClickListener(this); my_tab_receive.setOnClickListener(this); return rootView; } @Override public void onClick(View v) { transaction = manager.beginTransaction(); switch (v.getId()){ case R.id.my_tab_send: transaction.replace(R.id.fragment_container_my,new Fragment_My_send()); break; case R.id.my_tab_receive: transaction.replace(R.id.fragment_container_my,new Fragment_My_receive()); break; } transaction.commit(); } }
fragment_my.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"> <FrameLayout android:id="@+id/fragment_container_my" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/ll" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ll" android:background="#dcdcdc" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户头像和id和用户名"/> <RadioGroup android:id="@+id/tabs_rg_my" android:layout_width="match_parent" android:layout_height="56dp" android:orientation="horizontal"> <RadioButton android:id="@+id/my_tab_send" style="@style/Custom.TabRadioButton" android:checked="true" android:drawableTop="@drawable/tab_sign_selector" android:text="我的发帖" /> <RadioButton android:id="@+id/my_tab_receive" style="@style/Custom.TabRadioButton" android:drawableTop="@drawable/tab_record_selector" android:text <//>= "My Reply" RadioGroup> </LinearLayout> </RelativeLayout>
4. Team Link Today