android適配器持有者模式

fragment:android

<?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" 
    android:background="#00FF00">app

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="聊天"
        android:textAppearance="?android:attr/textAppearanceLarge" />ide

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>xml

</LinearLayout>
utf-8

items:get

<?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:gravity="center_vertical"
    android:orientation="horizontal" >it

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />io

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >class

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sss"
            android:textAppearance="?android:attr/textAppearanceSmall" />import

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="jjj"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</LinearLayout>

 

package com.jxc.fragment;

import com.jxc.homeandschool.R;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment2 extends Fragment {
 
 private View view;
 int x=0;
 int max=50;
 ListView list;

// String[] str1 = { "第一個", "第二個", "第三個", "第四個", "第五個" };
// String[] str2 = { "第一個", "第二個", "第三個", "第四個", "第五個" };
 String[] str1=new String[max];
 String[] str2=new String[max];
 
 int[] i = { R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, R.drawable.ic_launcher,
   R.drawable.ic_launcher, };

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  view = inflater.inflate(R.layout.fragment2, null);
  init();

  
  
  list.setAdapter(new MyAdapter());
  return view;
 }

 private void init() {
  
  list = (ListView) view.findViewById(R.id.listView1);
  

  for(x=0;x<max;x++){
   
   System.out.println("x----"+x);
   str1[x]="聊天"+x;
   System.out.println("str1[x]----"+x);
   
   str2[x]="聊天"+x;
   System.out.println("hai----hai");
  }
 }

 
 
 public class MyAdapter extends BaseAdapter {

  @Override
  public int getCount() {
   // TODO 自動生成的方法存根
   return str1.length;
  }

  @Override
  public Object getItem(int arg0) {
   // TODO 自動生成的方法存根
   return arg0;
  }

  @Override
  public long getItemId(int arg0) {
   // TODO 自動生成的方法存根
   return arg0;
  }

  @Override
  public View getView(int arg0, View convertView , ViewGroup arg2) {
   ViewHolder holder=null;
   if(convertView ==null){
    convertView =getActivity().getLayoutInflater().inflate(R.layout.items, null);
    holder=new ViewHolder(convertView);
//    holder.im = (ImageView) convertView.findViewById(R.id.imageView1);
//    holder.tv = (TextView) convertView.findViewById(R.id.textView1);
    
    convertView.setTag(holder);
   }else{
    holder=(ViewHolder) convertView.getTag();
   }
   
  
    holder.tv1.setText(str1[arg0]);
    holder.tv2.setText(str2[arg0]);
   
   
   
   return convertView ;
  }
 }
 class ViewHolder{
  ImageView im;
  TextView tv1,tv2;
  
  ViewHolder(View v){
   im = (ImageView) v.findViewById(R.id.imageView1);
   tv1 = (TextView) v.findViewById(R.id.textView1);
   tv2 = (TextView) v.findViewById(R.id.textView2);
  }
 }
}

 

package com.jxc.fragment;

import com.jxc.fragment.Fragment2.MyAdapter;
import com.jxc.fragment.Fragment2.ViewHolder;
import com.jxc.homeandschool.R;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment3 extends Fragment {
 private View view;
 int x=0;
 int max=100;
 ListView list;

 String[] str1=new String[max];
 String[] str2=new String[max];
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  view = inflater.inflate(R.layout.fragment3, null);
  init();

  
  
  list.setAdapter(new MyAdapter());
  return view;
 }
private void init() {
  
  list = (ListView) view.findViewById(R.id.listView1);
  

  for(x=0;x<max;x++){
   
   System.out.println("x----"+x);
   str1[x]="設置"+x;
   System.out.println("str1[x]----"+x);
   
   str2[x]="設置"+x;
   System.out.println("hai----hai");
  }
 }

 
 
 public class MyAdapter extends BaseAdapter {

  @Override
  public int getCount() {
   // TODO 自動生成的方法存根
   return str1.length;
  }

  @Override
  public Object getItem(int arg0) {
   // TODO 自動生成的方法存根
   return arg0;
  }

  @Override
  public long getItemId(int arg0) {
   // TODO 自動生成的方法存根
   return arg0;
  }

  @Override  public View getView(int arg0, View convertView , ViewGroup arg2) {   ViewHolder holder=null;   if(convertView ==null){    convertView =getActivity().getLayoutInflater().inflate(R.layout.items, null);    holder=new ViewHolder(convertView);//    holder.im = (ImageView) convertView.findViewById(R.id.imageView1);//    holder.tv = (TextView) convertView.findViewById(R.id.textView1);        convertView.setTag(holder);   }else{    holder=(ViewHolder) convertView.getTag();   }         holder.tv1.setText(str1[arg0]);    holder.tv2.setText(str2[arg0]);            return convertView ;  } } class ViewHolder{  ImageView im;  TextView tv1,tv2;    ViewHolder(View v){   im = (ImageView) v.findViewById(R.id.imageView1);   tv1 = (TextView) v.findViewById(R.id.textView1);   tv2 = (TextView) v.findViewById(R.id.textView2);  } }}

相關文章
相關標籤/搜索