@ -12,17 +12,20 @@ import androidx.lifecycle.ViewModelProvider;
@@ -12,17 +12,20 @@ import androidx.lifecycle.ViewModelProvider;
import cc.niushuai.dididone.R ;
import cc.niushuai.dididone.databinding.FragmentStatisticBinding ;
import cc.niushuai.dididone.ui.base.BaseFragment ;
import cc.niushuai.dididone.ui.statistic.charts.ListFragment ;
import cc.niushuai.dididone.ui.statistic.charts.MonthFragment ;
import cc.niushuai.dididone.ui.statistic.charts.OtherFragment ;
import cc.niushuai.dididone.ui.statistic.charts.WeekFragment ;
import cc.niushuai.dididone.util.XLog ;
public class StatisticFragment extends BaseFragment implements View . OnClickListener {
private FragmentStatisticBinding binding ;
//三个fragment
private MyFragment f1 ;
private MyFragment f2 ;
private MyFragment f3 ;
private MyFragment f4 ;
private ListFragment listFragment ;
private WeekFragment weekFragment ;
private MonthFragment monthFragment ;
private OtherFragment otherFragment ;
public View onCreateView ( @NonNull LayoutInflater inflater ,
ViewGroup container , Bundle savedInstanceState ) {
@ -38,15 +41,15 @@ public class StatisticFragment extends BaseFragment implements View.OnClickListe
@@ -38,15 +41,15 @@ public class StatisticFragment extends BaseFragment implements View.OnClickListe
@Override
public void init ( ) {
initFragment1 ( ) ;
initList Fragment ( ) ;
}
@Override
public void setListeners ( ) {
binding . btn1 . setOnClickListener ( this ) ;
binding . btn2 . setOnClickListener ( this ) ;
binding . btn3 . setOnClickListener ( this ) ;
binding . btn4 . setOnClickListener ( this ) ;
binding . statButtonList . setOnClickListener ( this ) ;
binding . statButtonWeek . setOnClickListener ( this ) ;
binding . statButtonMonth . setOnClickListener ( this ) ;
binding . statButtonOther . setOnClickListener ( this ) ;
}
@Override
@ -55,114 +58,96 @@ public class StatisticFragment extends BaseFragment implements View.OnClickListe
@@ -55,114 +58,96 @@ public class StatisticFragment extends BaseFragment implements View.OnClickListe
binding = null ;
}
//显示第一个fragment
private void initFragment1 ( ) {
//开启事务,fragment的控制是由事务来实现的
/ * *
* list维度
* /
private void initListFragment ( ) {
FragmentTransaction transaction = getActivity ( ) . getSupportFragmentManager ( ) . beginTransaction ( ) ;
//第一种方式(add),初始化fragment并添加到事务中,如果为null就new一个
if ( f1 = = null ) {
f1 = new MyFragment ( "消息" ) ;
transaction . add ( R . id . main_frame_layout , f1 ) ;
if ( listFragment = = null ) {
listFragment = new ListFragment ( ) ;
transaction . add ( R . id . stat_main_frame_layout , listFragment ) ;
}
//隐藏所有fragment
hideFragment ( transaction ) ;
//显示需要显示的fragment
transaction . show ( f1 ) ;
transaction . show ( listFragment ) ;
//第二种方式(replace),初始化fragment
// if(f1 == null){
// f1 = new MyFragment("消息");
// }
// transaction.replace(R.id.main_frame_layout, f1);
//提交事务
transaction . commit ( ) ;
}
//显示第二个fragment
private void initFragment2 ( ) {
/ * *
* 周维度
* /
private void initWeekFragment ( ) {
FragmentTransaction transaction = getActivity ( ) . getSupportFragmentManager ( ) . beginTransaction ( ) ;
if ( f2 = = null ) {
f2 = new MyFragment ( "联系人" ) ;
transaction . add ( R . id . main_frame_layout , f2 ) ;
if ( weekFragment = = null ) {
weekFragment = new WeekFragment ( ) ;
transaction . add ( R . id . stat_ main_frame_layout, weekFragment ) ;
}
hideFragment ( transaction ) ;
transaction . show ( f2 ) ;
// if(f2 == null) {
// f2 = new MyFragment("联系人");
// }
// transaction.replace(R.id.main_frame_layout, f2);
transaction . show ( weekFragment ) ;
transaction . commit ( ) ;
}
//显示第三个fragment
private void initFragment3 ( ) {
/ * *
* 月维度视图
* /
private void initMonthFragment ( ) {
FragmentTransaction transaction = getActivity ( ) . getSupportFragmentManager ( ) . beginTransaction ( ) ;
if ( f3 = = null ) {
f3 = new MyFragment ( "动态" ) ;
transaction . add ( R . id . main_frame_layout , f3 ) ;
if ( monthFragment = = null ) {
monthFragment = new MonthFragment ( ) ;
transaction . add ( R . id . stat_ main_frame_layout, monthFragment ) ;
}
hideFragment ( transaction ) ;
transaction . show ( f3 ) ;
// if(f3 == null) {
// f3 = new MyFragment("动态");
// }
// transaction.replace(R.id.main_frame_layout, f3);
transaction . show ( monthFragment ) ;
transaction . commit ( ) ;
}
//显示第三个fragment
private void initFragment4 ( ) {
/ * *
* 其他
* /
private void initOtherFragment ( ) {
FragmentTransaction transaction = getActivity ( ) . getSupportFragmentManager ( ) . beginTransaction ( ) ;
if ( f4 = = null ) {
f4 = new MyFragment ( "其他" ) ;
transaction . add ( R . id . main_frame_layout , f4 ) ;
if ( otherFragment = = null ) {
otherFragment = new OtherFragment ( ) ;
transaction . add ( R . id . stat_ main_frame_layout, otherFragment ) ;
}
hideFragment ( transaction ) ;
transaction . show ( f4 ) ;
// if(f3 == null) {
// f3 = new MyFragment("动态");
// }
// transaction.replace(R.id.main_frame_layout, f3);
transaction . show ( otherFragment ) ;
transaction . commit ( ) ;
}
//隐藏所有的fragment
private void hideFragment ( FragmentTransaction transaction ) {
if ( f1 ! = null ) {
transaction . hide ( f1 ) ;
if ( listFragment ! = null ) {
transaction . hide ( listFragment ) ;
}
if ( f2 ! = null ) {
transaction . hide ( f2 ) ;
if ( weekFragment ! = null ) {
transaction . hide ( weekFragment ) ;
}
if ( f3 ! = null ) {
transaction . hide ( f3 ) ;
if ( monthFragment ! = null ) {
transaction . hide ( monthFragment ) ;
}
if ( f4 ! = null ) {
transaction . hide ( f4 ) ;
if ( otherFragment ! = null ) {
transaction . hide ( otherFragment ) ;
}
}
@Override
public void onClick ( View v ) {
if ( v = = binding . btn1 ) {
initFragment1 ( ) ;
} else if ( v = = binding . btn2 ) {
initFragment2 ( ) ;
} else if ( v = = binding . btn3 ) {
initFragment3 ( ) ;
} else if ( v = = binding . btn4 ) {
initFragment4 ( ) ;
if ( v = = binding . statButtonList ) {
initList Fragment ( ) ;
} else if ( v = = binding . statButtonWeek ) {
initWeek Fragment ( ) ;
} else if ( v = = binding . statButtonMonth ) {
initMonth Fragment ( ) ;
} else if ( v = = binding . statButtonOther ) {
initOther Fragment ( ) ;
}
}
}