-1

我用剖面视图创建网格视图并遇到一些问题

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        int size = mArray.get(0).size();
        int z = size + (COLUMNS_NUM * 2);
//sectionView 
        if (position < (COLUMNS_NUM * 2) || (position > (z -1) && position < (z + COLUMNS_NUM))) {

            if(position == COLUMNS_NUM ){
                return header(R.string.grid_last_added, convertView);
            }

            if(position == z){
                return header(R.string.grid_last_added, convertView);
            }

            if (convertView == null) {
                convertView = new View(mContext);
            }
            // Set empty view with height of ActionBar
            convertView.setLayoutParams(new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight));

            if(position == COLUMNS_NUM || position == z){
                convertView.setLayoutParams(new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight - 15));
            }

            return convertView;
        }

        View v = convertView;
        if(v == null){
            v = LayoutInflater.from(mContext).inflate(R.layout.player_movie, null);
            v.setLayoutParams(mImageViewLayoutParams);
        }

        TextView title = (TextView) v.findViewById(R.id.serial_title);
        TextView seria = (TextView) v.findViewById(R.id.when);
        ImageView poster = (ImageView) v.findViewById(R.id.poster);

        //Offten.debug(getCount(), position);

        int x = (position - (COLUMNS_NUM * 2) )< mArray.get(0).size() ? position - (COLUMNS_NUM * 2) : position  - (z + COLUMNS_NUM);
        int y = (position - (COLUMNS_NUM * 2) )< mArray.get(0).size() ? 0 : 1;

//        Offten.debug(x + " " + (position < mArray.get(0).size() ? "true" : "false") + " " + mArray.get(0).size());



        //Offten.debug( Boolean.toString((position - (COLUMNS_NUM * 2) )< mArray.get(0).size()) + " " + x + " " + z);



        Serial serial = mArray.get(y).get(x);

        title.setText(serial.getTitle());
        seria.setText(serial.getSeria());
        mFetcher.loadImage( serial.getPoster(), poster);

        if(v.getLayoutParams().height != mItemHeight){
            v.setLayoutParams(mImageViewLayoutParams);
        }
        return v;
    }

    public TextView header(int message, View convertView){
        TextView view = (TextView) convertView; // issue with cast exception
        if(view == null){
            view = new TextView(mContext);
        }
        view.setText(underline(mContext.getResources().getString(message)));
        view.setTextSize(18);
        view.setTypeface(typeface, Typeface.BOLD_ITALIC);
        view.setTextColor(Color.WHITE);
        view.setGravity(Gravity.CENTER_VERTICAL);
        view.setLayoutParams(new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight - 15));
        return  view;
    }

使用此代码,我的标题 TextView 中的强制转换异常总是存在问题。(视图不能转换为 TextView)

@Override
public int getViewTypeCount() {
    // Two types of views, the normal ImageView and the top row of empty views
    return 2;
}

@Override
public int getCount() {
    int size = 0;
    for(List<Serial> s : mArray){
        size += s.size() + COLUMNS_NUM;
    }

    return size > 0 ? size + COLUMNS_NUM : size;
}

@Override
public int getItemViewType(int position) {

    int size = mArray.get(0).size();
    int z = size + (COLUMNS_NUM * 2);

    return position < (COLUMNS_NUM * 2) || (position > (z -1) && position < (z + COLUMNS_NUM)) ? 1 : 0;
}

我能解决我的问题吗??

4

2 回答 2

0

Do like this

public TextView header(int message, View convertView){
        TextView view = (TextView) convertView.findViewById(R.id.<textviewId>); // issue with cast exception
        if(view == null){
            view = new TextView(mContext);
        }
        view.setText(underline(mContext.getResources().getString(message)));
        view.setTextSize(18);
        view.setTypeface(typeface, Typeface.BOLD_ITALIC);
        view.setTextColor(Color.WHITE);
        view.setGravity(Gravity.CENTER_VERTICAL);
        view.setLayoutParams(new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight - 15));
        return  view;
    }
于 2013-08-24T06:22:26.307 回答
-1

所以我找到了我的问题的答案!我有 3 个不同的观点,但我只声明了 2 个,而且我遇到了问题!感谢大家的帮助!

@Override
public int getItemViewType(int position) {

    int size = mArray.get(0).size();
    int z = size + (COLUMNS_NUM * 2);

    if (position < (COLUMNS_NUM * 2) || (position > (z -1) && position < (z + COLUMNS_NUM))) {

        if(position == COLUMNS_NUM || position == z){
            return 1;
        }

        return 2;
    }

    return 0;
}

    @Override
    public int getViewTypeCount() {
        // Two types of views, the normal ImageView and the top row of empty views
        return 3;
    }
于 2013-08-24T06:54:33.207 回答