亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用PullToRefresh實(shí)現(xiàn)上拉加載和下拉刷新效果的代碼

 更新時(shí)間:2016年07月20日 11:21:58   作者:南塵  
這篇文章主要介紹了Android使用PullToRefresh實(shí)現(xiàn)上拉加載和下拉刷新效果 的相關(guān)資料,需要的朋友可以參考下

在沒給大家介紹正文之前,先給大家介紹展示下運(yùn)行圖,如果大家感覺還不錯(cuò),請(qǐng)繼續(xù)往下閱讀:

相關(guān)閱讀:分享Android中pullToRefresh的使用心得

項(xiàng)目已同步至:https://github.com/nanchen2251/pullToRefreshDemo

簡(jiǎn)單使用詳情:

1)studio可以直接在app的module設(shè)置中直接進(jìn)行搜索,但是有-的必須添上,而不能用空格代替,為了更加了解這個(gè)東西,我還是推薦大家去這里看看,奉上網(wǎng)址:
https://github.com/chrisbanes/Android-PullToRefresh

所以去git上下載了后通過(guò)studio的導(dǎo)入Module功能,導(dǎo)入library,修改library的gradle文件和自己的項(xiàng)目一致

apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "24.0.0"
defaultConfig {
minSdkVersion 18
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}

2)然后添加依賴,將app與其進(jìn)行關(guān)聯(lián)。

3)下面打開library可以看到很多的東西,這個(gè)不僅可以支持ListView,還可以支持GridView和ScollView等等,可謂相當(dāng)全面,不過(guò)是否能和當(dāng)前火熱的RecyclerView一起使用樓主還沒試過(guò)。

4)下面在我們的xml布局中布局,這里我用了自定義控件的屬性,所以添加了一個(gè)xmlns:app=http://schemas.android.com/apk/res-auto

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.nanchen.pulltorefreshdemo.MainActivity">
  <com.handmark.pulltorefresh.library.PullToRefreshListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_pull_refresh_lv"
    app:ptrAnimationStyle="flip"
    app:ptrHeaderBackground="@android:color/transparent"
    app:ptrHeaderTextColor="#919191"/>
</RelativeLayout>

5)由于我們這里使用的是它的ListView,所以我們需要一個(gè)java Bean 和一個(gè)Item的layout進(jìn)行自定義布局。

package com.example.nanchen.pulltorefreshdemo;
/**
 * Created by 南塵 on 16-7-20.
 */
public class Music {
  private String title;
  private String singer;
  public Music() {
  }
  public Music(String title, String singer) {
    this.title = title;
    this.singer = singer;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public String getSinger() {
    return singer;
  }
  public void setSinger(String singer) {
    this.singer = singer;
  }
}

還有l(wèi)ist_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_title"
android:text="歌曲1"
android:textSize="20sp"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_singer"
android:textSize="20sp"
android:text="歌手1"
android:layout_alignParentRight="true"/>
</RelativeLayout>

6)使用非常簡(jiǎn)單,在Activity中,代碼注釋已經(jīng)比較全面了,這里模擬了異步下載數(shù)據(jù)任務(wù),并且重寫了Adaper,至于其中為什么用靜態(tài)內(nèi)部類,這個(gè)好處很多,大家可以百度科普。

package com.example.nanchen.pulltorefreshdemo;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
  private PullToRefreshListView refresh_lv;
  private List<Music> list;
  private DataAdapter adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    refresh_lv = (PullToRefreshListView) findViewById(R.id.main_pull_refresh_lv);
    list = new ArrayList<>();
    //設(shè)置可上拉刷新和下拉刷新
    refresh_lv.setMode(PullToRefreshBase.Mode.BOTH);
    //設(shè)置刷新時(shí)顯示的文本
    ILoadingLayout startLayout = refresh_lv.getLoadingLayoutProxy(true,false);
    startLayout.setPullLabel("正在下拉刷新...");
    startLayout.setRefreshingLabel("正在玩命加載中...");
    startLayout.setReleaseLabel("放開以刷新");
    ILoadingLayout endLayout = refresh_lv.getLoadingLayoutProxy(false,true);
    endLayout.setPullLabel("正在上拉刷新...");
    endLayout.setRefreshingLabel("正在玩命加載中...");
    endLayout.setReleaseLabel("放開以刷新");
    refresh_lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
      @Override
      public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        new LoadDataAsyncTask(MainActivity.this).execute();
      }
      @Override
      public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        new LoadDataAsyncTask(MainActivity.this).execute();
      }
    });
    loadData();
    adapter = new DataAdapter(this,list);
    refresh_lv.setAdapter(adapter);
  }
  private int count = 1;
  private void loadData(){
    for (int i = 0; i < 10; i++) {
      list.add(new Music("歌曲"+count,"歌手"+count));
      count++;
    }
  }
  /**
   * 異步下載任務(wù)
   */
  private static class LoadDataAsyncTask extends AsyncTask<Void,Void,String>{
    private MainActivity mainActivity;
    public LoadDataAsyncTask(MainActivity mainActivity) {
      this.mainActivity = mainActivity;
    }
    @Override
    protected String doInBackground(Void... params) {
      try {
        Thread.sleep(2000);
        mainActivity.loadData();
        return "seccess";
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return null;
    }
    /**
     * 完成時(shí)的方法
     */
    @Override
    protected void onPostExecute(String s) {
      super.onPostExecute(s);
      if (s.equals("seccess")){
        mainActivity.adapter.notifyDataSetChanged();
        mainActivity.refresh_lv.onRefreshComplete();//刷新完成
      }
    }
  }
  /**
   * 自定義適配器
   */
  private static class DataAdapter extends BaseAdapter{
    private Context context;
    private List<Music> list;
    public DataAdapter(Context context, List<Music> list) {
      this.context = context;
      this.list = list;
    }
    @Override
    public int getCount() {
      if (list != null){
        return list.size();
      }
      return 0;
    }
    @Override
    public Object getItem(int position) {
      return list.get(position);
    }
    @Override
    public long getItemId(int position) {
      return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder vh;
      if (convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);
        vh = new ViewHolder();
        vh.tv_title = (TextView) convertView.findViewById(R.id.item_title);
        vh.tv_singer = (TextView) convertView.findViewById(R.id.item_singer);
        convertView.setTag(vh);
      }else{
        vh = (ViewHolder) convertView.getTag();
      }
      Music music = (Music) getItem(position);
      vh.tv_title.setText(music.getTitle());
      vh.tv_singer.setText(music.getSinger());
      return convertView;
    }
    class ViewHolder{
      TextView tv_title;
      TextView tv_singer;
    }
  }
}

以上所述是小編給大家介紹的Android使用PullToRefresh實(shí)現(xiàn)上拉加載和下拉刷新效果的代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論