当前位置:   article > 正文

ListView的使用_类型“{ id: string; imgurl: string; name: string; pri

类型“{ id: string; imgurl: string; name: string; price: string; } & { id: s

单独的ListView使用见Listview的DEMO


通过在Activity中Fragment中引用ListView与直接在Activity中引用ListView有所不同

ListView的使用 + BaseAdapter
1)在layout文件中声明控件
2)定义一个layout文件描述ListView的item的外观控件
3)java代码找出ListView控件
4)定义ListView的数据源并添加数据
List<自定义类>
5)适配器:定义类 extends BaseAdapter
6)java代码中初始化适配器
7)ListView调用setAdapter()

实现的效果图:通过点击“男装”,“女装”等标签分别显示listview加载出来的内容,或者通过向左右活动屏幕切换到新的页面,相应的标签栏也进行了切换


首相进行Layout的创建 product_fragment:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="@dimen/activity_horizontal_margin" >
  6. <HorizontalScrollView
  7. android:id="@+id/product_hscroll"
  8. android:layout_width="match_parent"
  9. android:layout_height="@dimen/line_width_40"
  10. android:scrollbars="none" >
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="40dp"
  14. android:background="#fff"
  15. android:orientation="horizontal" >
  16. <TextView
  17. android:id="@+id/product_boy"
  18. android:layout_width="100dp"
  19. android:layout_height="match_parent"
  20. android:layout_marginLeft="1dp"
  21. android:background="#ccc"
  22. android:gravity="center"
  23. android:text="男装"
  24. android:textColor="#f00"
  25. android:textSize="16sp" />
  26. <TextView
  27. android:id="@+id/product_nv"
  28. android:layout_width="100dp"
  29. android:layout_height="match_parent"
  30. android:layout_marginLeft="1dp"
  31. android:background="#ccc"
  32. android:gravity="center"
  33. android:text="女装"
  34. android:textColor="#f00"
  35. android:textSize="16sp" />
  36. <TextView
  37. android:id="@+id/product_tong"
  38. android:layout_width="100dp"
  39. android:layout_height="match_parent"
  40. android:layout_marginLeft="1dp"
  41. android:background="#ccc"
  42. android:gravity="center"
  43. android:text="童装"
  44. android:textColor="#f00"
  45. android:textSize="16sp" />
  46. <TextView
  47. android:id="@+id/product_shipin"
  48. android:layout_width="100dp"
  49. android:layout_height="match_parent"
  50. android:layout_marginLeft="1dp"
  51. android:background="#ccc"
  52. android:gravity="center"
  53. android:text="饰品"
  54. android:textColor="#f00"
  55. android:textSize="16sp" />
  56. <TextView
  57. android:id="@+id/product_bag"
  58. android:layout_width="100dp"
  59. android:layout_height="match_parent"
  60. android:layout_marginLeft="1dp"
  61. android:background="#ccc"
  62. android:gravity="center"
  63. android:text="包包"
  64. android:textColor="#f00"
  65. android:textSize="16sp" />
  66. </LinearLayout>
  67. </HorizontalScrollView>
  68. <android.support.v4.view.ViewPager
  69. android:id="@+id/product_viewpager"
  70. android:layout_width="match_parent"
  71. android:layout_height="wrap_content"
  72. android:layout_marginTop="2dp" />
  73. </LinearLayout>


