Fragment的两个子类ListFragment和DialogFragment的应用Demo
2015-12-03 23:51
423 查看
相比较之下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即可
相关文章推荐
- Android Fragment 基本了解(图文介绍)
- Android Fragment的使用方法(翻译)
- Android 中 Fragment的使用大全
- Android基于ViewPager Fragment实现选项卡
- Android Studio注释模板介绍
- Android基础之使用Fragment适应不同屏幕和分辨率(分享)
- 深入浅析 Android Fragment(上篇)
- Android中给fragment写入参数的轻量开发包FragmentArgs简介
- 深入浅析Android Fragment(下篇)
- Android Fragment中使用SurfaceView切换时闪一下黑屏的解决办法
- Android编程使用Fragment界面向下跳转并一级级返回的实现方法
- Fragment里添加ListView不要用ListFragment
- 详解Android开发中Fragment的使用
- Android中fragment嵌套fragment问题解决方法
- Android基础之Fragment与Activity交互详解
- Android基础之使用Fragment控制切换多个页面
- 关于Android Studio 和 Gradle的那些事儿
- 开源中国 OsChina Android 客户端源码分析(13)Fragment学习
- 收藏各种技术源码
- Fragment