Android ListView添加头布局和脚布局实例详解_Android

Android ListView添加头布局和脚布局

之前学习喜马拉雅的时候做的一个小Demo,贴出来,供大家学习参考;

如果我们当前的页面有多个接口、多种布局的话,我们一般的选择无非就是1、多布局;2、各种复杂滑动布局外面套一层ScrollView(好low);3、头布局脚布局。有的时候我们用多布局并不能很好的实现,所以头布局跟脚布局就是我们最好的选择了;学过了ListView的话原理很简单,没啥理解的东西,直接贴代码了:

效果图:

                

正文部分布局:

fragment_classify.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ListView
    android:id="@+id/teach_classify_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:listSelector="#00000000"/>
</LinearLayout>

classify_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <View
    android:id="@+id/teach_classify_item_divider"
    android:background="#f3fdeeee"
    android:layout_width="match_parent"
    android:layout_height="10dp"/>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <RelativeLayout
      android:id="@+id/teach_classify_left"
      android:layout_width="0dp"
      android:background="@drawable/item_pressed"
      android:layout_height="match_parent"
      android:layout_marginLeft="15dp"
      android:layout_weight="1">

      <ImageView
        android:id="@+id/teach_classify_item_iamge01"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher" />

      <TextView
        android:id="@+id/teach_classify_item_text01"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="60dp"
        android:gravity="center_vertical"
        android:text="@string/app_name" />

    </RelativeLayout>

    <View
      android:layout_width="1dp"
      android:layout_height="match_parent"
      android:background="#efe6e6" />

    <RelativeLayout
      android:id="@+id/teach_classify_right"
      android:layout_width="0dp"
      android:background="@drawable/item_pressed"
      android:layout_height="match_parent"
      android:layout_marginLeft="15dp"
      android:layout_weight="1">

      <ImageView
        android:id="@+id/teach_classify_item_iamge02"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher" />

      <TextView
        android:id="@+id/teach_classify_item_text02"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="60dp"
        android:gravity="center_vertical"
        android:text="@string/app_name" />

    </RelativeLayout>
  </LinearLayout>

</LinearLayout>

头布局:

fragment_classify_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView
    android:id="@+id/teach_classify_lv_header"
    android:src="@mipmap/ic_launcher"
    android:layout_width="match_parent"
    android:layout_height="180dp" />
</LinearLayout>

脚布局:

fragment_classify_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"    android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView
    android:id="@+id/teach_classify_bottom"
    android:src="@mipmap/ic_launcher"
    android:layout_width="match_parent"
    android:layout_height="160dp" />
</LinearLayout>

主页面:

public class ClassifyFragment extends BaseFragment implements ClassifyAdapter.OnClickItemListener{
  public static final String TAG = ClassifyFragment.class.getSimpleName();
  private ListView mListView;
  private ClassifyAdapter adapter;
  private ImageView mHeaderImage;
  private ImageView mBottomImage;

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    layout = inflater.inflate(R.layout.fragment_classify, container, false);
    return layout;
  }

  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    initView();
    setupView();
  }
  /**
   * Header的添加最好放在setAdapter之前,在Android4.4之前,Header添加必须放在设置Adapter之前
   */
  private void initView() {
    mListView = ((ListView) layout.findViewById(R.id.teach_classify_listview));
    //header
    View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_classify_header,null);
    mHeaderImage = ((ImageView) headerView.findViewById(R.id.teach_classify_lv_header));
    //可以添加多个HeaderView
    mListView.addHeaderView(headerView);
    //bottom
    View bottomView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_classify_bottom,null);
    mBottomImage = ((ImageView) bottomView.findViewById(R.id.teach_classify_bottom));
    mListView.addFooterView(bottomView);
    adapter = new ClassifyAdapter(getActivity(), null);
    mListView.setAdapter(adapter);
  }

  /**
   * 网络请求
   */
  private void setupView() {
    HttpUtil.getStringAsync(HttpConstant.CLASSIFY_URL, new HttpUtil.RequestCallBack() {
      @Override
      public void onFailure() {
        Log.e(TAG, "onFailure: ");
      }

      @Override
      public void onSuccess(String result) {
        Log.e(TAG, "onSuccess: " + result);
        Gson gson = new Gson();
        ClassifyList classifyList = gson.fromJson(result, ClassifyList.class);
        List<Classify> list = classifyList.getList();
        //更新适配器
        adapter.updateRes(list);
        //更新Header
        ImageLoader.display(mHeaderImage,list.get(0).getCoverPath());
      }

      @Override
      public void onFinish() {
        Log.e(TAG, "onFinish: ");
      }
    });
    String URL_BOTTOM="http://adse.ximalaya.com/ting?device=android&name=cata_index_banner&network=wifi&operator=0&version=4.3.98";
    HttpUtil.getStringAsync(URL_BOTTOM, new HttpUtil.RequestCallBack() {
      @Override
      public void onFailure() {
      }

      @Override
      public void onSuccess(String result) {
        Gson gson = new Gson();
        ClassifyBottomList classifyBottomList = gson.fromJson(result, ClassifyBottomList.class);
        ImageLoader.display(mBottomImage, classifyBottomList.getData().get(0).getCover());
      }

      @Override
      public void onFinish() {
      }
    });
  }
  @Override
  public void onOnclickItem(int position) {
    Log.e(TAG, "onOnclickItem:------------- "+position );
  }
}

