首页 > 代码库 > 安卓学习-activity-Fragment
安卓学习-activity-Fragment
左边列表,右边明细
activity_main1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:showDividers="middle" > <fragment android:id="@+id/fragment1" android:name="com.exa.books.ListFrag" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" /> <fragment android:id="@+id/fragment2" android:name="com.exa.books.MxFrag" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" /></LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements ListFrag.CallBacks{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); } //实现回调方法 @Override public void onItemSelect(HashMap<String, String> map) { Bundle b=new Bundle(); b.putSerializable("map", map); MxFrag mxFrag=new MxFrag(); mxFrag.setArguments(b); getFragmentManager().beginTransaction().replace(R.id.fragment2, mxFrag).commit(); }}
list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="20dp" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="书名:" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编号:" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>
ListFrag.java
//书的列表public class ListFrag extends ListFragment { //回调接口 public interface CallBacks{ public void onItemSelect(HashMap<String, String> map); } List<HashMap<String, String>> data=http://www.mamicode.com/new ArrayList<HashMap<String, String>>(); @Override public void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(savedInstanceState); HashMap<String, String> map=new HashMap<String, String>(); map.put("bh", "00001"); map.put("name", "java学习"); data.add(map); HashMap<String, String> map1=new HashMap<String, String>(); map1.put("bh", "00002"); map1.put("name", "delphi学习"); data.add(map1); HashMap<String, String> map2=new HashMap<String, String>(); map2.put("bh", "00003"); map2.put("name", "C#学习"); data.add(map2); SimpleAdapter adapter=new SimpleAdapter(getActivity(), data,R.layout.list,new String[]{"bh","name"},new int[]{R.id.textView2,R.id.textView4}); setListAdapter(adapter); } //触发回调接口 @Override public void onListItemClick(ListView l, View v, int position, long id) { HashMap<String, String> map=data.get(position); mCallBacks.onItemSelect(map); } private CallBacks mCallBacks; @Overridepublic void onAttach(Activity activity) { // TODO 自动生成的方法存根 super.onAttach(activity); mCallBacks=(CallBacks)activity;}}
mx.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView2" android:layout_toRightOf="@+id/textView2" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="编号" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textView2" android:layout_alignLeft="@+id/textView6" android:layout_marginLeft="28dp" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:text="书名" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:text="介绍" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView3" android:text="价格" android:textAppearance="?android:attr/textAppearanceLarge" /></RelativeLayout>
MxFrag.java
public class MxFrag extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view= inflater.inflate(R.layout.mx, null); Bundle b=getArguments(); if(b!=null){ Map map=(Map)b.getSerializable("map"); String bh=map.get("bh")+""; String name=map.get("name")+""; TextView textView5=(TextView)view.findViewById(R.id.textView5); TextView textView6=(TextView)view.findViewById(R.id.textView6); textView5.setText(bh); textView6.setText(name); } return view; }}
安卓学习-activity-Fragment
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。