SwipeRefreshLayout中的Listview - 当自定义适配器只显示一行时,不会显示任何内容

时间:2014-10-28 20:40:54

标签: android listview android-listview baseadapter custom-adapter

enter image description here

在我的应用中,我有一个列表视图。当我有多个项目展示时,所有的东西都能很好地工作,但是当我在列表视图中显示一个项目时,它会向我展示一个罕见的行为。

首先:如果单个项目的高度大于窗口高度......它就像一个魅力并且正常工作。(A)

第二:如果单个物品的高度小于窗户高度......有时它显示项目(B),有时则不显示! (C)。

第三:我正在使用SwipeRefreshLayout来自支持库,当我尝试刷新listview时,如果我在listview中有一个项目...如果它的高度小于窗口高度,它不会显示该项目。(C)< / p>

我无法弄明白!!任何人都可以帮助我

我知道这个问题与SwipeRefreshLayout或Listview的高度有关,因为当我将marginTop设置为此视图(作为屏幕高度的一半)时,listview可以正常工作。

我的xml文件是:

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="#FFedf1f2"
android:paddingLeft="8.5dp"
android:paddingRight="8.5dp"
android:paddingTop="0dp" >

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:divider="@android:color/transparent"
    android:dividerHeight="8.5dp"
    android:scrollbars="none" >
</ListView>

一切都像魅力一样,直到我在listview中显示至少多一个项目。但是当我在列表视图中显示一个项目时,我的列表视图没有显示任何内容:(

为什么呢?我的错在哪里?我哪里错了?

这是我的适配器的getView()(扩展的BaseAdapter - 我已经删除了一些代码行):

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

    Typeface MyriadRegular  = Farsi.GetMyriadRegular(mActivity);
    Typeface MyriadBold     = Farsi.GetMyriadBold(mActivity);
    Typeface Yagut          = Farsi.GetYagut(mActivity);
    Typeface JalalBold      = Farsi.GetJalalBold(mActivity);

    User_Session            = new SessionManagement(mActivity);

    View row    = convertView;
    ViewHolder holder;

    mVideo              = mData.get(position);
    //======================================================================== USER
    USERNAME            = mVideo.get("USERNAME").toUpperCase();
    CHANNEL_NAME        = mVideo.get("CHANNEL_NAME");
    CHANNEL_ID          = mVideo.get("CHANNEL_ID");
    USER_ID             = mVideo.get("USER_ID");
    AVATAR              = mVideo.get("AVATAR");
    //======================================================================== VIDEO
    VIDEO_URL           = mVideo.get("VIDEO_URL");
    TITLE               = mVideo.get("TITLE");
    DESCRIPTION         = mVideo.get("DESCRIPTION");
    BIG_IMAGE           = mVideo.get("BIG_IMAGE");
    VIEW_CNT            = mVideo.get("VIEW_CNT");
    COMMENT_CNT         = mVideo.get("COMMENT_CNT");
    LIKE_CNT            = mVideo.get("LIKE_CNT");
    LIKE_COUNT          = Integer.parseInt(LIKE_CNT);
    DISLIKE_CNT         = mVideo.get("DISLIKE_CNT");
    DISLIKE_COUNT       = Integer.parseInt(DISLIKE_CNT);
    POST_DATE           = mVideo.get("POST_DATE");
    DURATION            = mVideo.get("DURATION");
    CATEGORY_TITLE      = mVideo.get("CATEGORY_TITLE");
    CATEGORY_ID         = mVideo.get("CATEGORY_ID");
    VIDEO_ID            = mVideo.get("VIDEO_ID");
    VIDEO_WEB           = mVideo.get("VIDEO_WEB");
    LIKE_W_USER         = mVideo.get("LIKE_W_USER");
    DISLIKE_W_USER      = mVideo.get("DISLIKE_W_USER");
    Follow_B_USER       = mVideo.get("FOLLOW_B_USER");
    Like_with_user      = Boolean.parseBoolean(LIKE_W_USER);
    Dislike_with_user   = Boolean.parseBoolean(DISLIKE_W_USER);
    FOLLOW_BY_USER      = Boolean.parseBoolean(Follow_B_USER);


    if(convertView == null) {
        row     = mInflater.inflate(R.layout.layout_list_row, parent, false);
        holder  = new ViewHolder(row);
        row.setTag(holder);

    }else{
        row     = convertView;
        holder  = (ViewHolder) row.getTag();
    }

    holder.user_Avatar.setTag(USER_ID);
    holder.user_Name.setTag(USER_ID);

    holder.user_Avatar.setOnClickListener(ChannelProfile);
    holder.user_Name.setOnClickListener(ChannelProfile);

    holder.video_Comment.setTag(position);

    holder.video_Like.setTag(R.string.Position, position);
    holder.video_Like.setTag(R.string.Video_ID, VIDEO_ID);

    holder.video_Dislike.setTag(R.string.Position, position);
    holder.video_Dislike.setTag(R.string.Video_ID, VIDEO_ID);

    holder.video_Edit.setTag(position);
    holder.video_Delete.setTag(position);

    holder.video_editText.setTypeface(JalalBold);
    holder.video_deleteText.setTypeface(JalalBold);


    if (!mNext.equals("NONE")){
        if (position >= getCount() - 1){
            if(mSwipe != null){
                mSwipe.setRefreshing(true);
            }

            loadmore();
        }
    }



    holder.video_Duration.setTypeface(MyriadBold);
    holder.video_Duration.setText(Farsi.Convert(DURATION));

    holder.video_Title.setTypeface(Yagut);
    holder.video_Title.setText(TITLE);

    holder.user_Name.setTypeface(MyriadRegular);
    holder.user_Name.setText(CHANNEL_NAME);

    holder.video_Description.setTypeface(MyriadRegular);
    holder.video_Description.setText(DESCRIPTION);

    holder.video_Description.setClickable(true);

    holder.video_CategoryTitle.setTypeface(JalalBold);
    holder.video_CategoryTitle.setText(CATEGORY_TITLE);

    holder.video_ViewCount.setTypeface(JalalBold);
    holder.video_ViewCount.setText(VIEW_CNT);

    holder.video_CommentCount.setTypeface(JalalBold);



    return row;
}

