Android动态加载ListView

发布于 2024-11-19 08:31:37 字数 159 浏览 4 评论 0原文

  • 我正在使用 ArrayAdapter 在 ListView 中添加项目。
    • 首先在ListView中显示15个项目。
    • 滚动到底部,它必须从 Web API 获取并显示接下来的 15 条内容。

给一些想法。

  • I am adding item in ListView using ArrayAdapter.
    • First display 15 item in the ListView.
    • Scroll to bottom it must fetch and display next 15 content from web API.

Give some idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谈场末日恋爱 2024-11-26 08:31:37

有一个关于图像延迟加载的教程。您应该查看

http ://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/

这是来自 GitHub 的惰性列表视图的一些演示。

https://github.com/thest1/LazyList

希望这些可以提供帮助!

[感谢代码和教程的原作者]

There is a tutorial of lazy loading with image. You should check that out

http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/

Here is some demo of lazy listview from GitHub.

https://github.com/thest1/LazyList

Hope those can help !!

[Credit go to original author of code and tutorial]

执笏见 2024-11-26 08:31:37

设置 OnScrollListener。检查有多少元素可见,这是第一个可见元素。


@Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            if(((firstVisibleItem + visibleItemCount) == totalItemCount) && (totalItemCount > 0)) {
                loadNewData();
            }
        }

set OnScrollListener. Check how much elements are visible, which is the first visible element.


@Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            if(((firstVisibleItem + visibleItemCount) == totalItemCount) && (totalItemCount > 0)) {
                loadNewData();
            }
        }
雄赳赳气昂昂 2024-11-26 08:31:37

当项目可见时,将调用适配器的getView()
因此,您可以在getView(14,convertView,parent)时开始获取并显示下一个15。

getView() of the Adapter will be invoked when an item is visible.
So you can start the fetch and display next 15 when getView(14, convertView, parent).

仅一夜美梦 2024-11-26 08:31:37

从互联网加载数据是一项耗时的任务。因此,实现 AsyncTask 来处理 ListView 位图也加载在后台线程中,您可以忽略并获取其他线程,并在 onPostExecute() 中使用 setListAdapter()

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

    public class AndroidList extends ListActivity {

     public class backgroundLoadListView extends   AsyncTask {

      @Override  protected void onPostExecute(Void result) {   
// TODO Auto-generated method stub   
setListAdapter(new MyCustomAdapter(AndroidList.this, R.layout.row, month));   Toast.makeText(AndroidList.this,     "onPostExecute n: setListAdapter after bitmap preloaded",     Toast.LENGTH_LONG).show();  
}

      @Override  protected void onPreExecute() {  
 // TODO Auto-generated method stub   
Toast.makeText(AndroidList.this,     "onPreExecute n: preload bitmap in AsyncTask",     Toast.LENGTH_LONG).show();  
}

      @Override  protected Void doInBackground(Void... params) {  
 // TODO Auto-generated method stub  
 preLoadSrcBitmap();   
return null;  
}

}

     String image_URL=  "http://androidboss.com/wp-content/uploads/2010/07/83e268b443ndroid.png.png";

public class MyCustomAdapter extends ArrayAdapter {  
Bitmap bm;

      public MyCustomAdapter(Context context, int textViewResourceId,    String[] objects) 
{   
super(context, textViewResourceId, objects);  
 // TODO Auto-generated constructor stub

       bm = srcBitmap; 
  }

      @Override  public View getView(int position, View convertView, ViewGroup parent) { 
  // TODO Auto-generated method stub   
//return super.getView(position, convertView, parent);

       View row = convertView;

       if(row==null){    LayoutInflater inflater=getLayoutInflater();    row=inflater.inflate(R.layout.row, parent, false);   
 }

       TextView label=(TextView)row.findViewById(R.id.weekofday);   label.setText(month[position]);   ImageView icon=(ImageView)row.findViewById(R.id.icon);

       icon.setImageBitmap(bm);

       return row;  
 }
 }

     Bitmap srcBitmap; 
private void preLoadSrcBitmap()
{  
BitmapFactory.Options bmOptions;
  bmOptions = new BitmapFactory.Options(); 
 bmOptions.inSampleSize = 1; 
 srcBitmap = LoadImage(image_URL, bmOptions);
 }

     String[] month = {   "January", "February", "March", "April",   "May", "June", "July", "August",   "September", "October", "November", "December"   };

     /** Called when the activity is first created. */
 @Override public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState);
  //setContentView(R.layout.main);

      /*setListAdapter(new ArrayAdapter(this,        R.layout.row, R.id.weekofday, DayOfWeek));*/  new backgroundLoadListView().execute();
 }

     @Override protected void onListItemClick(ListView l, View v, int position, long id)
 {  
// TODO Auto-generated method stub  
//super.onListItemClick(l, v, position, id);
  String selection = l.getItemAtPosition(position).toString(); 
 Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
  }

     private Bitmap LoadImage(String URL, BitmapFactory.Options options) {     
   Bitmap bitmap = null;  
InputStream in = null;       
 try { 
  in = OpenHttpConnection(URL);  
 bitmap = BitmapFactory.decodeStream(in, null, options);   in.close(); 
  } catch (IOException e1)
 {  }

      return bitmap;                 }

     private InputStream OpenHttpConnection(String strURL) throws IOException{  
InputStream inputStream = null;  
URL url = new URL(strURL);  
URLConnection conn = url.openConnection();

      try{
   HttpURLConnection httpConn = (HttpURLConnection)conn;  
 httpConn.setRequestMethod("GET");   
httpConn.connect();

       if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {   
 inputStream = httpConn.getInputStream();   
  } 
   }  catch (Exception ex){  }

      return inputStream; }
}

