首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Fragment兑现两栏展示

2013-01-06 
Fragment实现两栏展示现在Fragment被使用的也比较多了,官方也在推荐使用Fragment,尤其是平板上开发显得非

Fragment实现两栏展示

现在Fragment被使用的也比较多了,官方也在推荐使用Fragment,尤其是平板上开发显得非常的方便,这里我是在手机里展示的,可以先看一下效果图。

Fragment兑现两栏展示

实现也是比较的简单,activity的布局文件里,有两个控件,一个是titles,一个是details, titles是通过指定了了对应的F绕过梦来展现的内容,Fragment本身是一个View,有自己的生命周期。这个Demo里点击titles里的目录在右侧可以显示对应的详情信息。右侧放的是一个Framlayout,在代码里通过语句

public class DetailsFragment extends Fragment{//方法newInstance用于新建一个DetailFragmentpublic static DetailsFragment newInstance(int index){DetailsFragment f = new DetailsFragment();Bundle bundle = new Bundle();bundle.putInt("index", index);//给Fragment初始化参数f.setArguments(bundle);return f;}//产生显示内容的Viewpublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){if(container == null){return null;}ScrollView scroller = new ScrollView(getActivity());TextView text = new TextView(getActivity());//设置TextView边框大小int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getActivity().getResources().getDisplayMetrics());text.setPadding(padding,padding,padding,padding);scroller.addView(text);text.setText(DIALOGUE[getShowIndex()]);return scroller;}//获取新建DetailFragment时声明的参数public int getShowIndex(){return getArguments().getInt("index");}


Demo下载


热点排行