赞
踩
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
-
- <FrameLayout
- android:id="@+id/titles"
- android:layout_width="0px"
- android:layout_height="match_parent"
- android:layout_weight="1" >
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <ListView
- android:id="@+id/listview"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- </ListView>
-
- <ProgressBar
- android:id="@+id/progressBar1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:visibility="gone" />
- </RelativeLayout>
- </FrameLayout>
-
- <FrameLayout
- android:id="@+id/details"
- android:layout_width="0px"
- android:layout_height="match_parent"
- android:layout_weight="3"
- android:background="?android:attr/detailsElementBackground" >
- </FrameLayout>
-
- </LinearLayout>
title.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
-
- <ImageView
- android:id="@+id/imageview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/ic_launcher"
- />
-
- <TextView
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
-
- </LinearLayout>
TitlesFragment.java
- package com.xuzhi.fragment;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.FragmentTransaction;
- import android.app.ListFragment;
- import android.app.ProgressDialog;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemLongClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.ProgressBar;
-
- public class TitlesFragment extends ListFragment
- {
-
- // boolean mDualPane;
-
- int mCurCheckPosition = 0;
-
- private static List<String> list = new ArrayList<String>();
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- System.out.println("Fragment-->onCreate");
- }
-
- public static TitlesFragment newInstance()
- {
- TitlesFragment details = new TitlesFragment();
- return details;
- }
-
- Thread th = new Thread()
- {
-
- @Override
- public void run()
- {
- try
- {
- sleep(5000);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- for (int i = 0; i < 100; i++)
- {
- list.add("csh" + i);
- }
- Message msg = new Message();
- msg.what = 1;
- handler.sendMessage(msg);
- super.run();
- }
-
- };
-
- Handler handler = new Handler()
- {
-
- @Override
- public void handleMessage(Message msg)
- {
- mInvock1.callInvock();
- super.handleMessage(msg);
- }
-
- };
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
- {
- System.out.println("Fragment-->onCreateView");
- return super.onCreateView(inflater, container, savedInstanceState);
- }
-
- @Override
- public void onPause()
- {
- super.onPause();
- System.out.println("Fragment-->onPause");
- }
-
- @Override
- public void onStop()
- {
- super.onStop();
- System.out.println("Fragment-->onStop");
- }
-
- @Override
- public void onAttach(Activity activity)
- {
- super.onAttach(activity);
- System.out.println("Fragment-->onAttach");
- }
-
- @Override
- public void onStart()
- {
- super.onStart();
- System.out.println("Fragment-->onStart");
- }
-
- @Override
- public void onResume()
- {
- super.onResume();
- System.out.println("Fragment-->onResume");
- th.start();
- }
-
- @Override
- public void onDestroy()
- {
- super.onDestroy();
- System.out.println("Fragment-->onDestroy");
- }
-
- private ListView listviewchild;
-
- private void addData()
- {
- for (int i = 0; i < 5; i++)
- {
- list.add("csh" + i);
- }
- }
-
- private adapter mAdapter;
-
- private ProgressBar mProgressBar;
-
- @Override
- public void onActivityCreated(Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- super.onActivityCreated(savedInstanceState);
- System.out.println("Fragment-->onActivityCreted");
- addData();// 添加初始数据
- View listview = getActivity().findViewById(R.id.titles);
- mProgressBar = (ProgressBar) listview.findViewById(R.id.progressBar1);
- if (listview != null)
- {
- listviewchild = (ListView) listview.findViewById(R.id.listview);
- mAdapter = new adapter(list, getActivity());
- listviewchild.setAdapter(mAdapter);
- listviewchild.setOnItemClickListener(new OnItemClickListener()
- {
-
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1,
- int position, long arg3)
- {
- showDetails(position);
- }
- });
- listviewchild
- .setOnItemLongClickListener(new OnItemLongClickListener()
- {
-
- @Override
- public boolean onItemLongClick(AdapterView<?> arg0,
- View arg1, int postion, long arg3)
- {
- MyDialogFragment dialog =
- MyDialogFragment.newInstance(
- "确认删除" + list.get(postion) + "数据",
- postion);
- dialog.show(getFragmentManager(), "dialog");
- return false;
- }
- });
- }
- showDetails(0);// 默认显示第一个
- }
-
- @Override
- public void onSaveInstanceState(Bundle outState)
- {
- super.onSaveInstanceState(outState);
- outState.putInt("curChoice", mCurCheckPosition);
- }
-
- @Override
- public void onListItemClick(ListView l, View v, int position, long id)
- {
- super.onListItemClick(l, v, position, id);
- showDetails(position);
- }
-
- void showDetails(int index)
- {
- mCurCheckPosition = index;
- getListView().setItemChecked(index, true);
- DetailsFragment details =
- (DetailsFragment) getFragmentManager().findFragmentById(
- R.id.details);
- if (details == null || details.getShownIndex() != index)
- {
- details = DetailsFragment.newInstance(mCurCheckPosition, list);
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.replace(R.id.details, details);
- ft.commit();
- }
- }
-
- public void deleteListItem(int postion)
- {
- list.remove(postion);
- mAdapter.refearch(list);
- }
-
- callBack mInvock1 = new callBack()
- {
-
- @Override
- public void callInvock()
- {
- mAdapter.refearch(list);
- }
- };
-
- public interface callBack
- {
-
- void callInvock();
- }
- }
- package com.xuzhi.fragment;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Fragment;
- import android.os.Bundle;
- import android.util.TypedValue;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ScrollView;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class DetailsFragment extends Fragment
- {
-
- private static List<String> list = new ArrayList<String>();
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setHasOptionsMenu(true);
- }
-
- public static DetailsFragment newInstance(int index, List<String> li)
- {
- list = li;
- DetailsFragment details = new DetailsFragment();
- Bundle args = new Bundle();
- args.putInt("index", index);
- details.setArguments(args);
- return details;
- }
-
- public int getShownIndex()
- {
- return getArguments().getInt("index", 0);
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- if (container == null)
- return null;
- if (list.size() == 0)
- {
- return null;
- }
- ScrollView scroller = new ScrollView(getActivity());
- TextView text = new TextView(getActivity());
- int padding =
- (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
- getActivity().getResources().getDisplayMetrics());
- text.setPadding(padding, padding, padding, padding);
- scroller.addView(text);
- text.setText(list.get(getShownIndex()));
- return scroller;
- }
-
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
- {
- // TODO Auto-generated method stub
- super.onCreateOptionsMenu(menu, inflater);
- menu.add("Menu 1a").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
- menu.add("Menu 1b").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- // TODO Auto-generated method stub
- Toast.makeText(
- getActivity(),
- "index is" + getShownIndex() + " && menu text is "
- + item.getTitle(), 1000).show();
- return super.onOptionsItemSelected(item);
- }
- }
- package com.xuzhi.fragment;
-
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.content.DialogInterface;
- import android.os.Bundle;
-
- public class MyDialogFragment extends DialogFragment
- {
-
- public static MyDialogFragment newInstance(String title, int position)
- {
-
- MyDialogFragment frag = new MyDialogFragment();
- Bundle args = new Bundle();
- args.putString("title", title);
- args.putInt("position", position);
- frag.setArguments(args);
- return frag;
-
- }
-
- public Dialog onCreateDialog(Bundle saveInstanceState)
- {
- // 获取对象实例化时传入的窗口标题。
-
- String title = getArguments().getString("title");
- // 返回提醒对话框。
-
- return new AlertDialog.Builder(getActivity())
- // 设置标题图标
- .setIcon(R.drawable.ic_launcher)
- // 设置标题
- .setTitle(title)
- // 设置确定按钮的标题和点击事件监听器。
- .setPositiveButton("确定", new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- int position = getArguments().getInt("position");
- ((TitlesFragment) (getFragmentManager()
- .findFragmentByTag("title")))
- .deleteListItem(position);
- }
- })
- // 设置取消按钮的标题和点击事件监听器。
- .setNegativeButton("取消", new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- }
- })
- // 创建对话框
- .create();
- }
- }
- package com.xuzhi.fragment;
-
- import java.util.List;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
-
- public class adapter extends BaseAdapter
- {
-
- private List<String> list;
-
- private Context mContext;
-
- public adapter(List<String> list, Context c)
- {
- this.list = list;
- this.mContext = c;
- }
-
- public void refearch(List<String> li){
- this.list=li;
- this.notifyDataSetChanged();
- }
- @Override
- public int getCount()
- {
- // TODO Auto-generated method stub
- return list.size();
- }
-
- @Override
- public Object getItem(int arg0)
- {
- // TODO Auto-generated method stub
- return list.get(arg0);
- }
-
- @Override
- public long getItemId(int arg0)
- {
- // TODO Auto-generated method stub
- return arg0;
- }
-
- @Override
- public View getView(int arg0, View view, ViewGroup arg2)
- {
- // TODO Auto-generated method stub
- view = LayoutInflater.from(mContext).inflate(R.layout.title, null);
- TextView txt = (TextView) view.findViewById(R.id.button);
- txt.setText(list.get(arg0));
- return view;
- }
-
- }
- package com.xuzhi.fragment;
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
-
- public class FragmentDemoActivity extends Activity
- {
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
-
- private void init()
- {
- TitlesFragment title;
- title =
- (TitlesFragment) getFragmentManager().findFragmentById(
- R.id.titles);
- if (title == null)
- {
- title = TitlesFragment.newInstance();
- FragmentManager fm = getFragmentManager();
- FragmentTransaction ft = fm.beginTransaction();
- ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
- ft.add(title, "title");// 设置title标记,方便用到的时候根据标记取出来
- ft.commit();
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。