滚动时android网格视图中缺少项目

时间:2015-10-12 12:02:09

标签: android android-gridview android-scroll

我有网格视图,每个项目包含少量图像和文本。当它第一次加载时,一切都很好,但如果我滚动到底部并返回顶部,一些项目中的两个图像就会消失,并且它会随机出现。

这是Grid视图的代码

  <GridView
        android:id="@+id/testR_grid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:clickable="true"
        android:columnWidth="@dimen/gridview_column_width"
        android:horizontalSpacing="@dimen/grid_horizontal_spacing"
        android:listSelector="@drawable/gridview_background"
        android:numColumns="auto_fit"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="vertical"
        android:verticalScrollbarPosition="right"
        android:verticalSpacing="@dimen/grid_vertical_spacing"
        android:fastScrollEnabled="false"/>

这里是单项

的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_marginTop="5dp"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/blurry_shadow_rect">

        <com.joooonho.SelectableRoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/item_image"
            android:layout_width="match_parent"
            android:layout_height="@dimen/gridview_column_width"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="-5dp"

            app:sriv_left_bottom_corner_radius="0dip"
            app:sriv_left_top_corner_radius="4dip"
            app:sriv_right_bottom_corner_radius="0dip"
            app:sriv_right_top_corner_radius="4dip" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/item_image"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="2dp"
            android:orientation="horizontal"
            android:weightSum="100">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="35">

                <TextView
                    android:id="@+id/item_text"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_gravity="start"
                    android:layout_marginBottom="5dp"
                    android:ellipsize="end"
                    android:fontFamily="sans-serif-light"
                    android:maxLines="1"
                    android:text=""
                    android:textColor="@color/bodyText"
                    android:textSize="@dimen/GridHeader" />

                <TextView
                    android:id="@+id/item_TestA_name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/item_text"
                    android:layout_marginBottom="10dp"
                    android:alpha=".5"
                    android:ellipsize="end"
                    android:maxLines="1"
                    android:text=""
                    android:textColor="@color/bodyText"
                    android:textSize="@dimen/GridHeader2" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight="65">

                <TextView
                    android:id="@+id/item_testR_free_text"
                    style="@style/textview_grid_free_banner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:fontFamily="sans-serif-light"
                    android:maxLines="1"
                    android:padding="2dp"
                    android:singleLine="true"
                    android:text=" FREE " />


                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:id="@+id/already_owned"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:singleLine="true"
                    android:layout_marginLeft="20dp"
                    android:layout_marginBottom="5dp"
                    android:visibility="gone"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/ic_testR_bought_36dp" />

                <ImageButton
                    android:id="@+id/item_testR_more_options"
                    android:layout_width="@dimen/image_button_more"
                    android:layout_height="@dimen/image_button_more"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/item_testR_free_text"
                    android:alpha=".5"
                    android:background="@drawable/imagebutton_background"
                    android:clickable="true"
                    android:focusable="false"

                    android:src="@drawable/ic_more_vert_black_24dp" />
            </RelativeLayout>
        </LinearLayout>

    </RelativeLayout>


</RelativeLayout>

元素随机丢失的是ID为item_testR_free_text的文本视图和ID为is already_owned的imageview。还有一点需要注意,这两个项目的可见性是有条件的。

这是我的适配器代码

public class TestRGridViewAdapter extends ArrayAdapter<TestR> {
    Context context;
    int layoutResourceId;
    List<TestR> data = new ArrayList<TestR>();
    RecordHolder holder = null;
    //DiskCache imgCache;
    TestA TestA;
    TestR item;
    FragmentActivity mFragmentActivity;
    boolean is_local = false;
    List<Track> trackList;

    public TestR getItem() {
        return item;
    }

    public void setItem(TestR item) {
        this.item = item;
    }

    public TestRGridViewAdapter(Context context, int layoutResourceId,
                                  List<TestR> data, FragmentActivity mFragmentActivity, boolean is_local) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        this.mFragmentActivity = mFragmentActivity;
//        imgCache = Parameters.imgCache;
        this.is_local = is_local;
    }

    public TestRGridViewAdapter(Context context, int layoutResourceId,
                                  List<TestR> data, FragmentActivity mFragmentActivity) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        this.mFragmentActivity = mFragmentActivity;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            //LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = LayoutInflater.from(context).inflate(layoutResourceId, null);
            //row = inflater.inflate(layoutResourceId, parent, false);
            holder = new RecordHolder();
            holder.item_testR_free_text = (TextView) row.findViewById(R.id.item_testR_free_text);
            holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
            holder.txtTestAName = (TextView) row.findViewById(R.id.item_TestA_name);
            holder.imageItem = (SelectableRoundedImageView) row.findViewById(R.id.item_image);
            holder.item_testR_more_options = (ImageButton) row.findViewById(R.id.item_testR_more_options);
            holder.already_owned = (ImageView) row.findViewById(R.id.already_owned);