适配器:

public class ClassifyAdapter extends BaseAdapter implements View.OnClickListener {

  private static final String TAG = ClassifyAdapter.class.getSimpleName();
  private List<Classify> data;
  private LayoutInflater inflater;
  private OnClickItemListener listener;//持有接口

  public void setListener(OnClickItemListener listener){
      this.listener=listener;
  }

  public ClassifyAdapter(Context context,List<Classify>data) {
    inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (data!=null) {
      this.data=data;
    }
    else {
      this.data=new ArrayList<>();
    }
  }

  public void updateRes(List<Classify> data){
    if (data!=null) {
      this.data.clear();
      this.data.addAll(data);
      notifyDataSetChanged();
    }
  }

  @Override
  public int getCount() {
    int count=0;
    if (data!=null) {
      count=(data.size()-1)/2;
    }
    return count;
  }

  @Override
  public Classify getItem(int position) {
    return data.get(position);
  }

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

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder=null;
    if (convertView==null) {
      convertView=inflater.inflate(R.layout.classify_item,parent,false);
      holder=new ViewHolder();
      holder.itemIamge01= (ImageView) convertView.findViewById(R.id.teach_classify_item_iamge01);
      holder.itemImage02= (ImageView) convertView.findViewById(R.id.teach_classify_item_iamge02);
      holder.itemText01= (TextView) convertView.findViewById(R.id.teach_classify_item_text01);
      holder.itemText02= (TextView) convertView.findViewById(R.id.teach_classify_item_text02);
      holder.topDivider=convertView.findViewById(R.id.teach_classify_item_divider);
      holder.leftItem=convertView.findViewById(R.id.teach_classify_left);
      holder.rightItem=convertView.findViewById(R.id.teach_classify_right);
      convertView.setTag(holder);
    }
    else {
      holder= (ViewHolder) convertView.getTag();
    }
    //根据条件判断是否显示分割线
    if (position%3==0&&position!=0) {
      holder.topDivider.setVisibility(View.VISIBLE);
    }else {
      holder.topDivider.setVisibility(View.GONE);
    }
    //加载数据
    holder.itemText01.setText(data.get(position*2+1).getTitle());
    holder.itemText02.setText(data.get(position*2+2).getTitle());
    //设置监听
    holder.leftItem.setOnClickListener(this);
    holder.rightItem.setOnClickListener(this);
    //设置标记
    holder.leftItem.setTag(position*2+1);
    holder.rightItem.setTag(position*2+2);
    //加载图片
    ImageLoader.display(holder.itemIamge01,data.get(position*2+1).getCoverPath());
    ImageLoader.display(holder.itemImage02,data.get(position*2+2).getCoverPath());
    return convertView;
  }

  @Override
  public void onClick(View v) {
    Integer position = (Integer) v.getTag();
    Log.e(TAG, "onClick: "+position );
    if (listener!=null) {
      listener.onOnclickItem(position);
    }

  }

  private static class ViewHolder{
    //左边的图片
    ImageView itemIamge01;
    //右边的图片
    ImageView itemImage02;
    //右边
    TextView itemText01;
    TextView itemText02;

    //分割线
    View topDivider;
    //左右布局
    View leftItem,rightItem;
  }

  public interface OnClickItemListener{
    void onOnclickItem(int position);
  }
}

model类:

public class Classify {

  private String title;
  private String coverPath;

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getCoverPath() {
    return coverPath;
  }

  public void setCoverPath(String coverPath) {
    this.coverPath = coverPath;
  }
}
public class ClassifyList {
  private List<Classify> list;

  public List<Classify> getList() {
    return list;
  }

  public void setList(List<Classify> list) {
    this.list = list;
  }
}
public class ClassifyBottom {
  private String cover;

  public String getCover() {
    return cover;
  }

  public void setCover(String cover) {
    this.cover = cover;
  }
}
public class ClassifyBottomList {
  private List<ClassifyBottom> data;

  public List<ClassifyBottom> getData() {
    return data;
  }

