listview中一行内有两个与游标一起使用的项目

时间:2013-12-10 14:42:49

标签: android listview android-listview

我已阅读现有问题,其中每行有两个列表项ListView with two items per row

问题类似,但区别在于我使用游标,在某些情况下我遇到项目位置的问题。我正在努力实现这个目标:

row1: [item1] [item2]

row2: [gone]

row3: [item3]

row5: [item4]

所以我在第一行内膨胀了两个列表项,然后我问(如果位置== 1),并且在里面我把row2的可见性消失了,因此它消失了,我实现了如上所述的情况。它在较新版本的android上运行良好,但在版本2.3及更低版本上会发生奇怪的事情。当我启动我的Activity时,当我把它放在去的时候,row2不会消失,而是每次调用bindView()时它都会开始用row1(有两个项目)来改变地方。它在我的屏幕上看起来像这样:

row1: [item1] [item2]
row2: [gone] (I see empty space on screen)

然后

row1: [gone] (I see empty space on top of the screen)
row2: [item1] [item2]

依旧......

如果我每行放回一个项目,那么每个版本都能正常运行。那么光标可能会与在newView()和bindView()函数中获取这些位置相混淆?

有谁知道为什么会这样?

编辑:这是我建议的光标适配器类:

public class MyCursorAdapter extends CursorAdapter {

    private boolean flag;
    private View headerView;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    // private int positionId;
    private int titleId;
    private int imageId;
    private int iconId;
    private int categoryId;
    private int dateId;
    // private int subcategoryId;
    // private int descriptionId;
    // private int linkId;
    int i = 0;
    int j = 0;

