如何以表格形式相邻地调用列表中的 2 个连续项目? [英] How do I call 2 consecutive items in a list in a table form next to each other?

查看:44
本文介绍了如何以表格形式相邻地调用列表中的 2 个连续项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个包含新闻标题的列表视图,其中第一个标题覆盖顶部,备用标题覆盖第二部分和第三个标题第三部分,第二个和第三个在列表中是连续的(第一个仍然是列表中唯一的标题).我以编程方式将其定义如下:

Basically I have a list view containing news headlines such that the first headline covers the top portion, the alternate headline the second portion and third headline third portion and the second and the third are consecutive in the list( the first remains as the only headline in the list ). I have programmatically defined it as follows:

public class NewsListAdapter extends UselessAdapter {

    private static final int NUM_TYPES = 3;
    LayoutInflater mInflater;

    private static final class Types {
        public static final int FIRST_HEADLINE = 0;
        public static final int OTHER_HEADLINE = 1;
        public static final int ALTERNATE_HEADLINE = 2;
    }

    public NewsListAdapter(final Context context, final int layout,
            final String[] from, final int[] to) {
        super(context, layout, from, to);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(final int position) {
        if (position == 0) {
            return Types.FIRST_HEADLINE;
        }
        if (position %2 == 0 && position!= 0) {
            return Types.ALTERNATE_HEADLINE;
        } else {
            return Types.OTHER_HEADLINE;
        }
    }

    @Override
    public int getViewTypeCount() {
        return NUM_TYPES;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (!mDataValid) {
            throw new IllegalStateException("this should only be called when the cursor is valid");
        }

        if (!mCursor.moveToPosition(position)) {
            throw new IllegalStateException("couldn't move cursor to position " + position);
        }

        final int type = getItemViewType(position);

        if(convertView == null) {
            convertView = newView(type, parent);
        }
        if (position % 2 == 0){
            convertView.setBackgroundResource(R.drawable.oddcellcolor);
        } else {
            convertView.setBackgroundResource(R.drawable.evencellcolor);
        }
        bindView(convertView, mContext, mCursor);
        return convertView;
    }

    private View newView(final int type, final ViewGroup parent) {
        View view;
        switch (type) {
        case Types.FIRST_HEADLINE:
            view = mInflater.inflate(R.layout.item_news_first_headline, parent, false);
            break;
        case Types.ALTERNATE_HEADLINE:
            view = mInflater.inflate(R.layout.item_news_alternate_headline, parent, false);
            break;
        default:
            view = mInflater.inflate(R.layout.item_news_headline, parent, false);
            break;
        }

        return view;
    }
}

现在,我的 item_news_alternate_headline 和 item_news_headline 代码分别在 2 个不同的 xml 文件中相同:

Now, I have item_news_alternate_headline and item_news_headline codes the same in 2 different xml files respectively:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res/com.justin.jar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/news_headline_padding" >


    <com.justin.jar.utils.FontTextView
        android:id="@+id/news_headline_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="3"
        android:paddingLeft="@dimen/common_left_padding"
        android:paddingRight="5dp"
        android:textSize="@dimen/news_first_headline_text" 
        foo:customFont="cabin.medium.ttf"
        android:textColor="@color/search_autosuggest_header_text"
        android:layout_toLeftOf="@id/news_headline_image" />

    <com.justin.jar.utils.FontTextView
        android:id="@+id/metadata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/search_autosuggest_item_subtitle"
        android:textSize="@dimen/metadata" 
        android:paddingLeft="@dimen/common_left_padding"
        android:paddingRight="5dp"
        android:layout_alignLeft="@id/news_headline_text"
        android:layout_toLeftOf="@id/news_headline_image"
        android:layout_below="@id/news_headline_text" 
        />
</RelativeLayout>

现在,当我调用运行我的项目时,我得到以下输出:

Now when I call run my project I get the following output:

我现在想更改为:

每个单元格都是可选择的,因此需要使第二个和第三个标题彼此相邻(不改变第一个标题)每个单元格都可以单独选择.我如何着手创建布局并更改我当前的 java 代码(请注意:我想只为平板电脑而不是手机更改代码,所以如果有人能提出一种方法,那就太棒了编辑 xml 布局就足够了,这样我就可以单独为平板电脑创建布局而不会影响电话代码).谢谢!

Each cell is selectable, so need to make it such that for the second and the third headline placed next to each other(without altering the first headline) each is individually selectable. How do I go about creating a layout and altering my current java code for the same (Please note: I would like ot change the code just for tablets and not for phone, so it'll be awesome if anyone can suggest a way in which editing the xml layout will be enough so I can create layouts for tablets separately without affecting phone code). Thanks!

推荐答案

使用网格视图.让它自动适应同样的方法得到 getItemViewType先退回大件.小憩.我认为这应该有效,从未尝试过.

Use grid view. make it auto fit fellow same approach get getItemViewType return large item for first. and rest small. I think this should work, never tried this.

或请参阅 http://sudarnimalan.blogspot.sg/2012/06/android-bigger-image-for-first-item-of.html类似于你的问题.

or refer http://sudarnimalan.blogspot.sg/2012/06/android-bigger-image-for-first-item-of.html similar to your problem.

这篇关于如何以表格形式相邻地调用列表中的 2 个连续项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