It’s a time-consume task to load data from internet. So AsyncTask is implemented to handle the ListView the bitmap is also loaded in background thread which u can ignore and take the others, and setListAdapter() in onPostExecute().

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

    public class AndroidList extends ListActivity {

     public class backgroundLoadListView extends   AsyncTask {

      @Override  protected void onPostExecute(Void result) {   
// TODO Auto-generated method stub   
setListAdapter(new MyCustomAdapter(AndroidList.this, R.layout.row, month));   Toast.makeText(AndroidList.this,     "onPostExecute n: setListAdapter after bitmap preloaded",     Toast.LENGTH_LONG).show();  
}

      @Override  protected void onPreExecute() {  
 // TODO Auto-generated method stub   
Toast.makeText(AndroidList.this,     "onPreExecute n: preload bitmap in AsyncTask",     Toast.LENGTH_LONG).show();  
}

      @Override  protected Void doInBackground(Void... params) {  
 // TODO Auto-generated method stub  
 preLoadSrcBitmap();   
return null;  
}

}

     String image_URL=  "http://androidboss.com/wp-content/uploads/2010/07/83e268b443ndroid.png.png";

public class MyCustomAdapter extends ArrayAdapter {  
Bitmap bm;

      public MyCustomAdapter(Context context, int textViewResourceId,    String[] objects) 
{   
super(context, textViewResourceId, objects);  
 // TODO Auto-generated constructor stub

       bm = srcBitmap; 
  }

      @Override  public View getView(int position, View convertView, ViewGroup parent) { 
  // TODO Auto-generated method stub   
//return super.getView(position, convertView, parent);

       View row = convertView;

       if(row==null){    LayoutInflater inflater=getLayoutInflater();    row=inflater.inflate(R.layout.row, parent, false);   
 }

       TextView label=(TextView)row.findViewById(R.id.weekofday);   label.setText(month[position]);   ImageView icon=(ImageView)row.findViewById(R.id.icon);

       icon.setImageBitmap(bm);

       return row;  
 }
 }

     Bitmap srcBitmap; 
private void preLoadSrcBitmap()
{  
BitmapFactory.Options bmOptions;
  bmOptions = new BitmapFactory.Options(); 
 bmOptions.inSampleSize = 1; 
 srcBitmap = LoadImage(image_URL, bmOptions);
 }

     String[] month = {   "January", "February", "March", "April",   "May", "June", "July", "August",   "September", "October", "November", "December"   };

     /** Called when the activity is first created. */
 @Override public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState);
  //setContentView(R.layout.main);

      /*setListAdapter(new ArrayAdapter(this,        R.layout.row, R.id.weekofday, DayOfWeek));*/  new backgroundLoadListView().execute();
 }

     @Override protected void onListItemClick(ListView l, View v, int position, long id)
 {  
// TODO Auto-generated method stub  
//super.onListItemClick(l, v, position, id);
  String selection = l.getItemAtPosition(position).toString(); 
 Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
  }

     private Bitmap LoadImage(String URL, BitmapFactory.Options options) {     
   Bitmap bitmap = null;  
InputStream in = null;       
 try { 
  in = OpenHttpConnection(URL);  
 bitmap = BitmapFactory.decodeStream(in, null, options);   in.close(); 
  } catch (IOException e1)
 {  }

      return bitmap;                 }

     private InputStream OpenHttpConnection(String strURL) throws IOException{  
InputStream inputStream = null;  
URL url = new URL(strURL);  
URLConnection conn = url.openConnection();

      try{
   HttpURLConnection httpConn = (HttpURLConnection)conn;  
 httpConn.setRequestMethod("GET");   
httpConn.connect();

       if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {   
 inputStream = httpConn.getInputStream();   
  } 
   }  catch (Exception ex){  }

      return inputStream; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文