Android 具有复杂数据和复杂布局的复杂 ListView 示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2275232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:15:03  来源:igfitidea点击:

Complex ListView example with complex data and complex layout of each row?

androidlistviewlayout

提问by Eno

Im pulling a list of product objects from a database call and I want to build a ListView of the data in my activity with a non-scrolling header and footer. Each row will consist of a product thumbnail, some data and a button arranged horizontally. I understand that using the ListView I can inflate each row using a separate layout.

我从数据库调用中提取了一个产品对象列表,我想在我的活动中构建一个带有非滚动页眉和页脚的数据的 ListView。每行将包含一个产品缩略图、一些数据和一个水平排列的按钮。我知道使用 ListView 我可以使用单独的布局来增加每一行。

Most of the examples Ive seen using ListView just show a simple array of strings that fill the whole view of the activity, but most real-world examples will be more complex and I can't find any good examples that explain how all these pieces fit together.

我见过的大多数使用 ListView 的示例只显示了一个简单的字符串数组,它们填充了整个活动的视图,但大多数现实世界的示例会更复杂,我找不到任何好的示例来解释所有这些部分是如何适合的一起。

Does anyone have any pointers to sample code with a good explanation ?

有没有人有任何指向示例代码的指针并有很好的解释?

采纳答案by Andy

An example that helped me a lot:

一个对我帮助很大的例子:

http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

I hope this would be useful to you too.

我希望这对你也有用。

回答by sreejithmohanan

private static class CustomCursorAdapter extends CursorAdapter {

    protected ListView mListView;

    protected static class RowViewHolder {
        public TextView mTitle;
        public TextView mText;
    }

    public CustomCursorAdapter(Activity activity) {
        super();
        mListView = activity.getListView();
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // do what you need to do
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = View.inflate(context, R.layout.row_layout, null);

        RowViewHolder holder = new RowViewHolder();
        holder.mTitle = (TextView) view.findViewById(R.id.Title);
        holder.mText = (TextView) view.findViewById(R.id.Text);

        holder.mTitle.setOnClickListener(mOnTitleClickListener);
        holder.mText.setOnClickListener(mOnTextClickListener);

        view.setTag(holder);

        return view;
    }
}

回答by Cheryl Simon

The easiest way to have a complex row in a ListView is via a SimpleAdapter. There are a number of examples around for that.

在 ListView 中拥有复杂行的最简单方法是通过 SimpleAdapter。有很多这样的例子。

To force the ListView to fill the remaining portion of the screen, set the properties as follows:

要强制 ListView 填充屏幕的剩余部分,请按如下方式设置属性:

Use a LinearLayout.

使用线性布局。

On the list view:

在列表视图中:

android:layout_height="fill_parent",
android:height="0dp",
android:layout_weight="1"

On all other widgets:

在所有其他小部件上:

android:layout_height="wrap_content"

Do not set a height or layout_weight

不要设置高度或 layout_weight