创建一个ProductFragment用于获取上面的布局:

  1. public class ProductFragment extends Fragment implements OnClickListener {
  2. private View rootView;
  3. // ViewPager相关
  4. private ViewPager viewPager;
  5. private List<Fragment> viewPagerData;
  6. private FragmentPagerAdapter viewPagerAdapter;
  7. private TextView[] tvCategorys;
  8. private HorizontalScrollView hScrollView;
  9. @Override
  10. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  11. Bundle savedInstanceState) {
  12. if (rootView == null) {
  13. rootView = inflater.inflate(R.layout.fragment_product, container,
  14. false);
  15. initView(rootView);
  16. }
  17. ViewGroup par = (ViewGroup) rootView.getParent();
  18. if (par != null) {
  19. par.removeView(rootView);
  20. }
  21. return rootView;
  22. }
  23. private void initView(View rootView) {
  24. // ViewPager相关
  25. viewPager = (ViewPager) rootView.findViewById(R.id.product_viewpager);
  26. // 数据源
  27. viewPagerData = new ArrayList<Fragment>();
  28. viewPagerData.add(ProductCategoryFragment.newInstance("男装"));
  29. viewPagerData.add(ProductCategoryFragment.newInstance("女装"));
  30. viewPagerData.add(ProductCategoryFragment.newInstance("童装"));
  31. viewPagerData.add(ProductCategoryFragment.newInstance("饰品"));
  32. viewPagerData.add(ProductCategoryFragment.newInstance("包包"));
  33. // 适配器
  34. viewPagerAdapter = new ViewPagerProductAdapter(
  35. getChildFragmentManager(), viewPagerData);
  36. // 控件绑定适配器
  37. viewPager.setAdapter(viewPagerAdapter);
  38. // 滑动页面时让对应的标签跟着一起改变,滑动到第几个页面arg0的值随着改变,数组tvCategorys[arg0]对应的标签也随着改变,但tvCategorys在后面部分也没有影响
  39. viewPager.setOnPageChangeListener(new OnPageChangeListener() {
  40. @Override
  41. public void onPageSelected(int arg0) {
  42. for (int i = 0; i < tvCategorys.length; i++) {
  43. tvCategorys[i].setTextColor(Color.BLACK);
  44. tvCategorys[i].setBackgroundColor(Color.rgb(0xDD, 0xDD,
  45. 0xDD));
  46. }
  47. tvCategorys[arg0].setBackgroundColor(Color.BLUE);
  48. tvCategorys[arg0].setTextColor(Color.WHITE);
  49. }
  50. @Override
  51. public void onPageScrolled(int arg0, float arg1, int arg2) {
  52. }
  53. @Override
  54. public void onPageScrollStateChanged(int arg0) {
  55. }
  56. });
  57. // //
  58. tvCategorys = new TextView[5];
  59. tvCategorys[0] = (TextView) rootView.findViewById(R.id.product_boy);
  60. tvCategorys[1] = (TextView) rootView.findViewById(R.id.product_nv);
  61. tvCategorys[2] = (TextView) rootView.findViewById(R.id.product_tong);
  62. tvCategorys[3] = (TextView) rootView.findViewById(R.id.product_shipin);
  63. tvCategorys[4] = (TextView) rootView.findViewById(R.id.product_bag);
  64. for (int i = 0; i < tvCategorys.length; i++) {
  65. tvCategorys[i].setOnClickListener(this);
  66. }
  67. hScrollView = (HorizontalScrollView) rootView
  68. .findViewById(R.id.product_hscroll);
  69. }
  70. // 通过点击控件进行对应页面的切换
  71. @Override
  72. public void onClick(View v) {
  73. for (int i = 0; i < tvCategorys.length; i++) {
  74. tvCategorys[i].setTextColor(Color.BLACK);
  75. tvCategorys[i].setBackgroundColor(Color.rgb(0xDD, 0xDD, 0xDD));
  76. }
  77. // 判断具体点击了哪一个TextView
  78. for (int i = 0; i < tvCategorys.length; i++) {
  79. if (v == tvCategorys[i]) {
  80. tvCategorys[i].setBackgroundColor(Color.BLUE);
  81. tvCategorys[i].setTextColor(Color.WHITE);
  82. viewPager.setCurrentItem(i);
  83. break;
  84. }
  85. }
  86. }
  87. }



创建一个productcategory_listview,目的是为了把紧接着下面的这个listview_item_product这个控件放入这个布局中:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <ListView
  6. android:id="@+id/productcategory_listview"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:cacheColorHint="#0000"
  10. ></ListView>
  11. </LinearLayout>


