Android SwipeRefreshLayout仿抖音app静态刷新

SwipeRefreshLayout的功能就是可以让我们的界面在不动的情况下,下拉直接刷新

废话不多说,效果图奉上:

activity_listview布局文件

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

Activity代码(ListViewActivity)

public class ListViewActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { 
 private SwipeRefreshLayout swipeRefreshLayout; 
 private ListView listView; 
 private List<String> list; 
 private ArrayAdapter adapter; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_list_view); 
  swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.sr1); 
  swipeRefreshLayout.setOnRefreshListener(this); 
  list = new ArrayList<>(); 
  list.add("ssss"); 
  listView = (ListView) findViewById(R.id.lv); 
  adapter = new ArrayAdapter(this 
    , android.R.layout.simple_list_item_1 
    , android.R.id.text1 
    , list); 
  listView.setAdapter(adapter); 
 } 
 
 @Override 
 public void onRefresh() { 
  new Handler().postDelayed(new Runnable() { 
   @Override 
   public void run() { 
    swipeRefreshLayout.setRefreshing(false); 
    adapter.clear(); 
    list.add("1111"); 
    adapter.notifyDataSetChanged(); 
   } 
  }, 1000); 
 } 
} 


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。