0

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

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;
    }
}

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

<?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>

现在,当我调用运行我的项目时,我得到以下输出: 在此处输入图像描述

我现在想更改为: 在此处输入图像描述

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

4

3 回答 3

0

是的,您可以为此使用 gridView。这是示例代码

只需检查链接 1链接 2

于 2013-11-21T13:19:33.697 回答
0

这基本上归结为在以下之间找到折衷:

  1. 一个 ListView (你被困在单独的行中)或 GridView (其中有固定数量的不可跨越的列),或者
  2. TableLayout 或 GridLayout 形式的固定布局。

显然,第一个选项开箱即用是不够的,因为您希望某些行包含一列,而另一些行包含两列。您可能会在编写自己的 ListView 实现时找到折衷方案,但这对您的应用程序来说非常繁琐,而且很可能过度杀伤力。

我知道允许交错列(StaggeredGridView)的自定义组件,但您需要跨列。也许在某个地方还有另一个开源自定义组件可以完成这项工作。

第二个选项 - 在 TableLayout 或 GridLayout 中编写固定布局 - 可能不适合您的需要,因为列表的长度是动态的,布局会很慢,并且将图像加载到占位符中会消耗大量的记忆。

相反,我建议使用 ListView 并编写一个自定义列表适配器来扩展不同的布局。这些布局应该包括处理点击事件的 ViewGroups,而不是通过 ListView 自己的 OnItemClickListener 来完成。也许你可以有两种布局:

  • 一种标题布局,本质上是一种跨越整个宽度的布局;
  • 一种多项目布局,以相同的权重并排显示两个新闻项目。

这确实意味着您需要提供具有选择器状态的自定义背景并自己处理各个 ViewGroups 的 OnClickListener,但它还允许您充分利用不同屏幕尺寸的布局。例如,通过使用,您可以考虑为或<include>显示三个彼此相邻的新闻项目。也许在三列的情况下,您甚至可以将带有图像的文章的两列跨度和没有的文章的单列混合起来。res/layout-landres/layout-w720dp

于 2013-11-24T19:49:25.843 回答
0

使用网格视图。让它自动适合同胞相同的方法首先获取 getItemViewType 返回大项目。休息小。我认为这应该可行,从未尝试过。

或参考 与您的问题类似的http://sudarnimalan.blogspot.sg/2012/06/android-bigger-image-for-first-item-of.html 。

于 2013-11-26T09:13:09.910 回答