Listview优化MovieListAdapter的使用
MovieListAdapter.java
1 package com.maomao.beautymovie.adapter; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import com.maomao.beautymovie.R; 6 import com.maomao.beautymovie.bean.MovieBean; 7 import com.maomao.beautymovie.commom.CommonUtils; 8 import com.maomao.beautymovie.commom.Constants; 9 10 import android.content.Context; 11 import android.view.LayoutInflater; 12 import android.view.View; 13 import android.view.ViewGroup; 14 import android.widget.BaseAdapter; 15 import android.widget.TextView; 16 17 /** 18 * 19 * @author NINGXUEFENG 20 * 21 */ 22 public class MovieListAdapter extends BaseAdapter { 23 private List<MovieBean> data = new ArrayList<MovieBean>(); 24 private Context context; 25 26 public MovieListAdapter(Context context,List<MovieBean> data){ 27 this.context = context; 28 this.data = data; 29 } 30 //适配器根据getCount()函数来确定要加载多少项 31 @Override 32 public int getCount() { 33 return data.size(); 34 } 35 36 @Override 37 public Object getItem(int paramInt) { 38 39 return data.get(paramInt); 40 //return null; 41 } 42 43 @Override 44 public long getItemId(int paramInt) { 45 return paramInt; 46 } 47 48 @Override 49 public View getView(int paramInt, View convertView, ViewGroup paramViewGroup) { 50 51 ViewHolder holder = null; 52 //优化ListView 53 if (convertView == null) { 54 holder = new ViewHolder(); 55 convertView = LayoutInflater.from(context).inflate(R.layout.movie_listview_item, null); 56 //通过view来得到Item中的每个控件的操作权 57 holder.name = (TextView)convertView.findViewById(R.id.tv_mname); 58 holder.type = (TextView)convertView.findViewById(R.id.tv_mtype); 59 holder.subtitle = (TextView)convertView.findViewById(R.id.tv_subtitle); 60 holder.director = (TextView)convertView.findViewById(R.id.director); 61 holder.seennum = (TextView)convertView.findViewById(R.id.tv_seen); 62 holder.likenum = (TextView)convertView.findViewById(R.id.tv_like); 63 holder.comnum = (TextView)convertView.findViewById(R.id.tv_com); 64 convertView.setTag(holder); 65 66 }else { 67 holder = (ViewHolder) convertView.getTag(); 68 } 69 70 //获得list里面的第paramInt个对象,并把值赋给每个控件 71 MovieBean movie = data.get(paramInt); 72 holder.name.setText(movie.getMovieName()); 73 holder.type.setText(CommonUtils.listToString(movie.getMovieType(),Constants.SPT_MOVIE_TYPE)); 74 holder.subtitle.setText(movie.getMovieIntrodutcion()); 75 holder.director.setText(movie.getMovieDirector()); 76 holder.seennum.setText(CommonUtils.basicToString(movie.getDoubanScore())); 77 holder.likenum.setText(CommonUtils.basicToString(movie.getMovieLikeNum())); 78 holder.comnum.setText(CommonUtils.basicToString(movie.getMovieCommentNum())); 79 80 return convertView; 81 82 } 83 84 85 86 87 public final class ViewHolder { 88 TextView name; 89 TextView type; 90 TextView subtitle; 91 TextView director; 92 TextView seennum; 93 TextView likenum; 94 TextView comnum; 95 } 96 97 98 99 100 }
使用:
1 package com.maomao.beautymovie.fragment; 2 3 public class AllMovieFragment extends Fragment{ 4 5 private static final String TAG = "movieFragment"; 6 private SwipeRefreshLayout swipeLayout; 7 private CustomListView movieListView; 8 private MovieListAdapter movieListAdapter; 9 private List<MovieBean> moviesLsit; 10 11 public AllMovieFragment() 12 { 13 } 14 15 public AllMovieFragment(List<MovieBean> moviesLsit) 16 { 17 this.moviesLsit = moviesLsit; 18 } 19 20 21 22 @Override 23 public void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 26 } 27 28 @SuppressLint("InlinedApi") 29 @Override 30 public View onCreateView(LayoutInflater inflater, ViewGroup container, 31 Bundle savedInstanceState) { 32 View view = inflater.inflate(R.layout.allmoviefrag, container, false); 33 movieListView = (CustomListView)view.findViewById(R.id.movielistview); 34 35 movieListAdapter = new MovieListAdapter(getActivity(), moviesLsit); 36 37 movieListView.setAdapter(movieListAdapter); 38 39 return view; 40 } 41 42 43 44 }