您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
main6.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView6_1_1" android:layout_width="match_parent" android:layout_height="fill_parent" ></ListView> </LinearLayout>
main6_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView_title_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="28sp" android:text="title" android:textColor="#55ff88" /> <TextView android:id="@+id/textView_content_2_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:text="duanluosdfsdf" /> </LinearLayout>
MainActivity6
package com.example.listview; import java.util.ArrayList; import java.util.List; import java.util.Vector; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity6 extends Activity implements OnScrollListener{ private ListView listView; private List<News> news=new Vector<News>(); private MyAdapter myDapter; //线程之间通信的桥梁 private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case 1: //通知适配器,更新数据 myDapter.notifyDataSetChanged(); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main6); listView=(ListView) findViewById(R.id.listView6_1_1); //设置底部视图 View footView=getLayoutInflater().inflate(R.layout.load, null); listView.addFooterView(footView); listView.setOnScrollListener(this); //initDate(); new LoadDateThread().start(); //加载数据的线程 myDapter=new MyAdapter(); listView.setAdapter(myDapter); } /** * 初始化数据 * */ int index; //计数器 void initDate(){ for(int i=0;i<10;i++){ News newss=new News(); newss.title="title-"+index; newss.content="content-"+index; index++; news.add(newss); } } /** * 自定义适配器 * */ class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return news.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return news.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Viewholer viewholer; if(convertView==null){ convertView=getLayoutInflater().inflate(R.layout.main6_item, null); viewholer=new Viewholer(); viewholer.textView=(TextView) convertView.findViewById(R.id.textView_title_1_1); viewholer.textContent=(TextView) convertView.findViewById(R.id.textView_content_2_2); convertView.setTag(viewholer); }else{ viewholer=(Viewholer) convertView.getTag(); } //从veclor中取出数据并填充 News newss=news.get(position); viewholer.textView.setText(newss.title); viewholer.textContent.setText(newss.content); return convertView; } } static class Viewholer{ TextView textView; TextView textContent; } // int scrollState=0; int visiableLastIndex=0; //滚动条状态发生变化事件方法 @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub /* this.scrollState=scrollState; System.out.println("scrollState:"+scrollState);*/ //滚动条在最下面并且滚动条是停止状态 if(myDapter.getCount()==visiableLastIndex&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){ //启动线程加载数据 new LoadDateThread().start(); } } //滚动事件方法 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub System.out.println("view:"+view); System.out.println("firstVisibleItem:"+firstVisibleItem); System.out.println("visibleItemCount:"+visibleItemCount); System.out.println("totalItemCount"+totalItemCount); visiableLastIndex=firstVisibleItem+visibleItemCount-1; } //模拟加载数据 //子线程不允许访问主线程的UI组件(保证主线程UI组件安全) class LoadDateThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); initDate(); try { Thread.sleep(2000); //通知主线程,数据已加载完毕 handler.sendEmptyMessage(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
News
package com.example.listview; /** * 新闻对象 * */ public class News { String title; String content; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。