2013-12-18 85 views
0

我正在使用示例來顯示具有圖像和文本的列表視圖。列表顯示時間過長。所以我想要的是使用這個列表作爲懶惰的圖像列表,以便所有的圖像不會立即加載,但會在用戶向下滾動時加載。如何在懶列表視圖中顯示可繪製的文件夾圖像

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; 
} 

     } 

回答

1

首先,在圖像視圖中放置一些虛擬圖像。一旦準備就緒,您可以使用異步任務來加載圖像。

+0

ü可以給我例子。我是begineer。 – kriss

0

您可以使用庫(如Picasso Library)從可繪製文件夾加載圖像。

我假設您正在使用自定義適配器來加載圖像和文本列表。 在適配器的getView()方法中使用下面的代碼片段。

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

希望你會發現這有幫助。

此致敬禮!

相關問題