Fragmentによる簡単なデータインタラクション
4443 ワード
前の文に続いてFragmentの使用を研究し続けて、本文は主にこのような簡単な応用を実現します:左側にタイトルバーを表示して、それからそれをクリックして、右側に異なる内容を表示して、内容は需要によって自分で相応の変化をします.コードは次のとおりです.
MainActivity:
TitleFragment:
main.xml:
fragment_title.xml:
画像の効果を添付します.
MainActivity:
package com.home.testfragment;
import com.home.testfragment.TitleFragment.OnTitleSelectedListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements
OnTitleSelectedListener {
public static String[] names = { " ", " ", " ", " ", " ", " ", " ",
" ", " " };
private TextView detailText;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main);
detailText = (TextView) findViewById(R.id.main_tv_detail);
}
@Override
public void OnShowDetails(int position) {
detailText.setText(" " + names[position]);
}
}
TitleFragment:
package com.home.testfragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TitleFragment extends ListFragment {
OnTitleSelectedListener mCallBack;
public interface OnTitleSelectedListener {
public void OnShowDetails(int position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_title, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, MainActivity.names));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallBack = (OnTitleSelectedListener) activity;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mCallBack.OnShowDetails(position);
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/main_FirstFragment"
android:name="com.home.testfragment.TitleFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/main_tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_weight="0.5"
android:textSize="40sp" />
</LinearLayout>
fragment_title.xml:
<?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" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
画像の効果を添付します.