我如何调用以表格形式列出连续2个项目相邻的?表格、形式、项目

2023-09-07 23:07:25 作者:一路风景

基本上我有含新闻标题,使得第一标题覆盖顶端部,备用标题的第二部分和第三标题的第三部分,并在第二和第三是连续的列表中(第一遗体作为一个列表视图在列表中唯一的标题)。我已经编程定义它,如下所示:

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 codeS分别在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:

我现在想更改为:

which I now want to change to:

每个小区是可选择的,所以需要使它使得对于所述第二和第三标题置于彼此相邻(而不改变第一标题)的每个是单独选择。我该如何去创造一个布局和改变我目前的java $ C $下是相同的(请注意:我想OT改变code只是为平板电脑而不是手机,所以如果有人这将是真棒可以提出一个方法,其中编辑XML的布局将是足够的,所以我可以创建布局片分别在不影响手机code)。谢谢!

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!

推荐答案

我不同意在这里所有的办法,似乎更繁琐的比它应该是。 试试这个适配器:

I disagree with all the approaches on here, seem more tedious than it should be. Try this adapter:

public class MultipleRowTypeList extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ListView list = new ListView(this);
            setContentView(list);
            // creating dummy data for testing
            MatrixCursor mc = new MatrixCursor(new String[] { "_id", "title",
                            "content", "image" });
            for (int i = 0; i < 40; i++) {
                    mc.addRow(new Object[] {
                                    i,
                                    "Item no." + i,
                                    "Lorem Ipsum is simply dummy text of the printing "
                                                    + "and typesetting industry. Lorem Ipsum has been the industry's"
                                                    + " standard dummy text ever since the 1500s, when an unknown"
                                                    + " printer took a galley of type and scrambled it to make a"
                                                    + " type specimen book. It has survived not only five centuries,"
                                                    + " but also the leap into electronic typesetting, remaining essentially unchanged.",
                                    R.drawable.ic_launcher });
            }
            list.setAdapter(new ForTabletAdapter(this, mc, list));
            list.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> list, View view,
                                    int position, long id) {
                            // the listener could be used for both phone and tablet as the
                            // position will be the position from the list of data
                            Toast.makeText(getApplicationContext(),
                                            "Selected item " + position, Toast.LENGTH_SHORT).show();
                    }
            });
            // for simplicity
            list.setSelector(android.R.drawable.list_selector_background);
    }

    /**
     * This would be the adapter used for tablet target layouts.
     */
    private static class ForTabletAdapter extends CursorAdapter {

            private static final int NUM_TYPES = 3;

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

            private LayoutInflater mInflater;
            private ListView mListHandle;

            ForTabletAdapter(Context context, Cursor cursor, ListView list) {
                    super(context, cursor, 0);
                    mInflater = LayoutInflater.from(context);
                    mListHandle = list;
            }

            @Override
            public int getCount() {
                    // calculate the real number of rows taking in consideration that
                    // there will be two items per row starting with the second row of
                    // the ListView
                    int size = getCursor().getCount();
                    if (size == 0) {
                            return size;
                    } else {
                            int count = 1;
                            size = size - 1;
                            count += size / 2; // two elements per row
                            int remainder = size % 2;
                            if (remainder != 0) { // check if we have a stranded element
                                    count++;
                            }
                            return count;
                    }
            }

            @Override
            public Object getItem(int position) {
                    // we can't use it because we can't return two values for the same
                    // position
                    return null;
            }

            @Override
            public long getItemId(int position) {
                    // we can't use it because we can't return two values for the same
                    // position
                    return -1;
            }

            /**
             * Populate our two items per row normal row.
             * 
             * @param convertView
             *            the ENTIRE row view(containing at maximum two items)
             * @param holder
             *            the holder with the references to the row views
             * @param position
             *            the position in the full list, this would need to be
             *            offset to map to the proper position in the mData
             */
            private void populateAleternativeRows(ViewGroup convertView,
                            Holder holder, Cursor cursor) {
                    // offset the position to match the real position
                    final int position = 2 * cursor.getPosition() - 1;
                    // move the cursor to the right position
                    cursor.moveToPosition(position);
                    holder.titles[0].setText(cursor.getString(cursor
                                    .getColumnIndex("title")));
                    holder.contents[0].setText(cursor.getString(cursor
                                    .getColumnIndex("content")));
                    holder.images[0].setImageResource(cursor.getInt(cursor
                                    .getColumnIndex("image")));
                    // set as tag the position so we can forward the proper event data
                    convertView.getChildAt(0).setTag(Integer.valueOf(position));
                    convertView.getChildAt(0).setOnClickListener(mClickDispatcher);
                    // check if we have two items per row, this might not happen at the
                    // end of the list if we have an even number of items in the Cursor
                    int nextPosition = position + 1;
                    // if we are within the proper bounds
                    if (cursor.getPosition() < cursor.getCount() - 1) {
                            cursor.moveToNext();
                            holder.titles[1].setText(cursor.getString(cursor
                                            .getColumnIndex("title")));
                            holder.contents[1].setText(cursor.getString(cursor
                                            .getColumnIndex("content")));
                            holder.images[1].setImageResource(cursor.getInt(cursor
                                            .getColumnIndex("image")));
                            convertView.getChildAt(1).setVisibility(View.VISIBLE); 
                    } else {
                            // we need to hide this if an item is not available so we don't
                            // end up with ghost elements on the last row
                            convertView.getChildAt(1).setVisibility(View.INVISIBLE);
                            return;
                    }
                    convertView.getChildAt(1).setTag(Integer.valueOf(nextPosition));
                    convertView.getChildAt(1).setOnClickListener(mClickDispatcher);
            }

            /**
             * This listener will dispatch the events from one of the two possible
             * items in a normal row to the actual OnItemCLickListener of the
             * ListView to have a unified listener for the entire list.
             */
            private OnClickListener mClickDispatcher = new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                            Integer position = (Integer) view.getTag();
                            mListHandle.getOnItemClickListener().onItemClick(mListHandle,
                                            view, position, -1);
                    }

            };

            // as the first row is a normal row we make it enabled, any other row
            // will be disabled and it will handle the click event on its own and
            // forward it to ListView's OnItemClickListener
            @Override
            public boolean isEnabled(int position) {
                    return position == 0;
            }

            /**
             * Holder class. As we have two items per row, we have an array of two
             * elements in the holder for the views(the first row will only use the
             * first element)
             * 
             */
            private static class Holder {
                    TextView[] titles = new TextView[2];
                    TextView[] contents = new TextView[2];;
                    ImageView[] images = new ImageView[2];
            }

            @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 void bindView(View rowView, Context context, Cursor cursor) {
                    final int type = getItemViewType(cursor.getPosition());
                    Holder holder = (Holder) rowView.getTag();
                    switch (type) {
                    case Types.FIRST_HEADLINE:
                            holder.titles[0].setText(cursor.getString(cursor
                                            .getColumnIndex("title")));
                            holder.contents[0].setText(cursor.getString(cursor
                                            .getColumnIndex("content")));
                            holder.images[0].setImageResource(cursor.getInt(cursor
                                            .getColumnIndex("image")));
                            break;
                    case Types.ALTERNATE_HEADLINE:
                            populateAleternativeRows((ViewGroup) rowView, holder, cursor);
                            break;
                    case Types.OTHER_HEADLINE:
                            populateAleternativeRows((ViewGroup) rowView, holder, cursor);
                            break;
                    default:
                            throw new IllegalStateException("Unknow type of row!");
                    }

            }

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                    Holder holder = new Holder();
                    View rowView = null;
                    final int type = getItemViewType(cursor.getPosition());
                    switch (type) {
                    case Types.FIRST_HEADLINE:
                            rowView = mInflater.inflate(R.layout.item_news_first_headline,
                                            parent, false);
                            holder.titles[0] = (TextView) rowView.findViewById(R.id.news_headline_text);
                            holder.contents[0] = (TextView) rowView
                                            .findViewById(R.id.metadata);
                            holder.images[0] = (ImageView) rowView.findViewById(R.id.news_headline_image);
                            break;
                    case Types.ALTERNATE_HEADLINE:
                            rowView = mInflater.inflate(R.layout.item_news_alternate_headline,
                                            parent, false);
                            holder.titles[0] = (TextView) rowView.findViewById(R.id.news_headline_text);
                            holder.contents[0] = (TextView) ((ViewGroup) rowView)
                                            .getChildAt(0).findViewById(R.id.metadata);
                            holder.images[0] = (ImageView) ((ViewGroup) rowView)
                                            .getChildAt(0).findViewById(R.id.news_headline_image);
                            holder.titles[1] = (TextView) ((ViewGroup) rowView).getChildAt(
                                            1).findViewById(R.id.news_headline_text);
                            holder.contents[1] = (TextView) ((ViewGroup) rowView)
                                            .getChildAt(1).findViewById(R.id.metadata);
                            holder.images[1] = (ImageView) ((ViewGroup) rowView)
                                            .getChildAt(1).findViewById(R.id.news_headline_image);
                            break;
                    case Types.OTHER_HEADLINE:
                            rowView = mInflater.inflate(R.layout.item_news_headline,
                                            parent, false);
                            holder.titles[0] = (TextView) rowView.findViewById(R.id.news_headline_text);
                            holder.contents[0] = (TextView) ((ViewGroup) rowView)
                                            .getChildAt(0).findViewById(R.id.metadata);
                            holder.images[0] = (ImageView) ((ViewGroup) rowView)
                                            .getChildAt(0).findViewById(R.id.news_headline_image);
                            holder.titles[1] = (TextView) ((ViewGroup) rowView).getChildAt(
                                            1).findViewById(R.id.news_headline_text);
                            holder.contents[1] = (TextView) ((ViewGroup) rowView)
                                            .getChildAt(1).findViewById(R.id.metadata);
                            holder.images[1] = (ImageView) ((ViewGroup) rowView)
                                            .getChildAt(1).findViewById(R.id.news_headline_image);
                            break;
                    default:
                            throw new IllegalStateException("Unknow type of row!");
                    }
                    rowView.setTag(holder);
                    return rowView;
            }
    }
}

此外,还要确保你能使你的布局文件到上面的类进行必要的修改(注:我呼吁各个类别和标签你)。只要改变布局文件,它的布局方式(简单的XML的变化),这将是所有good.Let我知道,如果你有任何问题,

Also, make sure you make the necessary changes in your layout file to the above class (note: i have called the respective classes and tags for you). Just make the changes in the layout files and how it is laid out (simple xml changes) and it will be all good.Let me know if you have any issues