Fragment的两个子类ListFragment和DialogFragment的应用Demo

来源:互联网 发布:win10怎么安装软件 编辑:程序博客网 时间:2024/05/20 04:31

相比较之下Fragment比Activity更加轻量级,更节省资源,能够实现动态移除添加替换,方便灵活快捷

FragmentChildActivity的代码:
<span style="font-size:14px;">import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import com.xhsc.layout.relativelayout.R;public class FragmentChildActivity extends Activity {    Button mDialogBtn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.fragment_activity__child_layout);        mDialogBtn = (Button) findViewById(R.id.dialog_fragment_btn);        getFragmentManager().beginTransaction().add(R.id.activity_fragment_child_framelayout,FragmentListChild.newInstance()).commit();        mDialogBtn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                getFragmentManager().beginTransaction().add(ChildDialogFragment.newInstance(),"dialog").commit();            }        });    }}</span>
FragmentChildActivity的Xml布局代码:
<span style="font-family:SimSun;font-size:12px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <Button        android:id="@+id/dialog_fragment_btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="弹出对话框"/>    <FrameLayout        android:layout_below="@id/dialog_fragment_btn"        android:background="@android:color/holo_orange_light"        android:id="@+id/activity_fragment_child_framelayout"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </FrameLayout></RelativeLayout></span>

FragmentListChild的Java代码:
<span style="font-size:10px;">import android.app.ListFragment;import android.content.Context;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;/** * A simple {@link Fragment} subclass. */public class FragmentListChild extends ListFragment {    String[] arraylist={"俄指土总统参与IS石油交易","SUV被打成蜂窝 俄土黑海对峙","俄间谍被处决 官员醉酒骂人",            "男女诡异死亡 女挺大肚热舞","冰冰李晨当康熙最后一期嘉宾","习近平:支持金融机构扩大对南非融资规模"            ,"习近平在津巴布韦野生动物基地给大象喂食","李克强部署金融改革 国务院通过计划生育法修正草案",            "户口办理拟向1300万黑户开放 多地超生落户脱钩计生","美国加州枪击案已致14死 确认系本土恐怖袭击 专题","普京让土耳其总统失眠 俄加紧在叙部署大批新武器"};    HeadlinesAdapter adapter;    public static FragmentListChild newInstance() {        Bundle args = new Bundle();        FragmentListChild fragment = new FragmentListChild();        fragment.setArguments(args);        return fragment;    }    @Override    public void onListItemClick(ListView l, View v, int position, long id) {        super.onListItemClick(l, v, position, id);        String msg = (String) l.getAdapter().getItem(position);        Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();    }    @Override    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        adapter = new HeadlinesAdapter(getActivity());        setListAdapter(adapter);        adapter.setArrayData(arraylist);    }    private class HeadlinesAdapter extends BaseAdapter {        String[] arrayData=new String[]{};        LayoutInflater layoutInflater;        public HeadlinesAdapter(Context context){            layoutInflater = LayoutInflater.from(context);        }        public void setArrayData(String[] arrayData) {            this.arrayData = arrayData;            notifyDataSetChanged();        }        @Override        public int getCount() {            return arrayData.length;        }        @Override        public Object getItem(int i) {            return arrayData[i];        }        @Override        public long getItemId(int i) {            return i;        }        @Override        public View getView(int i, View view, ViewGroup viewGroup) {            if(view==null){                view = layoutInflater.inflate(android.R.layout.simple_list_item_1,null);            }            TextView textView = (TextView) view;            textView.setText((String) getItem(i));            return view;        }    }}</span>

ChildDialogFragment的Java代码:
<span style="font-size:12px;">import android.app.AlertDialog;import android.app.Dialog;import android.app.DialogFragment;import android.content.DialogInterface;import android.os.Bundle;import android.support.v4.app.Fragment;import android.widget.Toast;import com.xhsc.layout.relativelayout.R;/** * A simple {@link Fragment} subclass. */public class ChildDialogFragment extends DialogFragment {    public static ChildDialogFragment newInstance() {                Bundle args = new Bundle();                ChildDialogFragment fragment = new ChildDialogFragment();        fragment.setArguments(args);        return fragment;    }    @Override    public Dialog onCreateDialog(Bundle savedInstanceState) {        return new AlertDialog.Builder(getActivity()).setMessage("你好我是Fragment对话框").setIcon(R.drawable.dorsey).                setPositiveButton("确定",new DialogInterface.OnClickListener(){            @Override            public void onClick(DialogInterface dialogInterface, int i) {                Toast.makeText(getActivity(),"确定",Toast.LENGTH_SHORT).show();            }        }).create();    }   }</span>
也可以自定义布局Fragment对话,采用onCreateView()方法返回这个View即可



0 0