如何在惰性列表视图中显示可绘制的文件夹图像

时间:2013-12-18 10:24:18

标签: android lazylist

我正在使用一个示例来显示带有图像和文本的列表视图。列表显示时需要花费太多时间。所以我想要的是将此列表用作一个惰性图像列表,以便所有图像不会立即加载,而是在用户向下滚动时加载。

public class SetImageText {
public int icon;
public String title;
public SetImageText()
     {
 super();
 }

public SetImageText(int icon,String title)
{
    super();
    this.icon=icon;
    this.title=title;
}
      }

 //Main Class 
         package com.example.customizearrayadapter;
         import android.R.layout;
         public class MainActivity extends Activity {
         private ListView listView1;
      @Override
         public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         SetImageText data[]=new SetImageText[]
        {
         new SetImageText(R.drawable.limage,"Cloudy"),
         new SetImageText(R.drawable.limage,"Showers"),
         new SetImageText(R.drawable.limage,"snow"),
         new SetImageText(R.drawable.limage,"Storm"),
         new SetImageText(R.drawable.limage,"Sunny"),
         new SetImageText(R.drawable.cow, "kriss"),
         new SetImageText(R.drawable.dog,"wazid"),
         new SetImageText(R.drawable.hen, "anuj"),
         new SetImageText(R.drawable.horse, "abhay"),
         new SetImageText(R.drawable.kbc, "abid"),
         new SetImageText(R.drawable.mj ,"aman"),
         new SetImageText(R.drawable.penguine ,"ali"),
        new SetImageText(R.drawable.rat, "hemant"),
        new SetImageText(R.drawable.pizza, "jai"),
        new SetImageText(R.drawable.tortoise ,"kapil")
     };

       SetImageTextArrayAdapter adapter=new        SetImageTextArrayAdapter(this,R.layout.listview_row_item,data);
    listView1=(ListView)findViewById(R.id.listView1);
    View header=(View)getLayoutInflater().inflate(R.layout.listview_header,null);
    listView1.addHeaderView(header);
    listView1.setAdapter(adapter);
}


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
}


     package com.example.customizearrayadapter;
     import java.util.Set;
     import android.app.Activity;
     import android.content.Context;

    public class SetImageTextArrayAdapter  extends ArrayAdapter<SetImageText>{

Context context;
int layoutResourceId;
SetImageText data[]=null;

public SetImageTextArrayAdapter(Context context, int layoutResourceId,
        SetImageText[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId=layoutResourceId;
    this.context=context;
    this.data=data;
    // TODO Auto-generated constructor stub
}
public View getView(int position,View convertView,ViewGroup parent)
{
     View row = convertView;
        SetImageTextHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new SetImageTextHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (SetImageTextHolder)row.getTag();
        }
        SetImageText setimagetext = data[position];
        holder.txtTitle.setText(setimagetext.title);
        holder.imgIcon.setImageResource(setimagetext.icon);

        return row;



}



static class SetImageTextHolder
{
    ImageView imgIcon;

    TextView txtTitle;
}

       }

3 个答案:

答案 0 :(得分:1)

首先,在图像视图中放置一些虚拟图像。并且您可以使用Async任务在图像准备好后加载它们。

答案 1 :(得分:0)

您可以使用Picasso Library等库来加载可绘制文件夹中的图像。

我假设您正在使用自定义适配器来加载图像和文本列表。 在适配器的getView()方法中使用下面的代码段。

Picasso.with(context).load(R.drawable.name_of_your_image).into(imageView1);

希望你会发现这有用。

祝你好运!

答案 2 :(得分:0)

在这种情况下,您需要使用排球库来处理FILE,DRAWBLE之类的任何内容......

Press link..this example solve issue of drawable