安卓学习-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>
View Code

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();
    }
}
View Code

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>
View Code

ListFrag.java

技术分享
//书的列表
public class ListFrag extends ListFragment {

    //回调接口
    public interface CallBacks{
        public void onItemSelect(HashMap<String, String> map);
    }
    
    
    List<HashMap<String, String>> data=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;
    
@Override
public void onAttach(Activity activity) {
    // TODO 自动生成的方法存根
    super.onAttach(activity);
    mCallBacks=(CallBacks)activity;
}
}
View Code

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>
View Code

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;
    }
}
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。