`
xsuo
  • 浏览: 123493 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Iconified TextList - The making of

阅读更多
Iconified TextList - The making of

What you will learn: You will learn how to create your own ListAdapter: IconifiedListAdapter

Question Problems/Questions: Write it right below...

Difficulty: 1.5 of 5 Smile

What it will look like:


Description:
The app (screenshot above) is created with the following simple code. So that is what we finally want to reach:
Java:
IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);

// Add four items
itla.addItem(new IconifiedText(
"Surf Web", getResources().getDrawable(R.drawable.favicon)));
itla.addItem(new IconifiedText(
"Report Bug", getResources().getDrawable(R.drawable.bug)));
itla.addItem(new IconifiedText(
"Speak Spanish", getResources().getDrawable(R.drawable.locale)));
itla.addItem(new IconifiedText(
"Vidoop", getResources().getDrawable(R.drawable.vidoop)));
// Display it
setListAdapter(itla);


We simply create an: IconifiedTextListAdapter, throw some String+Drawable into and 'display it. To use such awesome simple code, we need to do some work.

1. Lets start with the very Base: The IconifiedText.java which is a pretty small class, which only contains a simply constructor and some getters & setters. It is only a class that combines a String and a Drawable(Icon), nothing special!

Java:
public class IconifiedText{

private String mText = "";
private Drawable mIcon;
private boolean mSelectable = true;

public IconifiedText(String text, Drawable bullet) {
mIcon = bullet;
mText = text;
}
// ...
}


2. Now we take a look at the IconifiedTextListAdapter.java which consists of about 50 Lines of Code, but does not much. It basically is a storage for a list of IconifiedTexts, to which you can "add(IconifiedText);", and that passes through some functions to the elements of the IconifiedText-List, like 'getItem(int position)' or 'isSelectable(int position)'.
Being an (specialized) Adapter , the second thing to provide a function through which one can access the views (all the things) it contains:
Java:
/** @param convertView The old view to overwrite, if one is passed
* @returns a IconifiedTextView that holds wraps around an IconifiedText */

public View getView(int position, View convertView, ViewGroup parent) {
IconifiedTextView btv;
if (convertView == null) {
btv = new IconifiedTextView(mContext, mItems.get(position));
} else { // Reuse/Overwrite the View passed
// We are assuming(!) that it is castable!
btv = (IconifiedTextView) convertView;
btv.setText(mItems.get(position).getText());
btv.setIcon(mItems.get(position).getIcon());
}
return btv;
}

That was probably also not too hard to understand (if so, ask for explanation below).

3.The IconifiedTextView is also not too hard to understand.
It extends LinearLayout. Neutral
Idea Lets remember, that GUIs in Android are nested. ( A view, can contain a View, can contain a View, can contain a View, can contain a View, can contain a View...)
Arrow So it will be no problem, that our IconifiedTextView itself consists of two more Views, aIconView and a TextView.
The IconifiedTextView does it own setup in the Constructor:
Java:
public IconifiedTextView(Context context, IconifiedText aIconifiedText) {
super(context);

/* First Icon and the Text to the right (horizontal),
* not above and below (vertical) */

this.setOrientation(HORIZONTAL);

mIcon = new ImageView(context);
mIcon.setImageDrawable(aIconifiedText.getIcon());
// left, top, right, bottom
mIcon.setPadding(0, 2, 5, 0); // 2px up, 5px to the right

/* At first, add the Icon to ourself
* (! we are extending LinearLayout) */

addView(mIcon, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

mText = new TextView(context);
mText.setText(aIconifiedText.getText());
/* Now the text (after the icon) */
addView(mText, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}


So thats it =). You successfully created an IconifiedTextList(Adapter) Exclamation

The full source:

ArrowArrowIcons (res/drawable/*.png) used in Demo App Exclamation

'src/your_package_structure/TestLayout.java'
Java:
/* $Id: TestLayout.java 57 2007-11-21 18:31:52Z steven $
*
* Copyright 2007 Steven Osborn
*
* 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.anddev.android.testproject;

import android.app.ListActivity;
import android.os.Bundle;

public class TestLayout extends ListActivity {

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);

// Add four items
itla.addItem(new IconifiedText(
"Surf Web", getResources().getDrawable(R.drawable.favicon)));
itla.addItem(new IconifiedText(
"Report Bug", getResources().getDrawable(R.drawable.bug)));
itla.addItem(new IconifiedText(
"Speak Spanish", getResources().getDrawable(R.drawable.locale)));
itla.addItem(new IconifiedText(
"Vidoop", getResources().getDrawable(R.drawable.vidoop)));
// Display it
setListAdapter(itla);
}
}


'src/your_package_structure/IconifiedText.java'
Java:
/*
* Copyright 2007 Steven Osborn
*
* 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.anddev.android.testproject;

import android.graphics.drawable.Drawable;

/** @author Steven Osborn - http://steven.bitsetters.com */
public class IconifiedText implements Comparable<IconifiedText>{

private String mText = "";
private Drawable mIcon;
private boolean mSelectable = true;

public IconifiedText(String text, Drawable bullet) {
mIcon = bullet;
mText = text;
}

public boolean isSelectable() {
return mSelectable;
}

public void setSelectable(boolean selectable) {
mSelectable = selectable;
}

public String getText() {
return mText;
}

public void setText(String text) {
mText = text;
}

public void setIcon(Drawable icon) {
mIcon = icon;
}

public Drawable getIcon() {
return mIcon;
}

/** Make IconifiedText comparable by its name */
@Override
public int compareTo(IconifiedText other) {
if(this.mText != null)
return this.mText.compareTo(other.getText());
else
throw new IllegalArgumentException();
}
}


'src/your_package_structure/IconifiedTextView.java'
Java:
/* $Id: BulletedTextView.java 57 2007-11-21 18:31:52Z steven $
*
* Copyright 2007 Steven Osborn
*
* 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.anddev.android.testproject;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class IconifiedTextView extends LinearLayout {

private TextView mText;
private ImageView mIcon;

public IconifiedTextView(Context context, IconifiedText aIconifiedText) {
super(context);

/* First Icon and the Text to the right (horizontal),
* not above and below (vertical) */

this.setOrientation(HORIZONTAL);

mIcon = new ImageView(context);
mIcon.setImageDrawable(aIconifiedText.getIcon());
// left, top, right, bottom
mIcon.setPadding(0, 2, 5, 0); // 5px to the right

/* At first, add the Icon to ourself
* (! we are extending LinearLayout) */

addView(mIcon, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

mText = new TextView(context);
mText.setText(aIconifiedText.getText());
/* Now the text (after the icon) */
addView(mText, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

public void setText(String words) {
mText.setText(words);
}

public void setIcon(Drawable bullet) {
mIcon.setImageDrawable(bullet);
}
}


'src/your_package_structure/IconifiedTextListAdapter.java'
Java:
/* $Id: BulletedTextListAdapter.java 57 2007-11-21 18:31:52Z steven $
*
* Copyright 2007 Steven Osborn
*
* 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.anddev.android.testproject;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/** @author Steven Osborn - http://steven.bitsetters.com */
public class IconifiedTextListAdapter extends BaseAdapter {

/** Remember our context so we can use it when constructing views. */
private Context mContext;

private List<IconifiedText> mItems = new ArrayList<IconifiedText>();

public IconifiedTextListAdapter(Context context) {
mContext = context;
}

public void addItem(IconifiedText it) { mItems.add(it); }

public void setListItems(List<IconifiedText> lit) { mItems = lit; }

/** @return The number of items in the */
public int getCount() { return mItems.size(); }

public Object getItem(int position) { return mItems.get(position); }

public boolean areAllItemsSelectable() { return false; }

public boolean isSelectable(int position) {
try{
return mItems.get(position).isSelectable();
}catch (IndexOutOfBoundsException aioobe){
return super.isSelectable(position);
}
}

/** Use the array index as a unique id. */
public long getItemId(int position) {
return position;
}

/** @param convertView The old view to overwrite, if one is passed
* @returns a IconifiedTextView that holds wraps around an IconifiedText */

public View getView(int position, View convertView, ViewGroup parent) {
IconifiedTextView btv;
if (convertView == null) {
btv = new IconifiedTextView(mContext, mItems.get(position));
} else { // Reuse/Overwrite the View passed
// We are assuming(!) that it is castable!
btv = (IconifiedTextView) convertView;
btv.setText(mItems.get(position).getText());
btv.setIcon(mItems.get(position).getIcon());
}
return btv;
}
}
分享到:
评论

相关推荐

    Iconified

    【标题】:“Iconified”可能是指一个图标设计或者字体图标相关的项目。在IT领域,"Iconified"这个词常用于表示将应用程序或窗口最小化至仅显示图标的状态,但在这个上下文中,它似乎关联到一种特定的字体资源。 ...

    计算机英语

    - **ICONIFIED**:最小化状态,通常指窗口被最小化到任务栏的状态。 - **Expire**:过期,指数据或对象的有效期结束。 - **Extract**:提取,从文件或数据集中抽取特定部分的操作。 - **Regedit**:注册表编辑器,...

    Android搜索框(SearchView)的功能和用法详解

    ①setIconifiedByDefault(boolean iconified):设置搜索框默认是否自动缩小为图标。 ②setOnQueryTextListener(SearchView.OnQueryTextListener listener):为搜索框设置监听器,监听用户的输入并执行搜索操作。 ...

    Android特效之--Menu

    例如,添加图标到Menu项,可以使用`IconifiedText`库,如下所示: ```java MenuItem item = menu.findItem(R.id.action_one); IconifiedTextMenuItem iconifiedItem = (IconifiedTextMenuItem) MenuItemCompat....

    java JFrame最大化问题

    - `ICONIFIED`:表示窗口被最小化为图标。 - `MAXIMIZED_HORIZ`:表示窗口水平方向被最大化。 - `MAXIMIZED_VERT`:表示窗口垂直方向被最大化。 - `MAXIMIZED_BOTH`:表示窗口同时在水平和垂直方向上被最大化。 ...

    FileManager.7z

    简单的android文件管理器源码,从书上的例子改来的。 @打开没有权限的文件夹死机 @二级目录前面有"/" ... ├── IconifiedText.java ├── IconifiedTextListAdapter.java └── IconifiedTextView.java

    仿QQ登录窗体,拉伸展开!

    import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics;...import java.awt.GridLayout;... setLocation((displaySize.width - frame...

    qq项目第一天(java JFrame 标题栏 最小化 最大化 拖动窗体)

    frame.setState(JFrame.ICONIFIED); // 设置窗口为最小化状态 ``` 3. **拖动窗体**: 要实现窗口可以被拖动,我们需要监听鼠标事件,特别是鼠标按下和移动事件。这里可以创建一个内部类实现 `MouseListener` 和 ...

    android 文件管理器

    简单的android文件管理器源码,从书上的例子改来的。 @打开没有权限的文件夹死机 @二级目录前面有"/" ...├── IconifiedText.java ├── IconifiedTextListAdapter.java └── IconifiedTextView.java

    - `JFrame`有几种状态,如DEFAULT、ICONIFIED(最小化)、MAXIMIZED_BOTH(最大化)、NORMAL(常规)。可以通过`setExtendedState()`改变这些状态。 5. **透明度支持** Java 6以后的版本支持`JFrame`的透明度设置...

    WPF 设置 ShowInTaskbar 对窗口最小化的影响.rar

    在JavaFX中,虽然没有直接对应的`ShowInTaskbar`属性,但有类似的概念,如`Stage`的`iconified`状态管理和任务栏集成的配置。 总之,`WPF 设置 ShowInTaskbar 对窗口最小化的影响`这一主题涵盖了Windows桌面应用...

    如何实现程序的托盘化

    this.setDefaultCloseOperation(JFrame.ICONIFIED); ``` #### 总结 实现程序的托盘化不仅能够提升应用程序的用户体验,还能有效利用系统资源,特别适合于需要长期运行或作为后台服务的应用场景。通过本文介绍的...

    向QQ一样自动隐藏代码

    3. **执行隐藏逻辑**:一旦检测到窗口即将触碰屏幕边缘,你可以通过调用窗口对象的`setState()`方法,传入`JFrame.ICONIFIED`参数,将窗口最小化至任务栏,从而实现类似QQ窗口的自动隐藏效果。 4. **设置恢复机制**...

    swing组件和容器实例

    System.out.println("window iconified"); } public void windowDeiconified(WindowEvent e) { System.out.println("window deiconified"); } public void windowActivated(WindowEvent e) { System.out....

    JFrame最小化到托盘

    if (e.getNewState() == Frame.ICONIFIED) { frame.setVisible(false); } else if (e.getNewState() == Frame.NORMAL) { frame.setVisible(true); } } }); ``` 5. **注意事项**:不同的操作系统可能会有不同...

    java开发gui教程之jframe监听窗体大小变化事件和jframe创建窗体

    Java中窗口的状态由特定的整数常量表示,例如,`java.awt.WindowConstants`类中定义了`ICONIFIED`(最小化)状态对应整数`1`,`NORMAL`(初始状态)对应整数`0`,`MAXIMIZED_BOTH`(最大化)对应整数`6`等。...

Global site tag (gtag.js) - Google Analytics