0

基本的に、最初の見出しが上部を覆い、代替の見出しが2番目の部分を覆い、3番目の見出しが3番目の部分を覆い、2番目と3番目がリスト内で連続するようなニュースの見出しを含むリストビューがあります(最初は唯一の見出しとして残りますリスト )。プログラムで次のように定義しました。

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 ファイルで同じにしています:

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

run my project を呼び出すと、次の出力が得られます。 ここに画像の説明を入力

私は今に変更したい: ここに画像の説明を入力

各セルは選択可能であるため、(最初の見出しを変更せずに) 隣り合わせに配置された 2 番目と 3 番目の見出しを個別に選択できるようにする必要があります。レイアウトを作成し、現在の Java コードを同じように変更するにはどうすればよいですか (注: 電話ではなくタブレット用にコードを変更したいので、誰かがその方法を提案できれば素晴らしいことです) xml レイアウトを編集するだけで十分なので、電話コードに影響を与えずにタブレット用のレイアウトを個別に作成できます)。ありがとう!

4

3 に答える 3

0

はい、これには gridView を使用できます。ここにコード例があります

リンク1とリンク2を確認してください

于 2013-11-21T13:19:33.697 に答える
0

グリッド ビューを使用します。getItemViewType を取得すると、最初に大きなアイテムが返されます。小さく休んでください。これはうまくいくはずだと思いますが、これを試したことはありません。

または 、問題に似たhttp://sudarnimalan.blogspot.sg/2012/06/android-bigger-image-for-first-item-of.htmlを参照してください。

于 2013-11-26T09:13:09.910 に答える
0

これが本質的に要約することは、次の間の妥協点を見つけることです。

  1. ListView (個々の行で立ち往生している場合) または GridView (一定数のスパン不可能な列がある場合)、または
  2. TableLayout または GridLayout の形式の固定レイアウト。

明らかに、最初のオプションはそのままでは十分ではありません。1 つの列を持つ行と 2 つの列を持つ行が必要だからです。ListView の独自の実装を作成する際に妥協点が見つかるかもしれませんが、これは非常に退屈で、アプリケーションにとってやり過ぎになる可能性が高くなります。

ずらした列 ( StaggeredGridView )を許可するカスタム コンポーネントを認識していますが、代わりに列のスパンが必要です。おそらく、これを実現する別のオープン ソース カスタム コンポーネントがどこかにあるでしょう。

2 番目のオプション (TableLayout または GridLayout に固定レイアウトを書き込む) は、リストの長さが動的であり、レイアウトが遅くなり、プレースホルダーへの画像の読み込みにかなりの量が消費されるため、ニーズに合わない可能性があります。メモリの。

代わりに、ListView を使用して、さまざまなレイアウトを拡張するカスタム リスト アダプターを作成することをお勧めします。これらのレイアウトには、ListView 独自の OnItemClickListener を使用してクリック イベントを処理するのではなく、クリック イベントを処理する ViewGroups を含める必要があります。おそらく、次の 2 つのレイアウトを使用できます。

  • 1 つの見出しレイアウト。これは基本的に、幅全体にわたる 1 つのレイアウトです。
  • 2 つのニュース アイテムを同じ重みで並べて表示する 1 つのマルチ アイテム レイアウト。

これは、カスタムの背景にセレクターの状態を提供し、個々の ViewGroups の OnClickListener を自分で処理する必要があることを意味しますが、さまざまな画面サイズのレイアウトを有効に活用することもできます。を使用<include>すると、たとえば、 または の 3 つのニュース項目を並べて表示することが考えられres/layout-landますres/layout-w720dp。おそらく、3 列の場合、画像付きの記事は 2 列に、画像のない記事は 1 列に、少し混同することもできます。

于 2013-11-24T19:49:25.843 に答える