Edite:这是我的viewHolder类:

静态类ViewHolder {

    TextView            video_Duration;
    TextView            video_Title;
    ExpandableTextView  video_Description;
    TextView            video_CategoryTitle;
    TextView            video_ViewCount;
    TextView            video_PostDate;
    TextView            video_LikeCount;
    TextView            video_CommentCount;
    TextView            video_DislikeCount;
    TextView            video_editText;
    TextView            video_deleteText;
    ImageView           video_BigImage;
    Button              video_Like;
    Button              video_Dislike;
    Button              video_Comment;
    RelativeLayout      video_options;
    RelativeLayout      video_Edit;
    RelativeLayout      video_Delete;

    //=================================
    TextView            user_Name;
    ImageView           user_Avatar;

    public ViewHolder(View vi) {
        video_Duration      = (TextView) vi.findViewById(R.id.video_Duration);
        video_Title         = (TextView) vi.findViewById(R.id.video_Title);
        video_Description   = (ExpandableTextView) vi.findViewById(R.id.video_Description);
        video_CategoryTitle = (TextView) vi.findViewById(R.id.video_Category_Title);
        video_ViewCount     = (TextView) vi.findViewById(R.id.video_View_Count);
        video_PostDate      = (TextView) vi.findViewById(R.id.video_Post_Date);
        video_LikeCount     = (TextView) vi.findViewById(R.id.video_like_cnt);
        video_CommentCount  = (TextView) vi.findViewById(R.id.video_Comment_cnt);
        video_DislikeCount  = (TextView) vi.findViewById(R.id.video_dislike_cnt);
        video_editText      = (TextView) vi.findViewById(R.id.edit_text);
        video_deleteText    = (TextView) vi.findViewById(R.id.delete_text);
        video_BigImage      = (ImageView) vi.findViewById(R.id.video_Image);
        video_Like          = (Button) vi.findViewById(R.id.video_Like);
        video_Dislike       = (Button) vi.findViewById(R.id.video_disLike);
        video_Comment       = (Button) vi.findViewById(R.id.video_Comment);
        video_options       = (RelativeLayout) vi.findViewById(R.id.videoOptions);
        video_Edit          = (RelativeLayout) vi.findViewById(R.id.videoEditBtn);
        video_Delete        = (RelativeLayout) vi.findViewById(R.id.videoDeleteBtn);
        user_Name           = (TextView) vi.findViewById(R.id.username);
        user_Avatar         = (ImageView) vi.findViewById(R.id.user_avatar);


    }
}

