您的位置:首页 > 移动开发 > Android开发

Android功能组件源代码分析之二《AmazingListView》

2015-12-07 13:28 399 查看
    写一系列这种分析的文章,主要是用于理清这些优秀开源组建的设计模式和思想,为提高自己的开发水平和设计架构能力打好基础。
    目前有不少的开源ListView组件可以使用,像XListView、AmazingListView等。相对于原生的ListView,它们提供更加方便的功能供我们使用。例如XListView就提供完整的头部“下拉更新“和底部“加载更多”等功能。
ImageList则提供分组->详细这种显示模式。下面看看一个AmazingListView截图:



    AmazingListView提供了listFooter和mHeadView,不过这两个暂时没用,你可以根据你的需要将他们添加入listview中,比较有用的是其Adapter-AmazingAdapter
当然AmazingAdapter中最重要的当然是getView函数,里面可以到这个listview是如何布局的,其代码如下:

public final View getView(int position, View convertView, ViewGroup parent) {
View res = getAmazingView(position, convertView, parent);//获取正常显示的view
// 判断是否为最后一条且自动加载模式
if (position == getCount() - 1 && automaticNextPageLoading) {
onNextPageRequested(page + 1);//目前暂时还没有使用
}
//position这里为第几条消息的意思的位置
//selection这里为header对应的position
final int section = getSectionForPosition(position);
//如果还是当前的selection则不需要再讲header显示出来
//如果item为selection的第一条数据,则需要显示header
boolean displaySectionHeaders = (getPositionForSection(section) == position);//判断是否为selection第一条item
//具体header操作,如果displaysectionheaders为true时,则显示header
//否则就隐藏header
bindSectionHeader(res, position, displaySectionHeaders);

return res;
}


其中getAmazingView为一条Item显示的View。而bindSectionHeader则判断是否要将header放上去。
中间的代码则判断是否换header了,如果没有,则没有必要添加header,如果有,则添加header

而AmazingAdapter并没有提供具体的getAmazingView和bindSectionHeader,而只提供两个抽象函数,具体要
要在AmazingAdapter的集成类中实现。现在我们假定MyAmazingAdapter继承AmazingAdapter,其代码如下:

public View getAmazingView(int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(
R.layout.forum_home_item, null);
}
TextView tv = (TextView) convertView
.findViewById(R.id.forumHomeItemTitle);
tv.setText(m_forumModel.get(position).get("name"));

tv = (TextView) convertView
.findViewById(R.id.forumHomeItemSubtitle);
tv.setText("subTitle");
return convertView;
}
protected void bindSectionHeader(View view, int position,
boolean displaySectionHeader) {
if (displaySectionHeader) {
//forumhomeitemheader被include在forum_homte_item文件中
//且位置放在forum_home_item的顶部
view.findViewById(R.id.forumHomeItemHeader).setVisibility(
View.VISIBLE);
TextView lSectionTitle = (TextView) view
.findViewById(R.id.forumHomeItemHeader);
lSectionTitle.setText(m_groupModel.get(
getSectionForPosition(position)).get("forumTitle"));
} else {
view.findViewById(R.id.forumHomeItemHeader).setVisibility(
View.GONE);
}
}


其中m_forumModel为你需要显示的item数组,m_groupModel为你需要显示的section数据组,其中header中就放置section中数据
每个section组中,可能有一个或者多个item条目组成。

我们发现getAmazingView比较简单,只是将条目的名称显示出来。而bindSectionHeader也比简单,根据条件是否为真,判断
是否显示header头。

其中比较奇怪的,为什么我要将getAmazingView返回view要传递到bindSectionHeader中?
是因为,我们来看看两个view所对应的layout文件

AmazingView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="vertical" >

<include
android:id="@+id/forumHomeItemHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/forum_home_item_header" />

<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:src="@drawable/forum_new" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp" >

<TextView
android:id="@+id/forumHomeItemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/forumHomeItemSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15sp"
android:visibility="gone" />
</LinearLayout>

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/navigation_next_item" />

</LinearLayout>

</LinearLayout>

Header
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/item_header_color"
android:paddingLeft="5dp"
android:textSize="12sp"
android:textStyle="bold" />


我们比较清楚地可以看到AmazingView将header作为头文件include进去的。因此在bindSectionHeader中传入getAmazingView返回的View,就可以使用该View对header中的组件进行直接控制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: