【问题标题】:Custom Android ListView自定义 Android 列表视图
【发布时间】:2013-06-04 20:56:06
【问题描述】:

我是这个优秀网站和 Android 的 Java 编程新手。我开始制作一个小型测试应用程序,用于列出我镇上我最喜欢的地方。我尝试按照不同页面上的一些教程进行操作,但是当我在 Eclipse 中放入我的项目时,尽管导入了类和其他方法,但总是给我超过一百万个错误。

我想在图像名称 disco 旁边的示例图像 discotheque 上进行构建,并在该名称下使用 Diskotek 较小的文本附加信息。

非常感谢大家的帮助

【问题讨论】:

  • 帮助什么?您没有提出问题或向我们提供有关您遇到的问题的任何详细信息
  • 问题是:如何创建像我解释的那样的 ListView

标签: android list view


【解决方案1】:

Custom ListView 总共需要 4 个基本的东西 2 个设计 (layout .xml) 和 2 个类 (.java)

2 布局
a) 基本上一个容器有一个 listview 带有一个标题或按钮取决于你
b) 如果每行有buttonsimagestextview,你想怎么做都应该是什么样子。

2 Java 类文件
a) 一个是Activity,您肯定会拥有它。
b) Custom Adapter 表示您的Activity 中的哪个值将根据您的要求转到哪个View (Button , image)。

最好的例子就是关注这个Tutorial

【讨论】:

  • 正是我所要求的。非常感谢先生!
  • @Andro 然后请 +1 :D 很高兴帮助
【解决方案2】:

当有人说“我尝试遵循一些教程......”但它们不起作用时,我首先想到的是,这让人难以置信。

  • 您尝试的代码在哪里?
  • 编辑器上的导入错误是什么?

这将是一个更容易解决的问题。

给你一个简单的ListView例子:

首先,根据您的喜好创建一个资源文件:(example.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/disco_image"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_home"
        android:layout_width="96dp"
        android:layout_height="96dp"/>
<TextView
        android:id="@+id/disco_title"
        android:padding="12dp"
        android:layout_toRightOf="@+id/disco_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<TextView
        android:id="@+id/disco_info"
        android:padding="12dp"
        android:layout_toRightOf="@+id/disco_image"
        android:layout_below="@+id/disco_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

然后,创建一个自定义适配器,它们非常简单。现在只需扩展一个 BaseAdapter。

public class ExampleAdapter extends BaseAdapter {

//Let's create some constants first, to fill out the rows
private static final String [] DISCO_NAMES = {"Disco One", "Disco Two", "Disco Three", "Disco Four"};
private static final String [] DISCO_INFO = {"Some Info One", "Some Info Two", "Some Info Three", "Some Info Four"};

private LayoutInflater mInflater;

//Our custom adapter needs a constructor, so you can create from your activity.
public ExampleAdapter (final Context context) {
    //for now, let's just get the context, we'll need it to inflate views
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    //this needs to return to the amount of rows you want to display.
    //right now we return a fixed value, this could vary based on your needs
    return DISCO_NAMES.length;
}

@Override
public Object getItem(int pos) {
    //this is useful for knowing what item is at what position
    //for now, let's just return the disco name shall we?
    return DISCO_NAMES[pos];
}

@Override
public long getItemId(int pos) {
    //This returns an id to the item
    //personally I don't use this, so you can just return the position
    return pos;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    //Ha, here's the important part
    //ListViews reuse rows, so let's check if the view (also known as convertview) is new or being reused
    if (view == null) {
        //this means it's a new view, so we need to inflate it
        view = mInflater.inflate(R.layout.example, null);
    }
    ((TextView) view.findViewById(R.id.disco_title)).setText(DISCO_NAMES[position]);
    ((TextView) view.findViewById(R.id.disco_info)).setText(DISCO_INFO[position]);
    //You can also set some images to the imageview on the layout we created earlier
    return view;
    }
}

然后,让我们创建一个 ListActivity 作为示例。 注意 ListActivit 不需要通过 setContentView 设置 Layout Resource,所以我们这里不调用它。

public class ExampleListActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //create the adapter
    ExampleAdapter mExampleAdapter = new ExampleAdapter(this);

    //fill the listView
    setListAdapter(mExampleAdapter);
    }
}

应该按原样编译,但出于性能原因,您可能需要查看 ViewHolder 模式和等等等等。显然您需要阅读更多内容,但我希望这有助于作为一个起点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    相关资源
    最近更新 更多