基本上我有一个包含新闻标题的列表视图,这样第一个标题覆盖顶部,备用标题第二部分和第三个标题第三部分,第二个和第三个在列表中是连续的(第一个仍然是唯一的标题名单 )。我以编程方式将其定义如下:
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 布局就足够了,因此我可以单独为平板电脑创建布局,而不会影响电话代码)。谢谢!