欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實(shí)現(xiàn)ListView內(nèi)容無(wú)限循環(huán)顯示的方法

 更新時(shí)間:2017年06月27日 10:57:10   作者:Central-Perk  
這篇文章主要介紹了Android編程實(shí)現(xiàn)ListView內(nèi)容無(wú)限循環(huán)顯示的方法,通過(guò)繼承Adapter類實(shí)現(xiàn)ListView中的數(shù)據(jù)無(wú)限循環(huán)顯示功能,需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xiàn)ListView內(nèi)容無(wú)限循環(huán)顯示的方法。分享給大家供大家參考,具體如下:

其實(shí)要達(dá)到無(wú)限循環(huán)顯示,主要就是實(shí)現(xiàn)繼承Adapter的類。

我這里用到的是BaseAdapter

private class MyAdapter extends BaseAdapter{
    private Context context;
    private String[] strs = null;
    LayoutInflater inflater = null;
    public MyAdapter(Context context){
      this.context = context;
      //listview中循環(huán)顯示的數(shù)據(jù)
      strs = new String[]{"0","1","2","3","4","5","6","7","8","9"};
      inflater = LayoutInflater.from(context);
    }
    public MyAdapter(){
    }
    @Override
    public int getCount() {
      // TODO Auto-generated method stub
      //返回int可以保存的最大值,此值為2147483647
      return Integer.MAX_VALUE;
    }
    @Override
    public Object getItem(int position) {
      // TODO Auto-generated method stub
      return position;
    }
    @Override
    public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
      // TODO Auto-generated method stub
      ViewHolder holder = null;
      if(view == null){
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.textview, null);
        holder.text = (TextView) view.findViewById(R.id.text);
        view.setTag(holder);
      }else{
        holder = (ViewHolder) view.getTag();
      }
      //strs[position%strs.length]實(shí)現(xiàn)listview中數(shù)據(jù)的循環(huán)
      holder.text.setText(strs[position%strs.length]);
      return view;
    }
}
class ViewHolder{
    public TextView text;
}

在onCreate方法中將adapter賦值給listview

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    listView = (ListView) findViewById(R.id.listview);
    MyAdapter adapter = new MyAdapter(this);
    listView.setAdapter(adapter);
    listView.setSelection(Integer.MAX_VALUE/2+1);
    //設(shè)置listview初始化以后的默認(rèn)選中項(xiàng),要不然listview初始化以后只能向上拖動(dòng)而不能向下拖動(dòng)。
}

其實(shí),嚴(yán)格來(lái)說(shuō),此程序并不是無(wú)限循環(huán),只不過(guò)listview內(nèi)數(shù)據(jù)太多,達(dá)到20多個(gè)億,所以也可以算作是無(wú)限循環(huán)。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論