创建一个listview_item_product:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="100dp"
  4. android:orientation="horizontal" >
  5. <ImageView
  6. android:id="@+id/listview_item_product_img"
  7. android:layout_width="100dp"
  8. android:layout_height="100dp"
  9. />
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:layout_marginLeft="4dp"
  14. android:orientation="vertical"
  15. >
  16. <TextView
  17. android:id="@+id/listview_item_product_tv_name"
  18. android:layout_width="match_parent"
  19. android:layout_height="30dp"
  20. android:textSize="16sp"
  21. android:textStyle="bold"
  22. />
  23. <TextView
  24. android:id="@+id/listview_item_product_tv_desc"
  25. android:layout_width="match_parent"
  26. android:layout_height="match_parent"
  27. android:ellipsize="end"
  28. android:lineSpacingExtra="4dp"
  29. />
  30. </LinearLayout>
  31. </LinearLayout>

通过java代码创建一个适配器,加载入listview_item_product中所有控件,但在这之前还需要创建一个product命名的标准类,为了方便阅读,接着创建在一下这个类后面:

  1. public class ListViewProductAdapter extends BaseAdapter{
  2. // 需要两个成员变量
  3. private Context context ;
  4. private List<Product> data ;
  5. public ListViewProductAdapter(Context context, List<Product> data) {
  6. super();
  7. this.context = context;
  8. this.data = data;
  9. }
  10. @Override
  11. public int getCount() {
  12. return data.size() ;
  13. }
  14. @Override
  15. public Object getItem(int position) {
  16. return data.get(position) ;
  17. }
  18. @Override
  19. public long getItemId(int position) {
  20. return position ;
  21. }
  22. @Override
  23. public View getView(int position, View convertView, ViewGroup parent) {
  24. ViewHolder holder = null ;
  25. if(null == convertView) {
  26. convertView = LayoutInflater.from(context).inflate(R.layout.listview_product_item, null) ;
  27. holder = new ViewHolder() ;
  28. holder.iv = (ImageView) convertView.findViewById(R.id.listview_item_product_img) ;
  29. holder.tvTitle = (TextView) convertView.findViewById(R.id.listview_item_product_tv_name) ;
  30. holder.tvDesc = (TextView) convertView.findViewById(R.id.listview_item_product_tv_desc) ;
  31. convertView.setTag(holder) ;
  32. }
  33. else {
  34. holder = (ViewHolder) convertView.getTag() ;
  35. }
  36. // 为控件绑定数据项
  37. Product current = data.get(position) ;
  38. holder.iv.setImageResource(current.getImg()) ;
  39. holder.tvDesc.setText(current.getDesc()) ;
  40. holder.tvTitle.setText(current.getName()) ;
  41. return convertView ;
  42. }
  43. static class ViewHolder{
  44. // 声明你的item布局layout文件中所需要使用的控件
  45. ImageView iv;
  46. TextView tvTitle , tvDesc ;
  47. }
  48. }
标准的product类:

  1. public class Product {
  2. private long id ;
  3. private int img;
  4. private String name , desc ;
  5. private double price ;
  6. private int count ;
  7. private String imgUrl ;
  8. public Product(long id, int img, String name, String desc, double price,
  9. int count) {
  10. super();
  11. this.id = id;
  12. this.img = img;
  13. this.name = name;
  14. this.desc = desc;
  15. this.price = price;
  16. this.count = count;
  17. }
  18. public Product(long id, String imgUrl, String name, String desc, double price,
  19. int count) {
  20. super();
  21. this.id = id;
  22. this.imgUrl = imgUrl;
  23. this.name = name;
  24. this.desc = desc;
  25. this.price = price;
  26. this.count = count;
  27. }
  28. public long getId() {
  29. return id;
  30. }
  31. public int getImg() {
  32. return img;
  33. }
  34. public String getName() {
  35. return name;
  36. }
  37. public String getDesc() {
  38. return desc;
  39. }
  40. public double getPrice() {
  41. return price;
  42. }
  43. public int getCount() {
  44. return count;
  45. }
  46. public String getImgUrl() {
  47. return imgUrl;
  48. }
  49. @Override
  50. public String toString() {
  51. return "Product [id=" + id + ", img=" + img + ", name=" + name
  52. + ", desc=" + desc + ", price=" + price + ", count=" + count
  53. + ", imgUrl=" + imgUrl + "]";
  54. }
  55. }
