前言
期间遇到了非常多坑。也跟着师兄学到了非常多Android知识。
可是人总是要拥抱变化,不能让自己太安逸,尽管有不舍,可是我已经证明了自己的学习能力,下一步就是開始做Rom Porting了。
这里总结一下之前项目中用到最多的Fragment。
Fragment简单介绍
可是一个Fragment有它自己独立的xml布局文件,而且具有良好的封装性。因此特殊情况下Fragment能够非常easy用Activity来进行替换。
创建Fragment
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/testview" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> </LinearLayout>Java代码中,普通情况下能够依据须要实现Fragment下面几个生命周期方法:
1. onAttach():当Fragment依附于activity时被调用,能够在该方法中获取activity句柄,从而实现Fragment和activity之间的通信。
Fragment生命周期
在Activity中增加Fragment
静态方法
<?xml version="1.0" encoding="utf-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <fragment android:id="@+id/first" android:name="com.example.FristFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/second" android:name="com.example.SecondFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
为了Fragment提供ID有三种方法:
- 用android:id属性提供一个唯一的标识
- 用android:tag属性提供一个唯一的字符串
- 假设上述两个属性都没有,系统会使用其容器视图的ID
动态方法
FragmentManager fragmentManager = getFragmentManager() FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();
Fragments通信
创建带參数的Fragment
演示样例代码例如以下:
import android.os.Bundle; import android.support.v4.app.Fragment; public class TestFragment extends Fragment { public static TestFragment newInstance(int num, String title) { TestFragment fragment = new TestFragment(); Bundle args = new Bundle(); args.putInt("num", num); args.putString("title", title); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int num = getArguments().getInt("num", 0); String title = getArguments().getString("title", ""); } }你能够在Activity里,简单的载入一个带參数的Fragment:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); TestFragment fragment = TestFragment.newInstance(5, "fragment title"); ft.replace(R.id.placeholder, fragment); ft.commit();
调用Fragment的方法
public class TestFragment extends Fragment { public void doSomething(String param) { // do something in fragment } }在Activity中。能够直接通过Fragment的对象句柄调用该方法:
public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TestFragment testFragment = new TestFragment(); testFragment.doSomething("some param"); } }
Fragment Listener
假设Fragment须要共享事件给Activity,则须要利用这种方法。Fragment中定义一个接口,而且由Activity来实现这个接口。在onAttach()方法中将实现了这个接口的Activity获得到。
import android.support.v4.app.Fragment; public class MyListFragment extends Fragment { // ... // Define the listener of the interface type // listener is the activity itself private OnItemSelectedListener listener; // Define the events that the fragment will use to communicate public interface OnItemSelectedListener { public void onRssItemSelected(String link); } // Store the listener (activity) that will have events fired once the fragment is attached @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof OnItemSelectedListener) { listener = (OnItemSelectedListener) activity; } else { throw new ClassCastException(activity.toString() + " must implement MyListFragment.OnItemSelectedListener"); } } // Now we can fire the event when the user selects something in the fragment public void onSomeClick(View v) { listener.onRssItemSelected("some link"); } }在Activity中实现这个接口:
import android.support.v4.app.FragmentActivity; public class RssfeedActivity extends FragmentActivity implements MyListFragment.OnItemSelectedListener { DetailFragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rssfeed); fragment = (DetailFragment) getSupportFragmentManager() .findFragmentById(R.id.detailFragment); } // Now we can define the action to take in the activity when the fragment event fires @Override public void onRssItemSelected(String link) { if (fragment != null && fragment.isInLayout()) { fragment.setText(link); } } }