要寫Fragment真的是要點功力,對我這種android 新手實在是很不直觀,不過蝸牛還是試著去理解他,雖然還一知半解,可是至少可以實作deom program,在此順便記錄一下學習狀況供日後參考.

 

我要設計一個簡單的Fragment 並與一個管理Fragment的Activity溝通

簡單化面如下:

device-2014-06-18-065000    

activity_main.xml :如下 (LinearLayout mytest將會被Fragmentf取代)

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >

 

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="TextView" />

 

<LinearLayout
android:id="@+id/mytest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:orientation="vertical" >
</LinearLayout>

 

</LinearLayout>

 

 設計TestFragment.java

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class TestFragment extends Fragment {
View view;
Button btn1,btn2,btn3;
OnHeadlineSelectedListener mCallback;
public interface OnHeadlineSelectedListener {

//======================>與Activity溝通區塊,可以當作制式化
/**
* Called by HeadlinesFragment when a item is selected
*/
//這裡可以設定傳遞的資料型態google範例為int 這裡我使用String
public void onaitemSelected(String datestring); 

}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}

//======================>(end)與Activity溝通區塊

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmentcommunicate, container, false);

//在此設計Fragment UI元件
btn1=(Button)view.findViewById(R.id.button1);
btn2=(Button)view.findViewById(R.id.button2);
btn3=(Button)view.findViewById(R.id.button3);
btn1.setOnClickListener(new Btn_Listener());
btn2.setOnClickListener(new Btn_Listener());
btn3.setOnClickListener(new Btn_Listener());
return view;
}

//很一般的監聽方法
private class Btn_Listener implements OnClickListener {

@Override
public void onClick(View v) {
if(v.getId()==R.id.button1){
mCallback.onaitemSelected(btn1.getText().toString()); //回傳資料給activity
}else if(v.getId()==R.id.button2){
mCallback.onaitemSelected(btn2.getText().toString());
}else if(v.getId()==R.id.button3){
mCallback.onaitemSelected(btn3.getText().toString());
}
}
}

}

回到MainActivity.java

public class MainActivity extends Activity implements OnHeadlineSelectedListener {
private TextView tv1;
private FragmentManager fragMamager;
private FragmentTransaction fragTransaction;
private TestFragment testfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView1);
fragMamager = getFragmentManager();
fragTransaction = fragMamager.beginTransaction();
testfragment = new TestFragment();
fragTransaction.add(R.id.mytest, testfragment);
fragTransaction.commit();
}

 

//eclipse MainActivity extends Activity implements OnHeadlineSelectedListener 再加入這行後會要求實作此時由eclipse自動產生
@Override
public void onaitemSelected(String datestring) {
// TODO 自動產生的方法 Stub
tv1.setText(datestring); //在此寫入想要的動作
}
}

 

 

文章標籤
創作者介紹

台灣蝸牛人生哲學

台灣蝸牛 發表在 痞客邦 PIXNET 留言(0) 人氣()