当前位置:   article > 正文

android动态使用FrameLayout(简单使用)

android动态使用FrameLayout(简单使用)

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal" >
  6. <FrameLayout
  7. android:id="@+id/titles"
  8. android:layout_width="0px"
  9. android:layout_height="match_parent"
  10. android:layout_weight="1" >
  11. <RelativeLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent" >
  14. <ListView
  15. android:id="@+id/listview"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent" >
  18. </ListView>
  19. <ProgressBar
  20. android:id="@+id/progressBar1"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_centerHorizontal="true"
  24. android:layout_centerVertical="true"
  25. android:visibility="gone" />
  26. </RelativeLayout>
  27. </FrameLayout>
  28. <FrameLayout
  29. android:id="@+id/details"
  30. android:layout_width="0px"
  31. android:layout_height="match_parent"
  32. android:layout_weight="3"
  33. android:background="?android:attr/detailsElementBackground" >
  34. </FrameLayout>
  35. </LinearLayout>


title.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal" >
  6. <ImageView
  7. android:id="@+id/imageview"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:background="@drawable/ic_launcher"
  11. />
  12. <TextView
  13. android:id="@+id/button"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. />
  17. </LinearLayout>


TitlesFragment.java

  1. package com.xuzhi.fragment;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.app.FragmentTransaction;
  7. import android.app.ListFragment;
  8. import android.app.ProgressDialog;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.AdapterView;
  16. import android.widget.AdapterView.OnItemClickListener;
  17. import android.widget.AdapterView.OnItemLongClickListener;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.ListView;
  20. import android.widget.ProgressBar;
  21. public class TitlesFragment extends ListFragment
  22. {
  23. // boolean mDualPane;
  24. int mCurCheckPosition = 0;
  25. private static List<String> list = new ArrayList<String>();
  26. @Override
  27. public void onCreate(Bundle savedInstanceState)
  28. {
  29. // TODO Auto-generated method stub
  30. super.onCreate(savedInstanceState);
  31. System.out.println("Fragment-->onCreate");
  32. }
  33. public static TitlesFragment newInstance()
  34. {
  35. TitlesFragment details = new TitlesFragment();
  36. return details;
  37. }
  38. Thread th = new Thread()
  39. {
  40. @Override
  41. public void run()
  42. {
  43. try
  44. {
  45. sleep(5000);
  46. }
  47. catch (InterruptedException e)
  48. {
  49. e.printStackTrace();
  50. }
  51. for (int i = 0; i < 100; i++)
  52. {
  53. list.add("csh" + i);
  54. }
  55. Message msg = new Message();
  56. msg.what = 1;
  57. handler.sendMessage(msg);
  58. super.run();
  59. }
  60. };
  61. Handler handler = new Handler()
  62. {
  63. @Override
  64. public void handleMessage(Message msg)
  65. {
  66. mInvock1.callInvock();
  67. super.handleMessage(msg);
  68. }
  69. };
  70. @Override
  71. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  72. Bundle savedInstanceState)
  73. {
  74. System.out.println("Fragment-->onCreateView");
  75. return super.onCreateView(inflater, container, savedInstanceState);
  76. }
  77. @Override
  78. public void onPause()
  79. {
  80. super.onPause();
  81. System.out.println("Fragment-->onPause");
  82. }
  83. @Override
  84. public void onStop()
  85. {
  86. super.onStop();
  87. System.out.println("Fragment-->onStop");
  88. }
  89. @Override
  90. public void onAttach(Activity activity)
  91. {
  92. super.onAttach(activity);
  93. System.out.println("Fragment-->onAttach");
  94. }
  95. @Override
  96. public void onStart()
  97. {
  98. super.onStart();
  99. System.out.println("Fragment-->onStart");
  100. }
  101. @Override
  102. public void onResume()
  103. {
  104. super.onResume();
  105. System.out.println("Fragment-->onResume");
  106. th.start();
  107. }
  108. @Override
  109. public void onDestroy()
  110. {
  111. super.onDestroy();
  112. System.out.println("Fragment-->onDestroy");
  113. }
  114. private ListView listviewchild;
  115. private void addData()
  116. {
  117. for (int i = 0; i < 5; i++)
  118. {
  119. list.add("csh" + i);
  120. }
  121. }
  122. private adapter mAdapter;
  123. private ProgressBar mProgressBar;
  124. @Override
  125. public void onActivityCreated(Bundle savedInstanceState)
  126. {
  127. // TODO Auto-generated method stub
  128. super.onActivityCreated(savedInstanceState);
  129. System.out.println("Fragment-->onActivityCreted");
  130. addData();// 添加初始数据
  131. View listview = getActivity().findViewById(R.id.titles);
  132. mProgressBar = (ProgressBar) listview.findViewById(R.id.progressBar1);
  133. if (listview != null)
  134. {
  135. listviewchild = (ListView) listview.findViewById(R.id.listview);
  136. mAdapter = new adapter(list, getActivity());
  137. listviewchild.setAdapter(mAdapter);
  138. listviewchild.setOnItemClickListener(new OnItemClickListener()
  139. {
  140. @Override
  141. public void onItemClick(AdapterView<?> arg0, View arg1,
  142. int position, long arg3)
  143. {
  144. showDetails(position);
  145. }
  146. });
  147. listviewchild
  148. .setOnItemLongClickListener(new OnItemLongClickListener()
  149. {
  150. @Override
  151. public boolean onItemLongClick(AdapterView<?> arg0,
  152. View arg1, int postion, long arg3)
  153. {
  154. MyDialogFragment dialog =
  155. MyDialogFragment.newInstance(
  156. "确认删除" + list.get(postion) + "数据",
  157. postion);
  158. dialog.show(getFragmentManager(), "dialog");
  159. return false;
  160. }
  161. });
  162. }
  163. showDetails(0);// 默认显示第一个
  164. }
  165. @Override
  166. public void onSaveInstanceState(Bundle outState)
  167. {
  168. super.onSaveInstanceState(outState);
  169. outState.putInt("curChoice", mCurCheckPosition);
  170. }
  171. @Override
  172. public void onListItemClick(ListView l, View v, int position, long id)
  173. {
  174. super.onListItemClick(l, v, position, id);
  175. showDetails(position);
  176. }
  177. void showDetails(int index)
  178. {
  179. mCurCheckPosition = index;
  180. getListView().setItemChecked(index, true);
  181. DetailsFragment details =
  182. (DetailsFragment) getFragmentManager().findFragmentById(
  183. R.id.details);
  184. if (details == null || details.getShownIndex() != index)
  185. {
  186. details = DetailsFragment.newInstance(mCurCheckPosition, list);
  187. FragmentTransaction ft = getFragmentManager().beginTransaction();
  188. ft.replace(R.id.details, details);
  189. ft.commit();
  190. }
  191. }
  192. public void deleteListItem(int postion)
  193. {
  194. list.remove(postion);
  195. mAdapter.refearch(list);
  196. }
  197. callBack mInvock1 = new callBack()
  198. {
  199. @Override
  200. public void callInvock()
  201. {
  202. mAdapter.refearch(list);
  203. }
  204. };
  205. public interface callBack
  206. {
  207. void callInvock();
  208. }
  209. }

