Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 691|回复: 0

[实例教程][Android实例] Android 组件SectionedAdapter

[复制链接]

该用户从未签到

发表于 2011-10-22 12:16:27 | 显示全部楼层 |阅读模式
不过AggregatedAdapter比较难反编译出来,没关系,加上cwac-merge这个组件,就可以实现同样的效果。不同分段的Adapter,继承SectionedAdapter,实现自己的东西,该干啥就干啥,然后merge起来。

       SectionAdapter.java

java代码: /*

* Copyright (C) 2010 lytsing.org

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/



package org.lytsing.adapters;



import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;



/**

* abstract SectionAdapter, difference from

* cw-advandroid/ListView/Sections/src/com/commonsware/android/listview/SectionedAdapter.java

* just decompile from Vending.apk

*

*/

abstract public class SectionAdapter extends BaseAdapter {

protected int mCount;

protected boolean mDeactivated;

protected View mSectionHeaderView;



/**

* Constructor

*

* @param sectionTitleId The resource ID for a layout file containing a layout to use when

* instantiating views.

* @param context The current context.

* @param parent The parent that this view will eventually be attached to

*/

public SectionAdapter(int sectionTitleId, Context context, ViewGroup parent) {

this(Util.inflateView(R.layout.section_header, context, parent));

((TextView)mSectionHeaderView).setText(sectionTitleId);

}



/**

* Constructor

*

* @param sectionHeaderView The header view

*/

public SectionAdapter(View sectionHeaderView) {

mSectionHeaderView = sectionHeaderView;

mDeactivated = false;

mCount = 0;

}



public void activate() {

if (mDeactivated) {

mDeactivated = false;

notifyDataSetChanged();

}

}



/**

* Are all items in this SectionAdapter enable?

* If yes it means all items are selectable and clickable.

*/

public boolean areAllItemsEnabled() {

return false;

}



public void deactivate() {

if (mDeactivated == false) {

mDeactivated = true;

notifyDataSetChanged();

}

}



/**

* How many items are in the data set represented by this

* Adapter.

*/

public int getCount() {

if (mDeactivated) {

return 0;

} else {

return mCount + 1; // add one for header

}

}



/**

* Get the data item associated with the specified

* position in the data set.

*

* @param position Position of the item whose data we want

*/

public Object getItem(int position) {

if (position == 0) {

return mSectionHeaderView;

}



return null;

}

java代码:
/**

* Get the row id associated with the specified position

* in the list.

*

* @param position Position of the item whose data we want

*/

public long getItemId(int position) {

if (position == 0) {

return mSectionHeaderView.getId();

}



return 0;

}



/**

* Get a View that displays the data at the specified

* position in the data set.

*

* @param position Position of the item whose data we want

* @param convertView View to recycle, if not null

* @param parent ViewGroup containing the returned View

*/

public View getView(int position, View convertView, ViewGroup parent) {



return position == 0 ? mSectionHeaderView : null;

}



/**

* Returns true if the item at the specified position is not a separator

* (A separator is a non-selectable, non-clickable item).

*

* @param position Index of the item

* @return True if the item is not a separator

*/

public boolean isEnabled(int position) {

return false;

}

}

   Util.java

java代码:
/*

* Copyright (C) 2010 lytsing.org

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/



package org.lytsing.adapters;



import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



/**

* Util

*

*/

public class Util {



private Util() {

}



/**

* Inflate a new view hierarchy from the specified XML resource.

*

* @param resource ID for an XML layout resource to load

* @param context The current context.

* @return The root View of the inflated XML file.

*/

public static View inflateView(int resource, Context context) {



return inflateView(resource, context, null);

}



/**

* Inflate a new view hierarchy from the specified xml resource.

*

* @param resourceID for an XML layout resource to load (e.g.,

* @param context The current context.

* @param parent simply an object that provides a set of LayoutParams

* values for root of the returned hierarchy

* @return The root View of the inflated XML file.

*/

public static View inflateView(int resource, Context context, ViewGroup parent) {

LayoutInflater vi = (LayoutInflater)context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



return vi.inflate(resource, parent, false);

}

}

  res/layout/section_header.xml

java代码:
<?xml version="1.0" encoding="utf-8"?>

<TextView

xmlns:android="http://schemas.android.com/apk/res/android"

android:textAppearance="?android:attr/textAppearanceSmall"

android:textStyle="bold"

android:gravity="center_vertical"

android:background="@android:drawable/dark_header"

android:paddingLeft="8.0dip"

android:layout_width="fill_parent"

android:layout_height="26.0dip"

>

</TextView>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-5-5 21:08 , Processed in 0.383510 second(s), 38 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表