android147 360 程序锁fragment
package com.itheima.mobileguard.fragment; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.os.SystemClock; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.itheima.mobileguard.R; import com.itheima.mobileguard.db.dao.AppLockDao; import com.itheima.mobileguard.domain.AppInfo; import com.itheima.mobileguard.engine.AppInfoParser; import com.lidroid.xutils.DbUtils.DaoConfig; public class LockFragment extends Fragment { private ListView list_view; private TextView tv_lock; private List<AppInfo> lockLists; private AppLockDao dao; private LockAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.item_lock_fragment, null); list_view = (ListView) view.findViewById(R.id.list_view); tv_lock = (TextView) view.findViewById(R.id.tv_lock); return view; } @Override public void onStart() {// 每次页面可见就会调用这个方法 super.onStart(); // 拿到所有的应用程序 List<AppInfo> appInfos = AppInfoParser.getAppInfos(getActivity()); // 初始化一个加锁的集合 lockLists = new ArrayList<AppInfo>(); dao = new AppLockDao(getActivity()); for (AppInfo appInfo : appInfos) { // 如果能找到当前的包名说明在程序锁的数据库里面 if (dao.find(appInfo.getPackname())) { lockLists.add(appInfo); } else { } } adapter = new LockAdapter(); list_view.setAdapter(adapter); } private class LockAdapter extends BaseAdapter { @Override public int getCount() { tv_lock.setText("已加锁(" + lockLists.size() + ")个"); return lockLists.size(); } @Override public Object getItem(int position) { return lockLists.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final View view; ViewHolder holder = null; if (convertView == null) { view = View.inflate(getActivity(), R.layout.item_lock, null); holder = new ViewHolder(); holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon); holder.tv_name = (TextView) view.findViewById(R.id.tv_name); holder.iv_lock = (ImageView) view.findViewById(R.id.iv_lock); view.setTag(holder); } else { view = convertView; holder = (ViewHolder) view.getTag(); } final AppInfo appInfo = lockLists.get(position); holder.iv_icon.setImageDrawable(appInfo.getIcon()); holder.tv_name.setText(appInfo.getName()); holder.iv_lock.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0,//X的起始位置 Animation.RELATIVE_TO_SELF, -1.0f,//x的终止位置(左滑) Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0); translateAnimation.setDuration(5000); view.startAnimation(translateAnimation); new Thread() { public void run() { SystemClock.sleep(5000); getActivity().runOnUiThread(new Runnable() { @Override public void run() { dao.delete(appInfo.getPackname()); lockLists.remove(position); adapter.notifyDataSetChanged(); } }); }; }.start(); } }); return view; } } static class ViewHolder { ImageView iv_icon; TextView tv_name; ImageView iv_lock; } }
页面:
<?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="vertical" > <TextView android:id="@+id/tv_lock" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
UnLockFragment
package com.itheima.mobileguard.fragment; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.os.SystemClock; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.itheima.mobileguard.R; import com.itheima.mobileguard.db.dao.AppLockDao; import com.itheima.mobileguard.domain.AppInfo; import com.itheima.mobileguard.engine.AppInfoParser; //页面消失了再调用onStop()方法,onDestory()方法,不是调用onStop()方法,onDestory()方法再销毁页面。 public class UnLockFragment extends Fragment { private View view; private TextView tv_unlock; private ListView list_view; private List<AppInfo> appInfos; private AppLockDao dao; private List<AppInfo> unLockLists; private UnLockAdapter adapter; /* * 类似activity里面的setContentView,初始化界面在onCreateView()方法中, */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.item_unlock_fragment, null); list_view = (ListView) view.findViewById(R.id.list_view); tv_unlock = (TextView) view.findViewById(R.id.tv_unlock); return view; } //初始化数据要在onStart()方法中,因为onCreateView()方法只调用一次。 @Override public void onStart() { super.onStart(); appInfos = AppInfoParser.getAppInfos(getActivity()); // 获取到程序锁的dao dao = new AppLockDao(getActivity()); // 初始化一个没有加锁的集合 unLockLists = new ArrayList<AppInfo>(); for (AppInfo appInfo : appInfos) { // 判断当前的应用是否在程序所的数据里面 if (dao.find(appInfo.getPackname())) { } else { // 如果查询不到说明没有在程序锁的数据库里面 unLockLists.add(appInfo); } } adapter = new UnLockAdapter(); list_view.setAdapter(adapter); } public class UnLockAdapter extends BaseAdapter { @Override public int getCount() {//数量减少的时候会重新刷新,getCount()方法会执行, tv_unlock.setText("未加锁(" + unLockLists.size() + ")个"); return unLockLists.size(); } @Override public Object getItem(int position) { return unLockLists.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; final View view; final AppInfo appInfo; if (convertView == null) { view = View.inflate(getActivity(), R.layout.item_unlock, null); /*<?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="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/iv_icon" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1111" android:layout_gravity="center" android:layout_marginLeft="5dp" android:text="程序锁" /> <ImageView android:id="@+id/iv_unlock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/list_button_lock_default" /> </LinearLayout>*/ holder = new ViewHolder(); holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon); holder.tv_name = (TextView) view.findViewById(R.id.tv_name); holder.iv_unlock = (ImageView) view .findViewById(R.id.iv_unlock); view.setTag(holder); } else { view = convertView; holder = (ViewHolder) view.getTag(); } // 获取到当前的对象 appInfo = unLockLists.get(position); holder.iv_icon .setImageDrawable(unLockLists.get(position).getIcon()); holder.tv_name.setText(unLockLists.get(position).getName()); // 把程序添加到程序锁数据库里面,添加一个位移动画。 holder.iv_unlock.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 初始化一个位移动画 TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0,//X轴的开始位置 Animation.RELATIVE_TO_SELF, 1.0f,//X轴的结束位置(右滑) Animation.RELATIVE_TO_SELF, 0,//Y轴的开始位置 Animation.RELATIVE_TO_SELF, 0);//Y轴的结束位置 // 设置动画时间 translateAnimation.setDuration(5000); // 开始动画,动画作用在整个View上面。 view.startAnimation(translateAnimation); new Thread(){ public void run() { SystemClock.sleep(5000);//动画是开启了子线城,不然这行代码就先执行了。 getActivity().runOnUiThread(new Runnable() {//刷新界面所有要在主线程执行 @Override public void run() { // 添加到数据库里面 dao.add(appInfo.getPackname()); // 从当前的页面移除对象 unLockLists.remove(position); // 刷新界面 adapter.notifyDataSetChanged(); } }); }; }.start(); } }); return view; } } static class ViewHolder { ImageView iv_icon; TextView tv_name; ImageView iv_unlock; } }
item_unlock_fragment
<?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="vertical" > <TextView android:id="@+id/tv_unlock" android:layout_width="match_parent" android:layout_height="wrap_content" > </TextView> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>