14.不同条目的listview

分类界面

整个项目的逻辑就是这样的

CategoryInfo 
  1. public class CategoryInfo {
  2. private String title;
  3. private String url1;
  4. private String url2;
  5. private String url3;
  6. private String name1;
  7. private String name2;
  8. private String name3;
  9. private boolean isTitle;// 是否是标题,指的是游戏,应用这些标题,下面的字段也是也是叫title,所以也这样写
  10. public boolean isTitle() {
  11. return isTitle;
  12. }
  13. public void setIsTitle(boolean isTitle) {
  14. this.isTitle = isTitle;
  15. }
CategoryProtocol
  1. public class CategoryProtocol extends BaseProtocol<List<CategoryInfo>> {
  2. @Override
  3. public List<CategoryInfo> paserJson(String json) {
  4. List<CategoryInfo> categoryInfos=new ArrayList<CategoryInfo>();
  5. try {
  6. JSONArray array=new JSONArray(json);
  7. for(int i=0;i<array.length();i++){
  8. JSONObject jsonObject = array.getJSONObject(i);
  9. String title=jsonObject.getString("title");
  10. CategoryInfo categoryInfo=new CategoryInfo();
  11. categoryInfo.setTitle(title);
  12. categoryInfo.setIsTitle(true);
  13. categoryInfos.add(categoryInfo);
  14. JSONArray jsonArray = jsonObject.getJSONArray("infos");
  15. for(int j=0;j<jsonArray.length();j++){
  16. JSONObject jsonObject2 = jsonArray.getJSONObject(j);
  17. String url1=jsonObject2.getString("url1");
  18. String url2=jsonObject2.getString("url2");
  19. String url3=jsonObject2.getString("url3");
  20. String name1=jsonObject2.getString("name1");
  21. String name2=jsonObject2.getString("name2");
  22. String name3=jsonObject2.getString("name3");
  23. CategoryInfo categoryInfo2=new CategoryInfo(title, url1, url2, url3, name1, name2, name3,false);
  24. categoryInfos.add(categoryInfo2);
  25. }
  26. }
  27. return categoryInfos;
  28. } catch (JSONException e) {
  29. e.printStackTrace();
  30. return null;
  31. }
  32. }
  33. @Override
  34. public String getKey() {
  35. return "category";
  36. }
  37. }
布局(三个这)
  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:clickable="false"
  5. android:orientation="horizontal"
  6. android:paddingLeft="5dp"
  7. android:paddingRight="5dp" >
  8. <RelativeLayout
  9. android:id="@+id/rl_1"
  10. android:layout_width="0dip"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="1"
  13. android:background="@drawable/grid_item_bg"
  14. android:clickable="true"
  15. android:paddingBottom="10dp"
  16. android:paddingTop="10dp" >
  17. <ImageView
  18. android:id="@+id/iv_1"
  19. android:layout_width="55dp"
  20. android:layout_height="55dp"
  21. android:layout_centerHorizontal="true"
  22. android:scaleType="fitXY"
  23. android:src="@drawable/ic_default" />
  24. <TextView
  25. android:id="@+id/tv_1"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_below="@id/iv_1"
  29. android:layout_centerHorizontal="true"
  30. android:textColor="#ff7a7a7a" />
  31. </RelativeLayout>
CategoryContentHolder 
  1. public class CategoryContentHolder extends BaseHolder<CategoryInfo> {
  2. ImageView [] ivs;
  3. TextView [] tvs;
  4. @Override
  5. public View initView() {
  6. View view=UiUtils.inflate(R.layout.item_category_content);
  7. ivs=new ImageView[3];
  8. ivs[0]=(ImageView) view.findViewById(R.id.iv_1);
  9. ivs[1]=(ImageView) view.findViewById(R.id.iv_2);
  10. ivs[2]=(ImageView) view.findViewById(R.id.iv_3);
  11. tvs=new TextView[3];
  12. tvs[0]=(TextView) view.findViewById(R.id.tv_1);
  13. tvs[1]=(TextView) view.findViewById(R.id.tv_2);
  14. tvs[2]=(TextView) view.findViewById(R.id.tv_3);
  15. return view;
  16. }
  17. @Override
  18. public void refreshView(CategoryInfo data) {
  19. // 第一块
  20. if(!TextUtils.isEmpty(data.getName1())&&!TextUtils.isEmpty(data.getUrl1())){
  21. tvs[0].setText(data.getName1());
  22. bitmapUtils.display(ivs[0], HttpHelper.URL+"image?name="+data.getUrl1());
  23. tvs[0].setVisibility(View.VISIBLE);
  24. ivs[0].setVisibility(View.VISIBLE);
  25. }else{
  26. tvs[0].setVisibility(View.INVISIBLE);
  27. ivs[0].setVisibility(View.INVISIBLE);
  28. }
  29. // 第二块
  30. if(!TextUtils.isEmpty(data.getName2())&&!TextUtils.isEmpty(data.getUrl2())){
  31. tvs[1].setText(data.getName2());
  32. bitmapUtils.display(ivs[1], HttpHelper.URL+"image?name="+data.getUrl2());
  33. tvs[1].setVisibility(View.VISIBLE);
  34. ivs[1].setVisibility(View.VISIBLE);
  35. }else{
  36. tvs[1].setVisibility(View.INVISIBLE);
  37. ivs[1].setVisibility(View.INVISIBLE);
  38. }
  39. //第三块
  40. if(!TextUtils.isEmpty(data.getName3())&&!TextUtils.isEmpty(data.getUrl3())){
  41. tvs[2].setText(data.getName3());
  42. bitmapUtils.display(ivs[2], HttpHelper.URL+"image?name="+data.getUrl3());
  43. tvs[2].setVisibility(View.VISIBLE);
  44. ivs[2].setVisibility(View.VISIBLE);
  45. }else{
  46. tvs[2].setVisibility(View.INVISIBLE);
  47. ivs[2].setVisibility(View.INVISIBLE);
  48. }
  49. }
  50. }
CategoryTitleHolder 
  1. public class CategoryTitleHolder extends BaseHolder<CategoryInfo> {
  2. private TextView tv;
  3. @Override
  4. public View initView() {
  5. tv = new TextView(UiUtils.getContext());
  6. tv.setTextColor(Color.BLACK);
  7. tv.setBackgroundDrawable(UiUtils.getDrawalbe(R.drawable.grid_item_bg));
  8. return tv;
  9. }
  10. @Override
  11. public void refreshView(CategoryInfo data) {
  12. tv.setText(data.getTitle());
  13. }
  14. }
CategoryFragment ( 注意CategoryAdapter
  1. public class CategoryFragment extends BaseFragment {
  2. private List<CategoryInfo> datas;
  3. public static int ITEM_TITLE =2;
  4. // 创建成功的界面
  5. @Override
  6. public View createSuccessView() {
  7. BaseListView listView = new BaseListView(UiUtils.getContext());
  8. listView.setAdapter(new CategoryAdapter(datas, listView));
  9. return listView;
  10. }
  11. private class CategoryAdapter extends DefaultAdapter<CategoryInfo> {
  12. private int position;// 当前条目位置记录
  13. public CategoryAdapter(List<CategoryInfo> datas, ListView lv) {
  14. super(datas, lv);
  15. }
  16. // 实现每个条目的界面
  17. @Override
  18. protected BaseHolder<CategoryInfo> getHolder() {
  19. if (!datas.get(position).isTitle()) {
  20. return new CategoryContentHolder();
  21. }else{
  22. return new CategoryTitleHolder();
  23. }
  24. }
  25. @Override
  26. public View getView(int position, View convertView, ViewGroup parent) {
  27. this.position = position;
  28. return super.getView(position, convertView, parent);
  29. }
  30. @Override
  31. protected boolean hasMore() { // 当前方法 如果为false onload就不会被调用了
  32. return false;
  33. }
  34. @Override
  35. protected int getInnerItemViewType(int position) {
  36. if (datas.get(position).isTitle()) {
  37. return ITEM_TITLE;
  38. } else {
  39. return super.getInnerItemViewType(position);
  40. }
  41. }
  42. @Override
  43. protected List<CategoryInfo> onload() {
  44. return null;
  45. }
  46. // 集合 管理三个convertView
  47. @Override
  48. public int getViewTypeCount() {
  49. return super.getViewTypeCount() + 1; // 又额外多了一种条目类型 现在又三种 1 标题 2 内容 3 加载更多(没有显示)
  50. }
  51. }
  52. // 请求服务器
  53. @Override
  54. protected LoadResult load() {
  55. CategoryProtocol protocol = new CategoryProtocol();
  56. datas = protocol.load(0);
  57. return checkData(datas);
  58. }
  59. }
修改DefaultAdapter
  1. public View getView(int position, View convertView, ViewGroup parent) {
  2. BaseHolder holder = null;
  3. switch (getItemViewType(position)) { // 判断当前条目时什么类型
  4. case MORE_ITEM:
  5. if(convertView==null){
  6. holder=getMoreHolder();
  7. }else{
  8. holder=(BaseHolder) convertView.getTag();
  9. }
  10. break;
  11. default:
  12. if (convertView == null) {
  13. holder = getHolder();
  14. } else {
  15. System.out.println("aaa");
  16. holder = (BaseHolder) convertView.getTag();
  17. }
  18. if (position < datas.size()) {
  19. holder.setData(datas.get(position));
  20. }
  21. break;
  22. }
  23. return holder.getContentView(); // 如果当前Holder 恰好是MoreHolder 证明MoreHOlder已经显示
  24. }
  25. private MoreHolder holder;
  26. private BaseHolder getMoreHolder() {
  27. if(holder!=null){
  28. return holder;
  29. }else{
  30. holder=new MoreHolder(this,hasMore());
  31. return holder;
  32. }
  33. }
  34. /**
  35. * 是否有额外的数据
  36. * @return
  37. */
  38. protected boolean hasMore() {
  39. return true;
  40. }

 
 
 
 
 




posted @ 2015-11-20 19:42  梦和远方  阅读(192)  评论(0编辑  收藏  举报