DetailsFragment.java

  1. package com.xuzhi.fragment;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Fragment;
  5. import android.os.Bundle;
  6. import android.util.TypedValue;
  7. import android.view.LayoutInflater;
  8. import android.view.Menu;
  9. import android.view.MenuInflater;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ScrollView;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. public class DetailsFragment extends Fragment
  17. {
  18. private static List<String> list = new ArrayList<String>();
  19. @Override
  20. public void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setHasOptionsMenu(true);
  24. }
  25. public static DetailsFragment newInstance(int index, List<String> li)
  26. {
  27. list = li;
  28. DetailsFragment details = new DetailsFragment();
  29. Bundle args = new Bundle();
  30. args.putInt("index", index);
  31. details.setArguments(args);
  32. return details;
  33. }
  34. public int getShownIndex()
  35. {
  36. return getArguments().getInt("index", 0);
  37. }
  38. @Override
  39. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  40. Bundle savedInstanceState)
  41. {
  42. // TODO Auto-generated method stub
  43. if (container == null)
  44. return null;
  45. if (list.size() == 0)
  46. {
  47. return null;
  48. }
  49. ScrollView scroller = new ScrollView(getActivity());
  50. TextView text = new TextView(getActivity());
  51. int padding =
  52. (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
  53. getActivity().getResources().getDisplayMetrics());
  54. text.setPadding(padding, padding, padding, padding);
  55. scroller.addView(text);
  56. text.setText(list.get(getShownIndex()));
  57. return scroller;
  58. }
  59. @Override
  60. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
  61. {
  62. // TODO Auto-generated method stub
  63. super.onCreateOptionsMenu(menu, inflater);
  64. menu.add("Menu 1a").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
  65. menu.add("Menu 1b").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
  66. }
  67. @Override
  68. public boolean onOptionsItemSelected(MenuItem item)
  69. {
  70. // TODO Auto-generated method stub
  71. Toast.makeText(
  72. getActivity(),
  73. "index is" + getShownIndex() + " && menu text is "
  74. + item.getTitle(), 1000).show();
  75. return super.onOptionsItemSelected(item);
  76. }
  77. }

