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