标签:lda keep srp etc usb fragment oda dex sse
碎片(Fragment)是一种可以嵌入在活动当中的UI 片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛。
一、增加Fragment,another_right_fragment.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffff00" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="This is another right fragment" android:textSize="20sp" /> </LinearLayout>
二、AnotherRightFragment类:
package com.example.fragmenttest2; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Linux on 2016/8/9. */ public class AnotherRightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.another_right_fragment, container, false); return view; } }
三、在mainActivity中使用
public void addFragment(View view) { AnotherRightFragment anotherRightFragment = new AnotherRightFragment(); getFragmentManager().beginTransaction().add(R.id.right_layout, anotherRightFragment, FRAGMENT_TAG).commit(); }
四、运行效果如下:
一、调用FragmentManager的findFragmentById()方法,可以在活动中得到相应碎片的实例,然后就能轻松地调用碎片里的方法了。
RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.right_fragment);
二、在每个碎片中都可以通过调用getActivity()方法来得到和当前碎片相关联的活动实例
MainActivity activity = (MainActivity) getActivity();
三、替换Fragment,
public void replaceFragment(View view) { AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.right_layout, fragment); // 这没有加的话,back直接退出 transaction.addToBackStack(null); transaction.commit(); }
四、删除Fragment:
public void deleteFragment(View view) { getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentByTag(FRAGMENT_TAG)).commit(); }
五、运行效果:
一、在Fragment中定义接口,并在onAttach方法中转换,之后可以在Fragment其他方法中调用mCallback.onArticleSelected()的方法。
OnHeadlineSelectedListener mCallback; public interface OnHeadlineSelectedListener { public void onArticleSelected(); } @Override public void onAttach(Context context) { Log.i(TAG, "on attach"); super.onAttach(context); try { mCallback = (OnHeadlineSelectedListener) context; } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement OnHeadlineSelectedListener"); } }
二、在MainActivity中继承OnHeadlineSelectedListener接口。
public class MainActivity extends AppCompatActivity implements HeadlinesFragment.OnHeadlineSelectedListener { @Override public void onArticleSelected() { Toast.makeText(MainActivity.this, "hello world", Toast.LENGTH_SHORT).show(); } }
这个方法由于是在MainActivity,也就是可以实现Fragment与Fragment之间交互通信,然而事实也确实如此。
标签:lda keep srp etc usb fragment oda dex sse
原文地址:http://www.cnblogs.com/huhx/p/fragment.html