MyDialogFragment.java

  1. package com.xuzhi.fragment;
  2. import android.app.AlertDialog;
  3. import android.app.Dialog;
  4. import android.app.DialogFragment;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. public class MyDialogFragment extends DialogFragment
  8. {
  9. public static MyDialogFragment newInstance(String title, int position)
  10. {
  11. MyDialogFragment frag = new MyDialogFragment();
  12. Bundle args = new Bundle();
  13. args.putString("title", title);
  14. args.putInt("position", position);
  15. frag.setArguments(args);
  16. return frag;
  17. }
  18. public Dialog onCreateDialog(Bundle saveInstanceState)
  19. {
  20. // 获取对象实例化时传入的窗口标题。
  21. String title = getArguments().getString("title");
  22. // 返回提醒对话框。
  23. return new AlertDialog.Builder(getActivity())
  24. // 设置标题图标
  25. .setIcon(R.drawable.ic_launcher)
  26. // 设置标题
  27. .setTitle(title)
  28. // 设置确定按钮的标题和点击事件监听器。
  29. .setPositiveButton("确定", new DialogInterface.OnClickListener()
  30. {
  31. @Override
  32. public void onClick(DialogInterface dialog, int which)
  33. {
  34. int position = getArguments().getInt("position");
  35. ((TitlesFragment) (getFragmentManager()
  36. .findFragmentByTag("title")))
  37. .deleteListItem(position);
  38. }
  39. })
  40. // 设置取消按钮的标题和点击事件监听器。
  41. .setNegativeButton("取消", new DialogInterface.OnClickListener()
  42. {
  43. @Override
  44. public void onClick(DialogInterface dialog, int which)
  45. {
  46. }
  47. })
  48. // 创建对话框
  49. .create();
  50. }
  51. }

adapter.java

  1. package com.xuzhi.fragment;
  2. import java.util.List;
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.TextView;
  9. public class adapter extends BaseAdapter
  10. {
  11. private List<String> list;
  12. private Context mContext;
  13. public adapter(List<String> list, Context c)
  14. {
  15. this.list = list;
  16. this.mContext = c;
  17. }
  18. public void refearch(List<String> li){
  19. this.list=li;
  20. this.notifyDataSetChanged();
  21. }
  22. @Override
  23. public int getCount()
  24. {
  25. // TODO Auto-generated method stub
  26. return list.size();
  27. }
  28. @Override
  29. public Object getItem(int arg0)
  30. {
  31. // TODO Auto-generated method stub
  32. return list.get(arg0);
  33. }
  34. @Override
  35. public long getItemId(int arg0)
  36. {
  37. // TODO Auto-generated method stub
  38. return arg0;
  39. }
  40. @Override
  41. public View getView(int arg0, View view, ViewGroup arg2)
  42. {
  43. // TODO Auto-generated method stub
  44. view = LayoutInflater.from(mContext).inflate(R.layout.title, null);
  45. TextView txt = (TextView) view.findViewById(R.id.button);
  46. txt.setText(list.get(arg0));
  47. return view;
  48. }
  49. }

FragmentDemoActivity.java

  1. package com.xuzhi.fragment;
  2. import android.app.Activity;
  3. import android.app.FragmentManager;
  4. import android.app.FragmentTransaction;
  5. import android.os.Bundle;
  6. public class FragmentDemoActivity extends Activity
  7. {
  8. @Override
  9. public void onCreate(Bundle savedInstanceState)
  10. {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. init();
  14. }
  15. private void init()
  16. {
  17. TitlesFragment title;
  18. title =
  19. (TitlesFragment) getFragmentManager().findFragmentById(
  20. R.id.titles);
  21. if (title == null)
  22. {
  23. title = TitlesFragment.newInstance();
  24. FragmentManager fm = getFragmentManager();
  25. FragmentTransaction ft = fm.beginTransaction();
  26. ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  27. ft.add(title, "title");// 设置title标记,方便用到的时候根据标记取出来
  28. ft.commit();
  29. }
  30. }
  31. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/225290
推荐阅读
相关标签
  

闽ICP备14008679号