1. 程式人生 > >多條目 兩個佈局的BaseAdapter

多條目 兩個佈局的BaseAdapter

public class NewsAdapter extends BaseAdapter {

  private List<NewsResponseBean.NewsItem> mDatas;
  private Context context;
  // 0 - count-1
  private final int TT_ITEM = 0;
  private final int GN_ITEM = 1;
  private final int ITEM_COUNT = 2;

  //展示 兩種型別條目:文字    文字+圖片

  public NewsAdapter(Context context) {
    this.context = context;
    mDatas = new ArrayList<>();
  }

  public void setDatas(List<NewsResponseBean.NewsItem> datas) {
    this.mDatas = datas;
    notifyDataSetChanged();
  }

  @Override public int getViewTypeCount() {
    return ITEM_COUNT;
  }

  @Override public int getItemViewType(int position) {
    return getItem(position).isTT() ? TT_ITEM : GN_ITEM;
  }

  @Override public int getCount() {
    return mDatas.size();
  }

  @Override public NewsResponseBean.NewsItem getItem(int position) {
    return mDatas.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

//單一職責

//1 建立item

if (convertView == null) {
  convertView = LayoutInflater.from(context).inflate(
      getItemViewType(position) == GN_ITEM ? R.layout.item_gn : R.layout.item_tt
      , parent, false);
  viewHolder = new ViewHolder(convertView);
} else {
  viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.bind(getItem(position));

return convertView;
 }

 class ViewHolder {
    //如果是國內新聞,icon不為空;否則icon為空
    ImageView icon;

TextView title;
TextView date;

public ViewHolder(View convertView) {
  title = convertView.findViewById(R.id.title);
  date = convertView.findViewById(R.id.date);
  //可能為空
  icon = convertView.findViewById(R.id.icon);
  convertView.setTag(this);
}

public void bind(NewsResponseBean.NewsItem newsItem) {
  title.setText(newsItem.getTitle());
  date.setText(newsItem.getDate());
  if(icon != null) {
    //載入圖片
    icon.setImageResource(R.mipmap.ic_launcher);
  }
 /* if (getItemViewType(position) == GN_ITEM) {
    //載入圖片
  }*/
}
}

國內的子佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    >

  <ImageView
      android:id="@+id/icon"
      android:layout_width="160dp"
      android:layout_height="100dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      />

  <TextView
      android:id="@+id/title"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:ellipsize="end"
      android:maxLines="2"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toLeftOf="@id/icon"
      app:layout_constraintTop_toTopOf="@id/icon"
      />

  <TextView
      android:id="@+id/date"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:ellipsize="end"
      android:maxLines="1"
      android:text="日期"
      app:layout_constraintBottom_toBottomOf="@id/icon"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toLeftOf="@id/icon"
      />

</android.support.constraint.ConstraintLayout>

頭條的子佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    >

  <TextView
      android:id="@+id/title"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:maxLines="2"
      android:text="標題"
      app:layout_constraintBottom_toTopOf="@id/date"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      />

  <TextView
      android:id="@+id/date"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginTop="16dp"
      android:maxLines="1"
      android:text="日期"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toBottomOf="@id/title"
      />

</android.support.constraint.ConstraintLayout>