  public void setData(List<ClassifyBottom> data) {
    this.data = data;
  }
}

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
listview不同item布局、listview多布局、android listview布局、android listview详解、expandlistview详解,以便于您获取更多的相关知识。

时间: 2024-08-04 01:38:01

Android ListView添加头布局和脚布局实例详解_Android的相关文章

Android 实现夜间模式的快速简单方法实例详解_Android

ChangeMode 项目地址:ChangeMode Implementation of night mode for Android. 用最简单的方式实现夜间模式,支持ListView.RecyclerView. Preview Usage xml android:background="?attr/zzbackground" app:backgroundAttr="zzbackground"//如果当前页面要立即刷新,这里传入属性名称 比如 R.attr.zzb

Android编程之软键盘的隐藏显示实例详解_Android

本文实例分析了Android编程之软键盘的隐藏显示方法.分享给大家供大家参考,具体如下: Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入. 那么,弹出软键盘后必然会造成原有布局高度的减少,那么系统应该如何来处理布局的减少?我们能否在应用程序中进行自定义的控制?这些是本文要讨论的重点. 一.软键盘显示的原理 软件盘的本质是什么?软键盘其实是一个Dialog! InputMethodService为我们的输入法创建了一个Dialog,并且将

Android Webview添加网页加载进度条实例详解

推荐阅读:Android WebView线性进度条实例详解 最近在android项目中使用webview嵌套了一个抽奖活动网页,活动上线,运行良好(改了N次需求和突发bug),还好这种模式的活动,只需要修改网页,不需要重新打包发布市场,这也是这种模式开发的优势之一.后来据产品哥反馈说加载网页无进度提示,好吧,这个当时真没考虑这么多,这个要加加..想当然以为轻松搞定之....其实还是比轻松要复杂点... 1.首先自定义一个WebView控件 /** * 带进度条的Webivew * @author

Android 跨进程模拟按键(KeyEvent )实例详解_Android

  Android 解决不同进程发送KeyEvent 的问题 最近在做有关于Remote Controller 的功能,该功能把手机做成TV的遥控器来处理.在手机的客户端发送消息到TV的android 服务端,服务端接收到客户端的请求消息,模拟KeyEvent命令,发送Key值.  最简单的发送命令为如下代码: public static void simulateKeystroke(final int KeyCode) { new Thread(new Runnable() { public

Android编程中activity的完整生命周期实例详解_Android

本文实例分析了Android编程中activity的完整生命周期.分享给大家供大家参考,具体如下: android中 activity有自己的生命周期,对这些知识的学习可以帮助我们在今后写程序的时候,更好的理解其中遇到的一些错误.这篇文章很长,希望不要耽误大家的时间- 今天不会涉及太多关于activity栈的东西,主要说activity自身的生命周期 区分几个概念 1 Activity 官方解释为 "An Activity is an application component that pro

Android动画之补间动画(Tween Animation)实例详解_Android

本文实例讲述了Android动画之补间动画.分享给大家供大家参考,具体如下: 前面讲了<Android动画之逐帧动画(Frame Animation)>,今天就来详细讲解一下Tween动画的使用. 同样,在开始实例演示之前,先引用官方文档中的一段话: Tween动画是操作某个控件让其展现出旋转.渐变.移动.缩放的这么一种转换过程,我们称为补间动画.我们可以以XML形式定义动画,也可以编码实现. 如果以XML形式定义一个动画,我们按照动画的定义语法完成XML,并放置于/res/anim目录下,文

Android动画之逐帧动画(Frame Animation)实例详解_Android

本文实例分析了Android动画之逐帧动画.分享给大家供大家参考,具体如下: 在开始实例讲解之前,先引用官方文档中的一段话: Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画.Frame动画可以被定义在XML文件中,也可以完全编码实现. 如果被定义在XML文件中,我们可以放置在/res下的anim或drawable目录中(/res/[anim | drawable]/filename.xml),文件名可以作为资源ID在代码中引用:如果由完全由编码实现,我

Android TextView(圆弧)边框和背景实例详解_Android

 Android TextView 圆弧 效果图: 布局代码: <TextView android:id="@+id/product_tag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:singleLine="true" androi

Android ListView添加头布局和脚布局实例详解

Android ListView添加头布局和脚布局 之前学习喜马拉雅的时候做的一个小Demo,贴出来,供大家学习参考: 如果我们当前的页面有多个接口.多种布局的话,我们一般的选择无非就是1.多布局:2.各种复杂滑动布局外面套一层ScrollView(好low):3.头布局脚布局.有的时候我们用多布局并不能很好的实现,所以头布局跟脚布局就是我们最好的选择了:学过了ListView的话原理很简单,没啥理解的东西,直接贴代码了: 效果图: 正文部分布局: fragment_classify.xml <