//            holder.grid_swipe_refresh_layout = (SwipeRefreshLayout) row.findViewById(R.id.grid_swipe_refresh_layout);

            row.setTag(holder);
        } else {
            holder = (RecordHolder) row.getTag();
        }
        item = data.get(position);

        holder.txtTitle.setText(item.getName());

//        else
//            holder.txtArtitName.setText("");
        //holder.imageItem.setImageBitmap(item.getImage());
        try {
            if (item.getTestA_id() == null)
                item = new SaveData(context).get_online_testR(item.getTestR_id());
            TestA = Helper.getDaoSession(context).getTestADao().load(item.getTestA_id());
            if (TestA == null) {
                TestA = new SaveData(context).get_save_TestA(item.getTestA_id());
            }
//            if (TestA != null)
            holder.txtTestAName.setText((TestA != null) ? TestA.getName() : "");
//            else
//                TestA = new SaveData(context).get_save_TestA(item.getTestA_id());


            if (!is_local) {

                if(!DBQuery.is_owner(context,item.getTestR_id())) {

                    Log.d("test","not owner - "+item.getName());

                    holder.item_testR_free_text.setText(
                            UXHelper.getPriceFromString(Parameters.price_type.equals("local_price") ?
                                    item.getLocal_price().toString() : item.getPrice().toString()));
                    holder.already_owned.setVisibility(View.INVISIBLE);

                    holder.item_testR_more_options.setOnClickListener(new testR_popup_onClick(context, item, holder.item_testR_more_options, mFragmentActivity, TestA));
                }else {
                    Log.d("test","owner - "+item.getName());
                    holder.item_testR_free_text.setVisibility(View.INVISIBLE);
                    holder.already_owned.setVisibility(View.VISIBLE);
                    holder.item_testR_more_options.setOnClickListener(new testR_popup_onClick(context, item, holder.item_testR_more_options, mFragmentActivity, TestA));
                }

            } else {
                Log.d("test","local - "+item.getName());
                holder.already_owned.setVisibility(View.INVISIBLE);
                boolean synced = true;

                trackList = new ArrayList<Track>();
                List<Track> trackListTmp = new SaveData(context).get_tracks_local(item.getTestR_id());
                boolean testR_owner = DBQuery.is_owner(context, trackListTmp.get(0).getTestR_id());
                for (Track t : trackListTmp) {
                    boolean track_owner = DBQuery.is_owner(context, t.getTrack_id(), t.getTestR_id());
                    if (track_owner)
                        trackList.add(t);
                    if (testR_owner || track_owner) {
                        if (t.getSynced_dir() == null) {
                            synced = false;
//                        break;
                        } else if (!new File(t.getSynced_dir()).exists()) {
                            synced = false;
//                        break;
                        }
                    }

                }

                if (synced) {
                    Log.d("test","synched - "+item.getName());
                    holder.item_testR_free_text.setVisibility(View.VISIBLE);
                    holder.item_testR_free_text.setText("Synced");
                } else {
                    Log.d("test","not synched - "+item.getName());
                    holder.item_testR_free_text.setVisibility(View.INVISIBLE);
                    holder.item_testR_more_options.setOnClickListener(
                            new testR_popup_downloaded_onCLick(context, item, holder.item_testR_more_options, mFragmentActivity, TestA,
                                    trackList.toArray(new Track[trackList.size()]), synced));
                }
            }
            Target target = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    holder.imageItem.setImageBitmap(bitmap);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {
                    holder.imageItem.setImageResource(R.drawable.ic_example_36);
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                    holder.imageItem.setImageResource(R.drawable.ic_example_36);
                }
            };


            if (Parameters.mPicasso == null)
                Parameters.mPicasso = new Picasso.Builder(context)

                        .build();

            Parameters.mPicasso.load("http://" + context.getString(R.string.ip) + "/" +
                    context.getString(R.string.TestRsController) + "/" + context.getString(R.string.TESTR_THUMB_URL)
                    + "/" + item.getTestR_id()+"/"+Parameters.dpi)


                    .placeholder(R.drawable.ic_example_36)
                    .error(R.drawable.ic_example_36)

                    .into(holder.imageItem);

        } catch (Exception ex) {
            new Logger(context).appendLog("Error in TestR grid view item " + ex.getMessage());
        }
        return row;

    }

    static class RecordHolder {
        //        SwipeRefreshLayout grid_swipe_refresh_layout;
        TextView txtTitle;
        TextView txtTestAName;
        SelectableRoundedImageView imageItem;
        TextView item_testR_free_text;
        ImageButton item_testR_more_options;
        ImageView already_owned;
    }
}

0 个答案:

没有答案