android studio 项目中引入第三方lib
本文以引入https://github.com/chrisbanes/Android-PullToRefresh为例
借鉴参考:http://www.apkbus.com/android-125663-1-1.html
本人电脑上的android studio是1.2.1.1 ,sdk api22.
正常创建一个project(本例中project为名"demo")
开始为项目引入PullToRefresh类库
按下键盘上“F12”或File->new->import module,如下
依次导入android-pulltorefresh目录下的library、extras\PullToRefreshListFragment、extras\PullToRefreshListFragment
完成后目录如下
b.为demo下的app modules添加类库依赖。
完成后结果如上。点击“OK”完成。
3.解决编译错误
重新按"F12",修改除app外的其他三个modules中的min sdk和target sdk版本,与app相同
ok,编译可以正常通过了。
4、添加一个测试示例。
修改:layout/activity_main.xml文件
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="demo.zhezi.MainActivity"android:orientation="vertical"><com.handmark.pulltorefresh.library.PullToRefreshListViewandroid:id="@+id/listview"android:layout_width="fill_parent"android:layout_height="fill_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView></LinearLayout>
修改:MainActivity.java文件
privatePullToRefreshListViewlistView;privateArrayAdapter<String>arrayAdapter;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(PullToRefreshListView)findViewById(R.id.listview);arrayAdapter=newArrayAdapter<String>(this,android.R.layout.simple_list_item_1);arrayAdapter.add("welcome");arrayAdapter.add("welcome1");arrayAdapter.add("welcome2");arrayAdapter.add("welcome3");listView.setAdapter(arrayAdapter);listView.setOnRefreshListener(newOnRefreshListener<ListView>(){@OverridepublicvoidonRefresh(PullToRefreshBase<ListView>refreshView){newAsyncTask<Void,Void,Void>(){@OverrideprotectedVoiddoInBackground(Void...params){try{Thread.sleep(3000);}catch(InterruptedExceptione){e.printStackTrace();}returnnull;}protectedvoidonPostExecute(Voidresult){arrayAdapter.addAll("Hello","大家好");listView.onRefreshComplete();};}.execute();}});
好了。展示效果如下
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。