Android 自带SwipeRefreshLayout 下拉刷新简单用法

布局:

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:id="@+id/swipeRefreshLayout"
    android:layout_height="wrap_content">
    <ListView
        android:layout_width="match_parent"
        android:id="@+id/listView_main"
        android:layout_height="wrap_content">
    </ListView>
</android.support.v4.widget.SwipeRefreshLayout>

方法:

public class MainActivity extends AppCompatActivity {
    ListView listView;
    SwipeRefreshLayout swipeRefreshLayout;
    ArrayList<String> list;
    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=new ArrayList<>();
        list.add("aaaaa");
        swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        //刷新条变颜色
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,android.R.color.holo_green_dark,
                android.R.color.black,android.R.color.holo_orange_dark);
        listView= (ListView) findViewById(R.id.listView_main);
        arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(arrayAdapter);

        //下拉监听
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                list.add(0,"oooo");
                arrayAdapter.notifyDataSetChanged();        //刷新
                swipeRefreshLayout.setRefreshing(false);    //停止刷新
            }
        });
    }

猜你喜欢

转载自blog.csdn.net/hbw020/article/details/53160562