        public MyCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, 0);
        imageLoader = new ImageLoader(getActivity());
        inflater = LayoutInflater.from(context);

        headerView = null;
        flag = true;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        Log.i("bindView", "bindView");

        final ViewHolder holder = (ViewHolder) view.getTag();
        // Log.i("bind", "bind view: " + i);
        i++;
        switch (getItemViewType(cursor)) {

        case Constants.view_header:
 //             holder.title.setText(cursor.getString(titleId));
//              imageLoader.DisplayImage(cursor.getString(imageId),
//                      holder.image);
//              // downloader.download(cursor.getString(imageId),       holder.image);
//              break;

            HeaderViewHolder headerHolder = (HeaderViewHolder) holder;

            if (headerHolder == null) {
                headerHolder = (HeaderViewHolder) headerView.getTag();
            }               

            if (flag) {
                flag = !flag;
            headerHolder.leftViewHolder.title.setText(cursor.getString(titleId));
            imageLoader.DisplayImage(cursor.getString(imageId), headerHolder.leftViewHolder.image);
            }
            else {
                flag = !flag;
            headerHolder.rightViewHolder.title.setText(cursor.getString(titleId));
            imageLoader.DisplayImage(cursor.getString(imageId), headerHolder.rightViewHolder.image);
            }
            break;


        case Constants.view_headline:
            holder.title.setText(cursor.getString(titleId));
            holder.date.setText(cursor.getString(dateId));
            if (currentFragmentPosition == 0)
                holder.category.setText(cursor.getString(categoryId));
            imageLoader
                    .DisplayImage(cursor.getString(iconId), holder.image);
            // Log.i("image", cursor.getString(iconId));
            // downloader.download(cursor.getString(imageId), holder.image);
            break;

        case Constants.view_ad1:
            if (!hasRunAd1) {
                // mAdView1.loadAd();
                if (!isTabletDevice()) {
                    initBanner(mAdView1, Constants.CATEGORY_AD_1_ID);
                } else {
                    initBanner(mAdView1, Constants.CATEGORY_AD_1_TABLET_ID);
                }
                hasRunAd1 = true;
            }
            break;
        case Constants.view_ad2:
            if (!hasRunAd2) {
                // mAdView2.loadAd();
                if (!isTabletDevice()) {
                    initBanner(mAdView2, Constants.CATEGORY_AD_2_ID);
                } else {
                    initBanner(mAdView2, Constants.CATEGORY_AD_2_TABLET_ID);
                }
                hasRunAd2 = true;
            }
            break;
        case Constants.view_ad3:
            if (!hasRunAd3) {
                // mAdView3.loadAd();
                if (!isTabletDevice()) {
                    initBanner(mAdView3, Constants.CATEGORY_AD_3_ID);
                } else {
                    initBanner(mAdView3, Constants.CATEGORY_AD_3_TABLET_ID);
                }
                hasRunAd3 = true;
            }
            break;

        default:
            holder.title.setText(cursor.getString(titleId));
            holder.date.setText(cursor.getString(dateId));
            if (currentFragmentPosition == 0)
                holder.category.setText(cursor.getString(categoryId));
            imageLoader
                    .DisplayImage(cursor.getString(iconId), holder.image);
            // downloader.download(cursor.getString(imageId), holder.image);
            break;

        }

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        Log.i("newView", "newView");

        // Log.i("bind", "new view: " + j);
        j++;
        View row = null;

        switch (getItemViewType(cursor)) {

        case Constants.view_header:
//              row = inflater
//                      .inflate(R.layout.news_list_header, parent,     false);
//              TextView headerTitle = (TextView) row
//                      .findViewById(R.id.news_list_header_title);
//
//              row.setTag(new ViewHolder((ImageView) row
//                      .findViewById(R.id.news_list_header_image),     headerTitle));
//
//              if (currentFragmentPosition < Constants.headerColors.length)
//                  headerTitle
//                          .setBackgroundColor(Constants.headerColors[currentFragmentPosition]);
//              else
//                  headerTitle
//                          .setBackgroundColor(Constants.defaultHeaderColor);
//
//              break;

            if (headerView == null) {
            row = inflater.inflate(R.layout.news_list_header, parent, false);

            RelativeLayout leftLayout = (RelativeLayout) row.findViewById(R.id.news_list_header_layout_left);
            TextView leftTitle = (TextView) leftLayout.findViewById(R.id.news_list_header_title_left);
            ViewHolder leftHolder = new ViewHolder((ImageView)leftLayout.findViewById(R.id.news_list_header_image_left), leftTitle);

            RelativeLayout rightLayout = (RelativeLayout) row.findViewById(R.id.news_list_header_layout_right);
            TextView rightTitle = (TextView) rightLayout.findViewById(R.id.news_list_header_title_right);
            ViewHolder rightHolder = new ViewHolder((ImageView)rightLayout.findViewById(R.id.news_list_header_image_right), rightTitle);

            HeaderViewHolder holder = new HeaderViewHolder(leftHolder, rightHolder);

            row.setTag(holder);

            headerView = row;
            }
            else {
                row = new View(context);
                row.setVisibility(View.GONE);
            }
            break;

        case Constants.view_headline:
            row = inflater.inflate(R.layout.news_list_headline, parent,
                    false);
            row.setTag(new ViewHolder(
                    (ImageView) row
                            .findViewById(R.id.news_list_headline_image),
                    (TextView) row
                            .findViewById(R.id.news_list_headline_date),
                    (TextView) row
                            .findViewById(R.id.news_list_headline_category),
                    (TextView) row
                            .findViewById(R.id.news_list_headline_title)));

            RelativeLayout headlineLayout = (RelativeLayout) row
                    .findViewById(R.id.news_list_headline_layout);

            if (currentFragmentPosition < Constants.headlineColors.length)
                headlineLayout
                        .setBackgroundColor(Constants.headlineColors[currentFragmentPosition]);
            else
                headlineLayout
                        .setBackgroundColor(Constants.defaultHeadlineColor);

            break;

        case Constants.view_ad1:
            row = inflater.inflate(R.layout.news_list_ad_item, parent,
                    false);
            mAdView1 = (MoPubView) row.findViewById(R.id.adview);
            mAdView1.setAdUnitId(Constants.CATEGORY_AD_1_ID);

            // row=initBanner(mAdView1, Constants.CATEGORY_AD_1_ID);

            break;
        case Constants.view_ad2:
            row = inflater.inflate(R.layout.news_list_ad_item, parent,
                    false);
            mAdView2 = (MoPubView) row.findViewById(R.id.adview);
            mAdView2.setAdUnitId(Constants.CATEGORY_AD_2_ID);
            break;
        case Constants.view_ad3:
            row = inflater.inflate(R.layout.news_list_ad_item, parent,
                    false);
            mAdView3 = (MoPubView) row.findViewById(R.id.adview);
            mAdView3.setAdUnitId(Constants.CATEGORY_AD_3_ID);
            break;

        default:
            row = inflater.inflate(R.layout.news_list_item, parent, false);
            row.setTag(new ViewHolder((ImageView) row
                    .findViewById(R.id.news_list_item_image),
                    (TextView) row.findViewById(R.id.news_list_item_date),
                    (TextView) row
                            .findViewById(R.id.news_list_item_category),
                    (TextView) row.findViewById(R.id.news_list_item_title)));

            TextView colorTitle = (TextView) row
                    .findViewById(R.id.news_list_item_title);

            if (currentFragmentPosition < Constants.headlineColors.length)
                colorTitle
                        .setTextColor(Constants.headlineColors[currentFragmentPosition]);
            else
                colorTitle.setTextColor(Constants.defaultHeadlineColor);

            break;
        }

        return row;

    }

    private void initBanner(MoPubView banner, String id) {

        // View v = inflater.inflate(R.layout.news_list_ad_item, null,
        // false);
        // banner = (MoPubView) v.findViewById(R.id.adview);

        final MoPubView bannerPlace = banner;

        bannerPlace
                .setOnAdLoadedListener(new MoPubView.OnAdLoadedListener() {
                    public void OnAdLoaded(MoPubView paramMoPubView) {
                        bannerPlace.setVisibility(View.VISIBLE);

                    }
                });
        bannerPlace
                .setOnAdFailedListener(new MoPubView.OnAdFailedListener() {
                    public void OnAdFailed(MoPubView paramMoPubView) {
                        bannerPlace.setVisibility(View.GONE);
                    }
                });
        bannerPlace.setAdUnitId(id);
        bannerPlace.loadAd();

    }

    private boolean isTabletDevice() {
        if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
            // test screen size, use reflection because isLayoutSizeAtLeast
            // is
            // only available since 11
            Configuration con = getResources().getConfiguration();
            try {
                Method mIsLayoutSizeAtLeast = con.getClass().getMethod(
                        "isLayoutSizeAtLeast", int.class);
                Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con,
                        Configuration.SCREENLAYOUT_SIZE_LARGE); // Configuration.SCREENLAYOUT_SIZE_XLARGE
                return r;
            } catch (Exception x) {
                x.printStackTrace();
                return false;
            }
        }
        return false;
    }

    // helper for columns from database
    private void initColumns(final Cursor c) {
        if (c != null) {

            // linkId = c.getColumnIndex(SvetDatabase.COL_LINK);
            // positionId = c.getColumnIndex(SvetDatabase.COL_HEADPOS);
            titleId = c.getColumnIndex(SvetDatabase.COL_TITLE);
            imageId = c.getColumnIndex(SvetDatabase.COL_IMAGE);
            iconId = c.getColumnIndex(SvetDatabase.COL_ICON);
            categoryId = c.getColumnIndex(SvetDatabase.COL_CATEGORY);
            dateId = c.getColumnIndex(SvetDatabase.COL_STRINGDATE);
            // subcategoryId =
            // c.getColumnIndex(SvetDatabase.COL_SUBCATEGORY);
            // descriptionId =
            // c.getColumnIndex(SvetDatabase.COL_DESCRIPTION);

        }
    }

    @Override
    public Cursor swapCursor(Cursor newCursor) {
        initColumns(newCursor);
        return super.swapCursor(newCursor);
    }

    @Override
    public int getItemViewType(int position) {
        if (position != 0) {
            Cursor cursor = (Cursor) getItem(position);
            return getItemViewType(cursor);
        }
        return 0;
    }

    // number of view types
    @Override
    public int getViewTypeCount() {
        return 6;
    }

    // return view type
    private int getItemViewType(Cursor cursor) {
        return cursor.getInt(cursor
                .getColumnIndexOrThrow(SvetDatabase.COL_HEADPOS));
    }

}

谢谢!

0 个答案:

没有答案