创建一个ProductCategoryFragment,把适配器加载到listview
  1. public class ProductCategoryFragment extends Fragment {
  2. private View rootView;
  3. private String category;
  4. private ListView listview;
  5. private List<Product> data;
  6. private BaseAdapter adapter;
  7. // 当前Fragment初始化时需要传入字符串类型的商品类别
  8. public static ProductCategoryFragment newInstance(String categoryName) {
  9. // 把参数放入Bundle
  10. Bundle bundle = new Bundle();
  11. bundle.putString("category", categoryName);
  12. // 初始化当前Fragment对象
  13. ProductCategoryFragment f = new ProductCategoryFragment();
  14. // 将Bundle放入当前Fragment对象的arguments里面
  15. f.setArguments(bundle);
  16. // 返回定义的当前Fragment对象
  17. return f;
  18. }
  19. // 获取Fragment初始化时传入的参数
  20. public String getCategory() {
  21. // 从Arguments中取出Bundle
  22. Bundle bundle = getArguments();
  23. // 从Bundle中取出数据
  24. String result = null;
  25. if (null != bundle) {
  26. result = bundle.getString("category");
  27. }
  28. return result;
  29. }
  30. @Override
  31. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  32. Bundle savedInstanceState) {
  33. if (null == rootView) {
  34. rootView = inflater.inflate(R.layout.fragment_productcategory,
  35. container, false);
  36. initView(rootView);
  37. }
  38. ViewGroup parent = (ViewGroup) rootView.getParent();
  39. if (null != parent) {
  40. parent.removeView(rootView);
  41. }
  42. return rootView;
  43. }
  44. private void initView(View rootView) {
  45. category = getCategory();
  46. if (null == category) {
  47. return;
  48. }
  49. listview = (ListView) rootView
  50. .findViewById(R.id.productcategory_listview);
  51. data = new ArrayList<Product>();
  52. if ("男装".equals(category)) {
  53. data.add(new Product(1, R.drawable.nan_1, "dfsd", "dfdsgds", 128, 4));
  54. data.add(new Product(2, R.drawable.nan_2, "jeep", "dssdf", 228, 4));
  55. data.add(new Product(3, R.drawable.nan_3, "dfssdf", "dfdsgsdg",
  56. 128, 4));
  57. } else if ("女装".equals(category)) {
  58. data.add(new Product(1, R.drawable.nv_1, "only", "only ", 128, 4));
  59. data.add(new Product(2, R.drawable.nv_2, "Very moda", "fdsfs", 128,
  60. 4));
  61. } else if ("童装".equals(category)) {
  62. data.add(new Product(1, R.drawable.tong_1, "balala", "dfdsfs", 128,
  63. 4));
  64. data.add(new Product(2, R.drawable.tong_2, "tom", "fgsf", 128, 4));
  65. data.add(new Product(2, R.drawable.tong_3, "cat", "dfsdf", 128, 4));
  66. } else if ("饰品".equals(category)) {
  67. data.add(new Product(2, R.drawable.shi_1, "Chanel", "dsfdsfds",
  68. 128, 4));
  69. } else if ("包包".equals(category)) {
  70. data.add(new Product(2, R.drawable.bao_1, "MK", "MK", 128, 4));
  71. data.add(new Product(2, R.drawable.bao_2, "baburry", "dfdsf", 128,
  72. 4));
  73. data.add(new Product(2, R.drawable.bao_3, "NineWest", "dfdsfsd",
  74. 128, 4));
  75. }
  76. // 适配器
  77. adapter = new ListViewProductAdapter(getActivity(), data);
  78. listview.setAdapter(adapter);
  79. }
  80. }





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/787412
推荐阅读
相关标签
  

闽ICP备14008679号