JSON解析部分(我正在使用ION Library进行异步网络):

    private void initSearch(String tag, boolean refresh){

    final boolean ReforNormal = refresh;

    URL = Constants.Search_URL+tag;

    if(User_Session.isLoggedIn()){
        URL = Constants.Search_URL+tag+"&token="+User_TOKEN;
    }

    System.out.println("URL: "+URL);

    if (VideoLoad != null && !VideoLoad.isDone() && !VideoLoad.isCancelled()) return;

    VideoLoad   = Ion.with(this).load(URL).asJsonObject().setCallback(new FutureCallback<JsonObject>() {

        @Override
        public void onCompleted(Exception e, JsonObject result) {

            if (ReforNormal == true){
                mAdapter.clearData();
            }

            if ( e == null){

                System.out.println("RESULT: "+result);
                Videos  = result.getAsJsonArray("objects");
                if(Videos.size() > 0){

                    for (int i = 0; i < Videos.size(); i++) {

                        JsonObject Item = Videos.get(i).getAsJsonObject();
                        //======================================================================== USER
                        USERNAME        = Item.get("username").getAsString();
                        CHANNEL_NAME    = Item.get("channel_name").getAsString();
                        CHANNEL_ID      = Item.get("channel_id").getAsString();
                        USER_ID         = Item.get("user_id").getAsString();
                        AVATAR          = Item.get("avatar").getAsString();
                        //======================================================================== VIDEO
                        VIDEO_URL       = Item.get("video_url").getAsString();
                        TITLE           = Item.get("title").getAsString();
                        DESCRIPTION     = Item.get("text").getAsString();
                        BIG_IMAGE       = Item.get("image").getAsString();
                        VIEW_CNT        = Item.get("cnt_view").getAsString();
                        COMMENT_CNT     = Item.get("cnt_comment").getAsString();
                        LIKE_CNT        = Item.get("cnt_like").getAsString();
                        DISLIKE_CNT     = Item.get("cnt_dislike").getAsString();
                        POST_DATE       = Item.get("create_time").getAsString();
                        DURATION        = Item.get("duration").getAsString();
                        CATEGORY_TITLE  = Item.get("category_title").getAsString();
                        CATEGORY_ID     = Item.get("category_id").toString();
                        VIDEO_ID        = Item.get("id").getAsString();
                        VIDEO_WEB       = Item.get("web_url").getAsString();
                        LIKE_W_USER     = Item.get("like_with_user").getAsString();
                        DISLIKE_W_USER  = Item.get("dislike_with_user").getAsString();
                        Follow_B_USER   = Item.get("follow_by_user").getAsString();

                        HashMap<String, String> Items = new HashMap<String, String>();
                        Items.put("USERNAME", USERNAME);
                        Items.put("CHANNEL_NAME", CHANNEL_NAME);
                        Items.put("CHANNEL_ID", CHANNEL_ID);
                        Items.put("USER_ID", USER_ID);
                        Items.put("AVATAR", AVATAR);
                        Items.put("VIDEO_URL", VIDEO_URL);
                        Items.put("TITLE", TITLE);
                        Items.put("DESCRIPTION", DESCRIPTION);
                        Items.put("BIG_IMAGE", BIG_IMAGE);
                        Items.put("VIEW_CNT", VIEW_CNT);
                        Items.put("COMMENT_CNT", COMMENT_CNT);
                        Items.put("LIKE_CNT", LIKE_CNT);
                        Items.put("DISLIKE_CNT", DISLIKE_CNT);
                        Items.put("POST_DATE", POST_DATE);
                        Items.put("DURATION", DURATION);
                        Items.put("CATEGORY_TITLE", CATEGORY_TITLE);
                        Items.put("CATEGORY_ID", CATEGORY_ID);
                        Items.put("VIDEO_ID", VIDEO_ID);
                        Items.put("VIDEO_WEB", VIDEO_WEB);
                        Items.put("LIKE_W_USER", LIKE_W_USER);
                        Items.put("DISLIKE_W_USER", DISLIKE_W_USER);
                        Items.put("Follow_B_USER", Follow_B_USER);

                        DATA.add(Items);
                    }

                    Page            = result.getAsJsonObject("meta");
                    if(User_Session.isLoggedIn()){
                        HashMap<String, String> user    = User_Session.GetUserDetails();
                        String User_TOKEN               = user.get(SessionManagement.KEY_USERTOKEN);

                        if(Page.equals("")){
                            NextPage    = "NONE";
                        }else{
                            NextPage    = Page.get("next").getAsString()+"&token="+User_TOKEN;
                        }

                    }else{

                        if(Page.equals("")){
                            NextPage    = "NONE";
                        }else{
                            NextPage    = Page.get("next").getAsString();
                        }
                    }


                    mAdapter = new MyCustomAdapter(SearchActivity.this, DATA, NextPage, ParsvidswipeLayout, false, false, "");
                    list.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            ParsvidswipeLayout.setRefreshing(false);
                        }
                    }, 500);

                }else{
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            ParsvidswipeLayout.setRefreshing(false);
                        }
                    }, 500);
                }


            }
        }
    });
}

1 个答案:

答案 0 :(得分:3)

问题在于您每次创建new Adapter而不是向Adapter

添加数据
mAdapter = new MyCustomAdapter(SearchActivity.this, DATA, NextPage, ParsvidswipeLayout, false, false, ""); 
                    list.setAdapter(mAdapter); 
                    mAdapter.notifyDataSetChanged(); 

只需在适配器中使用方法addAll( )添加数据即可。在每次数据加载时都不需要一次又一次地更换适配器。

我在你的代码中发现了这个问题。