Android custom ListView 3- serial interface, performance optimization and bind click event

First, the custom interface ListView

1. The first establish a Fruit Fruit

 

package com.example.listviewtest;

​


public class Fruit {

  private String name;

 

  private int imageId;

 

  public Fruit(String name,int imageId) {

    this.name=name;

    this.imageId = imageId;

  }

  public String getName() {

    return name;

  }

  public int getImageId() {

    return imageId;

   

  }

}

 

2. LayOut file and then create a display of fruit

 

<?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" >

   

<ImageView

    android:id="@+id/fruit_image"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" /><TextView

    android:id="@+id/fruit_name"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_gravity="center"

    android:layout_marginLeft="10dip" />

   

</LinearLayout>

 

Then create a FruitAdapter adapter to inherit ArrayAdapter

 

package com.example.listviewtest;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import android.content.Context;

import java.util.List;import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



public class FruitAdapter extends ArrayAdapter<Fruit> {

  

  private int resourceId;

 

  public FruitAdapter(Context context,int textViewResourceId,List<Fruit> objects) {

    super(context,textViewResourceId,objects);

    resourceId = textViewResourceId;

  }

 

  @Override

  public View getView(int position,View convertView,ViewGroup parent) {

    Fruit Fruit = the getItem (position); // Get current item Fruit Examples 

    View View = LayoutInflater.from (the getContext ()) the inflate (The resourceId,. Null );

    ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);

    TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);

    fruitImage.setImageResource(fruit.getImageId());

    fruitName.setText(fruit.getName());

    return view;

   

  }

}

 

Finally, we refine our main program

 

package com.example.listviewtest;

​

import java.util.List;


import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import java.util. * ;

//import java.lang.ArrayAdapter;


public class MainActivity extends Activity {

 

  private List<Fruit> fruitList = new ArrayList<Fruit>();

 

//  private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry"};

 

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super .onCreate (savedInstanceState);

    setContentView(R.layout.activity_main);

   

    initFruits (); // initialize data fruit 

    FruitAdapter Adapter = new new FruitAdapter (. the MainActivity the this , R.layout.fruit_item, fruitList);

   

//    ArrayAdapter<String> adapter = new ArrayAdapter<String>(

//        MainActivity.this,android.R.layout.simple_list_item_1,data);

    ListView listView = (ListView) findViewById(R.id.list_view);

    listView.setAdapter(adapter);

   

  }

  private void initFruits() {

    Fruit apple = new Fruit("Apple",R.drawable.apple_pic);

    fruitList.add(apple);

    Fruit banana = new Fruit("Banana",R.drawable.banana_pic);

    fruitList.add(banana);

    Fruit orange =new Fruit("Orange",R.drawable.orange_pic);

    fruitList.add(orange);

       

  }

}

 

We run this code, the following results

 

 

Second, enhance operational efficiency ListView

1. We can see by the following annotation content, reconstructed this piece of code, you can improve the fluency of the screen, and a cache View Holder object can greatly enhance the speed.

 

package com.example.listviewtest;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import android.content.Context;

import java.util.List;import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



public class FruitAdapter extends ArrayAdapter<Fruit> {

 

 

 

  private int resourceId;

 

  public FruitAdapter(Context context,int textViewResourceId,List<Fruit> objects) {

    super(context,textViewResourceId,objects);

    resourceId = textViewResourceId;

  }

 

  @Override

  public View getView(int position,View convertView,ViewGroup parent) {

    Fruit Fruit = the getItem (position); // Get the current item Fruit Examples

//    View view = LayoutInflater.from(getContext()).inflate(resourceId,null);

    // above we commented a View instance, our next line of first instance to initialize a View

    View view;

    ViewHolder viewHolder;

    IF (convertView == null ) { // If the View does not exist, we just build a View 

      View = LayoutInflater.from (getContext ()) inflate (resourceId,. null );

      viewHolder = new ViewHolder();

      viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);

      viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);

      view.setTag (viewHolder); // be stored in View ViewHolder

     

    }else {

      View = convertView; // if the View has been in existence, then we reuse of View has been in existence, so you can avoid regenerate View again, saving run time 

      viewHolder = (ViewHolder) view.getTag (); // call getTag method, the ViewHolder removed again.

    }

    // Here we set up an internal class, for instance fruitImage and fruieName storage has been generated and look for the target instance on the province's every need by means of findViewById, come and memory

    viewHolder.fruitImage.setImageResource(fruit.getImageId());

    viewHolder.fruitName.setText(fruit.getName());

​

//    ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);

//    TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);

//    fruitImage.setImageResource(fruit.getImageId());

//    fruitName.setText(fruit.getName());

    return view;

   

  }

  class ViewHolder{

    ImageView fruitImage;

   

    TextView fruitName;

  }

}

 

Three, ListView click event

If only listed a few contents, we can not get to by clicking on the List to the information we want to know, it would be meaningless.

Then in MainActivity to register in our click event.

 

package com.example.listviewtest;

​

import java.util.List;


import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;


import java.util. * ;

//import java.lang.ArrayAdapter;
public class MainActivity extends Activity {

 

  private List<Fruit> fruitList = new ArrayList<Fruit>();

 

//  private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry"};

 

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super .onCreate (savedInstanceState);

    setContentView(R.layout.activity_main);

   

    initFruits (); // initialize data fruit 

    FruitAdapter Adapter = new new FruitAdapter (. the MainActivity the this , R.layout.fruit_item, fruitList);

   

//    ArrayAdapter<String> adapter = new ArrayAdapter<String>(

//        MainActivity.this,android.R.layout.simple_list_item_1,data);

    ListView listView = (ListView) findViewById(R.id.list_view);

    listView.setAdapter(adapter);

   

    // Then on to register the click event 

    listView.setOnItemClickListener ( new new OnItemClickListener () {

      @Override

      public void onItemClick(AdapterView<?> parent,View view,int position,long id) {

        Fruit fruit = fruitList.get(position);

        Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();

      }

    });

   

  }

  private void initFruits() {

    Fruit apple = new Fruit("Apple",R.drawable.apple_pic);

    fruitList.add(apple);

    Fruit banana = new Fruit("Banana",R.drawable.banana_pic);

    fruitList.add(banana);

    Fruit orange =new Fruit("Orange",R.drawable.orange_pic);

    fruitList.add(orange);

       

  }

}

 

 

Fourth, the source code:

1. Project address

https://github.com/ruigege66/Android/tree/master/ListViewTest

2.CSDN:https://blog.csdn.net/weixin_44630050

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/12657337.html