国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

SwipeView+ListView實(shí)現(xiàn)下拉刷新效果

2019-11-09 14:39:25
字體:
供稿:網(wǎng)友

1.編寫布局文件 SwipeView是V4包里的,可系統(tǒng)直接導(dǎo)包使用。 activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:id="@+id/swipeView"android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.soft.qianyu.swipeviewdemo.MainActivity"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView></android.support.v4.widget.SwipeRefreshLayout>

2,編寫ListView的item布局文件 這里只是簡單的使用listview,所以使用的是ArrayAdapter,不需要單獨(dú)寫item布局文件,使用的是系統(tǒng)的android.R.layout.simple_list_item_1布局文件。

3,在Activity中編寫獲得控件相應(yīng)的控件并進(jìn)行操作 MainActivity.java

public class MainActivity extends AppCompatActivity { //1.聲明各個變量 //聲明Listview、數(shù)組適配器ArrayAdapter、數(shù)據(jù)源ArrayList ListView lv; ArrayAdapter<String> adapter; ArrayList<String> datas; //聲明下拉刷新的標(biāo)記,根據(jù)這個來判斷加載哪一組數(shù)據(jù)。 boolean flag = true; //聲明刷新控件swipview SwipeRefreshLayout swipeView; @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } //2.初始化各個控件 private void init() {//初始化listview、數(shù)據(jù)源、獲取初始數(shù)據(jù)lv = (ListView) findViewById(R.id.lv);datas = new ArrayList<String>();getData();//初始化適配器:/*第一個參數(shù)是當(dāng)前上下文*第二個參數(shù)是系統(tǒng)默認(rèn)的listviewItem布局*第三個參數(shù)是數(shù)據(jù)源*/adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,datas);//為Listview設(shè)置適配器lv.setAdapter(adapter);//3.以下代碼是對SwipeView的操作swipeView = (SwipeRefreshLayout) findViewById(R.id.swipeView); //修改進(jìn)度條的背景顏色swipeView.setProgressBackgroundColorSchemeColor(Color.GRAY); //修改進(jìn)度條的顏色(若數(shù)據(jù)很多,可以轉(zhuǎn)一圈換一個顏色) swipeView.setColorSchemeColors(Color.YELLOW,Color.GREEN); //為swipeView設(shè)置下拉監(jiān)聽(只有設(shè)置監(jiān)聽了才能修改數(shù)據(jù))swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //刷新listview之前先清空數(shù)據(jù)源 datas.clear(); //根據(jù)flag的值來確定加載哪一組數(shù)據(jù) if (flag){ refresh(); }else { getData(); } //修改數(shù)據(jù)后要通知控件的適配器數(shù)據(jù)改變了! adapter.notifyDataSetChanged(); //重新設(shè)置flag的值,要不然達(dá)不到刷新效果 flag =!flag; //刷新結(jié)束,關(guān)閉刷新效果 swipeView.setRefreshing(false); } }); } private void getData() { datas.add("趙云"); datas.add("關(guān)羽"); datas.add("曹操"); datas.add("呂布"); datas.add("貂蟬"); datas.add("西施"); datas.add("李師師"); } private void refresh() { datas.add("常勝將軍"); datas.add("漢壽亭候"); datas.add("奸雄"); datas.add("三國無雙"); datas.add("三國
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石狮市| 靖远县| 望江县| 金门县| 亚东县| 敦煌市| 广丰县| 新巴尔虎右旗| 巍山| 于都县| 博乐市| 鄯善县| 威信县| 古丈县| 大理市| 黄大仙区| 金门县| 台中市| 郎溪县| 外汇| 伊通| 醴陵市| 肥乡县| 通城县| 金堂县| 南和县| 虹口区| 尼木县| 宜兰市| 酒泉市| 蛟河市| 吉安市| 锡林郭勒盟| 鸡东县| 竹溪县| 通化县| 漳平市| 夏河县| 扶余县| 招远市| 建德市|