Skip to content

Commit

Permalink
fragment hide show 方式使用
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Feb 5, 2018
1 parent 0941df0 commit 85ca64b
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,63 @@

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import necer.ncalendardemo.R;
import necer.ncalendardemo.fragment.TestFragment;
import necer.ncalendardemo.fragment.TestFragment1;
import necer.ncalendardemo.fragment.TestFragment2;

/**
* Created by necer on 2017/12/22.
*/

public class TestFragmentActivity extends AppCompatActivity {

private Fragment currentFragment ;
private Fragment fragment1;
private Fragment fragment2;




@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_fragment);
getSupportActionBar().hide();

fragment1 = new TestFragment1();
fragment2 = new TestFragment2();

currentFragment = fragment1;

getSupportFragmentManager().beginTransaction().add(R.id.fl_, fragment1).show(fragment1).commit();

getSupportFragmentManager().beginTransaction().replace(R.id.fl_, TestFragment.newInstance("","")).commit();
}

public void bt1(View view) {
showFragment(fragment1);
}

public void bt2(View view) {
showFragment(fragment2);
}

private void showFragment(Fragment fragment) {

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (currentFragment != fragment) {
fragmentTransaction.hide(currentFragment);
currentFragment = fragment;
if (!fragment.isAdded()) {
fragmentTransaction.add(R.id.fl_, fragment).show(fragment).commit();
} else {
fragmentTransaction.show(fragment).commit();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

weekCalendar = (WeekCalendar) findViewById(R.id.weekCalendar);



}

public void setDate(View view) {
weekCalendar.setVisibility(View.VISIBLE);
weekCalendar.post(new Runnable() {
@Override
public void run() {
weekCalendar.setDate("2018-01-01");
weekCalendar.setDate("2017-12-29");
}
});

Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/necer/ncalendardemo/fragment/TestFragment1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package necer.ncalendardemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.necer.ncalendar.calendar.MonthCalendar;
import com.necer.ncalendar.listener.OnMonthCalendarChangedListener;
import com.necer.ncalendar.utils.MyLog;

import org.joda.time.DateTime;

import necer.ncalendardemo.R;

/**
* Created by necer on 2018/2/5.
*/

public class TestFragment1 extends Fragment {


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


View inflate = inflater.inflate(R.layout.fragment_1, null);
MonthCalendar monthCalendar = (MonthCalendar) inflate.findViewById(R.id.monthcalendar);

monthCalendar.setOnMonthCalendarChangedListener(new OnMonthCalendarChangedListener() {
@Override
public void onMonthCalendarChanged(DateTime dateTime) {
MyLog.d("TestFragment1::;"+dateTime.toString());
}
});



return inflate;

}
}
44 changes: 44 additions & 0 deletions app/src/main/java/necer/ncalendardemo/fragment/TestFragment2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package necer.ncalendardemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.necer.ncalendar.calendar.MonthCalendar;
import com.necer.ncalendar.listener.OnMonthCalendarChangedListener;
import com.necer.ncalendar.utils.MyLog;

import org.joda.time.DateTime;

import necer.ncalendardemo.R;

/**
* Created by necer on 2018/2/5.
*/

public class TestFragment2 extends Fragment {


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View inflate = inflater.inflate(R.layout.fragment_2, null);
MonthCalendar monthCalendar = (MonthCalendar) inflate.findViewById(R.id.monthcalendar);

monthCalendar.setOnMonthCalendarChangedListener(new OnMonthCalendarChangedListener() {
@Override
public void onMonthCalendarChanged(DateTime dateTime) {
MyLog.d("TestFragment2::;"+dateTime.toString());
}
});



return inflate;

}
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_test_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1"
android:onClick="bt1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"
android:onClick="bt2"/>

</LinearLayout>


<FrameLayout
android:id="@+id/fl_"
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/layout/fragment_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="111111"
android:background="#aa88cc"
android:gravity="center"/>

<com.necer.ncalendar.calendar.MonthCalendar
android:id="@+id/monthcalendar"
android:layout_width="match_parent"
android:layout_height="300dp"/>



</LinearLayout>
19 changes: 19 additions & 0 deletions app/src/main/res/layout/fragment_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="222222"
android:background="#aa4081"
android:gravity="center"/>

<com.necer.ncalendar.calendar.MonthCalendar
android:id="@+id/monthcalendar"
android:layout_width="match_parent"
android:layout_height="300dp"/>

</LinearLayout>

0 comments on commit 85ca64b

Please sign in to comment.