当前位置: 编程技术>移动开发
本页文章导读:
▪关于点击状态栏不回到顶部有关问题解决办法 关于点击状态栏不回到顶部问题解决方法
// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate.........
▪ 在多个Activity其间共享变量 在多个Activity之间共享变量
话说以前做的一个纯Java引擎准备移植到Android上面,前期已做过Demo,看起来一切都没有什么问题。不过,现在真正移植问题就来了。
第一个问题就.........
▪ 基于SD卡得资料选择器 基于SD卡得文件选择器
◦public class ActivityFilePicker extends Activity {
◦
◦ private ListView listViewFilePicker;
◦ private File mCurrentDirectory = new File(Environment.getExternalStorageDirectory().getAbsoluteP.........
[1]关于点击状态栏不回到顶部有关问题解决办法
来源: 互联网 发布时间: 2014-02-18
关于点击状态栏不回到顶部问题解决方法
// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
@property(nonatomic) BOOL scrollsToTop; // default is YES.
设置这个属性,可以让点击状态栏不回到顶部,但是如果我们需要让他回到顶部,程序又不响应操作,解决方法
刚才上面的官方文档说了,只有当一个主控制器有一个scrollview 并把这个属性设置为yes,其他的scrollview.scrollsToTop = NO 这样才会响应这个事件,原理很简单,如果有2个scrollview,系统根本不知道你需要哪个滚动到最上面
// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
@property(nonatomic) BOOL scrollsToTop; // default is YES.
设置这个属性,可以让点击状态栏不回到顶部,但是如果我们需要让他回到顶部,程序又不响应操作,解决方法
刚才上面的官方文档说了,只有当一个主控制器有一个scrollview 并把这个属性设置为yes,其他的scrollview.scrollsToTop = NO 这样才会响应这个事件,原理很简单,如果有2个scrollview,系统根本不知道你需要哪个滚动到最上面
[2] 在多个Activity其间共享变量
来源: 互联网 发布时间: 2014-02-18
在多个Activity之间共享变量
话说以前做的一个纯Java引擎准备移植到Android上面,前期已做过Demo,看起来一切都没有什么问题。不过,现在真正移植问题就来了。
第一个问题就是,以前的Demo只是一个画面,引擎初期化是放在Activity的onCreate方法里面,这样做当然没有什么问题啦。现在画面增加到10来个,这个引擎如何为各个Activity公用成为了一个问题。
对Android不熟悉,也只算是边学边做,所以有了下面的想法:
1、用Service
2、用Thread
3、用Application
现在用了第3种方法成功了。
第1、2中方法我想本质上都是启动一个独立于Activity的线程,但是无法和Activity挂上关系。所以肯定是NullPointException啦。
具体做法:
很简单,引擎(或者某个普通的类)需要初始化,而且在应用程序不被销毁之前报保证被各个Activity可用。
1、自定义一个类MyApplication来继承Application,在这个类中的onCreate方法里面初始化引擎。
2、为了在各个Activity类中可以调用引擎,需要在1的自定义类中写一个getEngine的方法。
class MyApplication extends Application { private Engine engine; public Engine getEngine(){ return engine; } public void onCreate(){ engine = new Engine(); engine.init(); } }
3、在Activity中通过获取引擎对象,并调用getEngine
MyApplication app = ((MyApplication)this.getApplicationContext()); Engine engine = app.getEngine();
4、在AndroidMainfest.xml文件中将Application节点的Android:name改成我们自定义的类。
以上
[3] 基于SD卡得资料选择器
来源: 互联网 发布时间: 2014-02-18
基于SD卡得文件选择器
由于模块结构很简单,所以把遍历文件的方法放在Activity上了
◦/**
◦ * 文件结构
◦ * @author Administrator
◦ *
◦ */
◦public class FileData {
◦ public String name; //文件名
◦ public int type; //文件类型
◦}
◦public class ActivityFilePicker extends Activity { ◦ ◦ private ListView listViewFilePicker; ◦ private File mCurrentDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); //根目录位置 ◦ AdapterFilePicker mFileAdapter; //ListView适配器 ◦ String fileEndings[] = { "mp3" }; //显示的文件类型 ◦ ◦ @Override ◦ public void onCreate(Bundle savedInstanceState) { ◦ super.onCreate(savedInstanceState); ◦ setContentView(R.layout.layout_activityfilepicker); ◦ listViewFilePicker = (ListView) findViewById(R.id.listview); ◦ listViewFilePicker.setCacheColorHint(0x00000000); ◦ mFileAdapter = new AdapterFilePicker(this); ◦ listViewFilePicker.setAdapter(mFileAdapter); ◦ //Item点击事件 ◦ ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() { ◦ public void onItemClick(AdapterView parent, View view, ◦ int position, long id) { ◦ int fid = mFileAdapter.getItemType((int) id); ◦ String mPath = ""; ◦ if (fid == 1) { //文件类型1为文件夹,点击进入该文件夹目录 ◦ String s1 = mFileAdapter.getItem((int) id).name; ◦ if (s1.equals("..")) { ◦ mPath = mCurrentDirectory.getParent(); ◦ } else { ◦ mPath = mCurrentDirectory.getPath() + "/" + s1 + "/"; ◦ } ◦ mCurrentDirectory = new File(mPath); ◦ ListFile(mCurrentDirectory); ◦ } else { //返回选中的文件的Path ◦ Bundle bundle = new Bundle(); ◦ bundle.putString("path", mCurrentDirectory.getPath() ◦ + "/" + mFileAdapter.getItem((int) id).name); ◦ Intent mIntent = new Intent(); ◦ mIntent.putExtras(bundle); ◦ setResult(RESULT_OK, mIntent); ◦ ActivityFilePicker.this.finish(); ◦ } ◦ } ◦ }; ◦ ListFile(mCurrentDirectory); ◦ listViewFilePicker.setOnItemClickListener(lv2click); ◦ } ◦ ◦ /** ◦ * 列出该目录的文件 ◦ * @param aDirectory ◦ */ ◦ private void ListFile(File aDirectory) { ◦ mFileAdapter.clearItems(); ◦ mFileAdapter.notifyDataSetChanged(); ◦ listViewFilePicker.postInvalidate(); ◦ if (!aDirectory.getPath().equals("/sdcard")) { ◦ FileData fd = new FileData(); ◦ fd.name = ".."; ◦ fd.type = 1; ◦ mFileAdapter.addItem(fd); ◦ } ◦ for (File f : aDirectory.listFiles()) { ◦ if (f.isDirectory()) { ◦ FileData fd = new FileData(); ◦ fd.name = f.getName(); ◦ fd.type = 1; ◦ mFileAdapter.addItem(fd); ◦ } else { ◦ if (checkEnds(f.getName().toLowerCase())) { ◦ FileData fd = new FileData(); ◦ fd.name = f.getName(); ◦ fd.type = 0; ◦ mFileAdapter.addItem(fd); ◦ } ◦ } ◦ } ◦ mFileAdapter.notifyDataSetChanged(); ◦ listViewFilePicker.postInvalidate(); ◦ } ◦ ◦ private boolean checkEnds(String checkItsEnd) { ◦ for (String aEnd : fileEndings) { ◦ if (checkItsEnd.endsWith(aEnd)) ◦ return true; ◦ } ◦ return false; ◦ } ◦}
由于模块结构很简单,所以把遍历文件的方法放在Activity上了
◦public class AdapterFilePicker extends BaseAdapter { ◦ private Context mContext; ◦ private Vector mItems = new Vector(); ◦ private LinearLayout layout, layout_more; ◦ private LayoutInflater inflate; ◦ private TextView textViewItem; ◦ ◦ public AdapterFilePicker(Context context) { ◦ mContext = context; ◦ inflate = (LayoutInflater) mContext ◦ .getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE); ◦ } ◦ ◦ public void addItem(FileData item) { ◦ mItems.add(item); ◦ } ◦ ◦ public FileData getItem(int it) { ◦ return (FileData) mItems.elementAt(it); ◦ } ◦ ◦ public int getCount() { ◦ return mItems.size(); ◦ } ◦ ◦ public long getItemId(int arg0) { ◦ return arg0; ◦ } ◦ ◦ public int getItemType(int arg0) { ◦ return getItem(arg0).type; ◦ } ◦ ◦ public void clearItems() { ◦ mItems.clear(); ◦ } ◦ ◦ public View getView(int position, View view, ViewGroup arg2) { ◦ if(null == view){ ◦ view = (LinearLayout) inflate.inflate(R.layout.item_filepicker_listview, null); ◦ } ◦ textViewItem = (TextView) view.findViewById(R.id.textViewFileName); ◦ textViewItem.setText(getItem(position).name); ◦ return view; ◦ } ◦ ◦}
◦/**
◦ * 文件结构
◦ * @author Administrator
◦ *
◦ */
◦public class FileData {
◦ public String name; //文件名
◦ public int type; //文件类型
◦}
最新技术文章: