Android - 在活动片段上刷卡刷新

时间:2017-03-20 07:36:52

标签: java android android-fragments

MainActivity

我在MainActivity中有一个片段

菜单上的

,有一个刷新按钮和刷卡刷新选项,调用myUpdateOperation()方法

根据用户的选择,此片段可能会有所不同。

如何在调用刷新操作时确保刷新当前活动的正确片段?

  

content_main.xml

values
  

MainActivity.java

<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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.app.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/main_fragment"
            android:name="com.example.app.main_fragment.MainFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="7"
            tools:layout="@layout/fragment_main" />

    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>
  

Fragment1.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);

    mSwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    Log.i(TAG, "onRefresh called from SwipeRefreshLayout");

                    // This method performs the actual data-refresh operation.
                    // The method calls setRefreshing(false) when it's finished.
                    myUpdateOperation();
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }
    );
}

void myUpdateOperation(){
    // TODO: 20-Mar-17
    Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
}
  

Fragment2.java

public class Fragment1 extends Fragment {


    public Fragment1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_1, container, false);
    }

    public void fragment1refresh(){
        // TODO: this to be call when refresh on MainActivity and also fragment_1 is visible 
    }

}

2 个答案:

答案 0 :(得分:1)

为此,您的Fragment1Fragment2课程会延长CustomFragment CustomFragment的代码可以是这样的

public abstarct class CustomFragment extends Fragment {

        public CustomFragment() {
            // Required empty public constructor
        }

        public abstract void refresh();
}

Fragment1实施更改为:

public class Fragment1 extends CustomFragment {


        public Fragment1() {
            // Required empty public constructor
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_1, container, false);
        }

        @Override
        public void refresh(){
            // TODO: this to be call when refresh on MainActivity and also fragment_1 is visible 
        }
}

同样改变Fragment2类的实现。

现在在MainActivity课程中获取对当前可见片段的引用。这可以这样做:

CustomFragment currentFragment = (CustomFragment)mFragmentManager.findFragmentById(R.id.main_fragment);

现在将更改myUpdateOperation()实施内容改为:

private void myUpdateOperation(){
// TODO: 20-Mar-17
currentFragment.refresh();
}

答案 1 :(得分:0)

从片段管理器中获取当前片段,然后检查哪种片段类型与之匹配:

Fragment currentFragment = mFragmentManager.findFragmentById(R.id.main_fragment);
if (currentFragment instanceof Fragment1) {
    ((Fragemnt1) currentFragment).fragment1refresh()
} if (currentFragment instanceof Fragment2) {
    ((Fragemnt2) currentFragment).fragment2refresh()
}