【问题标题】:ListView items drawn on top of each other when scrolling (Android)滚动时相互叠加的 ListView 项目(Android)
【发布时间】:2014-07-01 01:20:51
【问题描述】:

在一个片段中,我有一个具有自定义 ParseQueryAdapter<T> 的 ListView。这个问题可能与 Parse 没有任何关系,尽管我不确定。

在测试我的应用程序时,我注意到一些非常奇怪的事情。当我向下滚动 ListView 时,所有可见的 ListView 项目都将绘制在下一个 ListView 项目的顶部,如下面的第二张图片所示。

列表正确初始化如下:

如您所见,在我的列表项布局中,我有一个 ImageView(具体来说是ParseImageView)和一个 TextView。 TextView 现在显示一些注释(不要介意 ID user_name_text_view),ImageView 显示占位符空白个人资料图片。

当我向下滚动时,列表看起来像:

这是我的列表视图布局,名为fragment_post_view_list_view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/post_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

这是我的列表项布局,名为list_item_post_view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.parse.ParseImageView
        android:id="@+id/icon"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignParentLeft="true"
        android:background="@drawable/com_facebook_profile_picture_blank_square" />

    <TextView
        android:id="@+id/user_name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/icon"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/link_blue" />

</RelativeLayout>

这是我的适配器,名为PostViewListViewAdapter

public class PostViewListViewAdapter extends ParseQueryAdapter<Post> {
    // call superclass with a query to populate list view
    public PostViewListViewAdapter(Context context, final String[] postsObjectIds) {
        super(context, new ParseQueryAdapter.QueryFactory<Post>(){
            public ParseQuery<Post> create() {
                ParseQuery<Post> query = Post.getQuery();
                query.whereContainedIn("objectId", Arrays.asList(postsObjectIds));
                return query;
            }
        });
    }

    // this is similar to getView method in an adapter
    @Override
    public View getItemView(Post post, View v, ViewGroup parent) {
        if(v == null) {
            v = View.inflate(getContext(), R.layout.list_item_post_view, null);
        }

        super.getItemView(post, v, parent);
        TextView usernameTextView = (TextView) v.findViewById(R.id.user_name_text_view);
        usernameTextView.setText(post.getNotes()); // some string

        return v;
    }
}

我该如何解决这个问题?

这是 XMLJava 的问题吗?

我正在学习 Parse 和 example from the Parse docs 的两个教程:

我在这里设置了适配器和ListView:

View rootView = inflater.inflate(R.layout.fragment_post_view_list_view, container, false);
mPostsObjectIds = SOME_STRING[];

PostViewListViewAdapter adapter = new PostViewListViewAdapter(getActivity(), mPostsObjectIds);

ListView listView = (ListView) rootView.findViewById(R.id.post_list_view);
listView.setAdapter(adapter);

我已经尝试在列表项布局中删除 ParseImageView,但当我滚动时,我的 TextViews 仍然相互重叠。

编辑:

我忘了提到列表项在方向更改后显示在彼此之上。

我在 Galaxy S5(Android 版本 4.4.2 和 Parse 1.4.1)上对此进行了测试。

在我的活动中,我在此处显示片段(称为PostViewListViewFragment):

getSupportFragmentManager().beginTransaction().add(android.R.id.content, new PostViewListViewFragment()).commit();

【问题讨论】:

    标签: android android-layout android-listview parse-platform


    【解决方案1】:

    试试下面的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ListView
            android:id="@+id/post_list_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:scrollbars="none"    >
    
        </ListView>
    
    </RelativeLayout >
    

    【讨论】:

      【解决方案2】:

      确保您的适配器是这样的:

      @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          View rowView = convertView;
          // reuse views
          if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.rowlayout, null);
            // configure view holder
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
            rowView.setTag(viewHolder);
          }
      
          // fill data
          ViewHolder holder = (ViewHolder) rowView.getTag();
          String s = names[position];
          holder.text.setText(s);
      
          return rowView;
        }
      } 
      

      PS:你应该看看这个关于 Listview 的 Google IO video,这里是 slides

      【讨论】:

      • 什么是ViewHolder
      • 你应该看看这个关于 Listview 的 Google IO video,这里是 slides
      • 感谢您提供这些资源。由于我使用的是Parse Android API,我认为这行不通。
      【解决方案3】:

      首先创建一个 ViewHolder 类

      static class ViewHolder {
      
              protected TextView usernameTextView;
          }
      

      然后像下面这样更改您的 getItemView 方法

      public View getItemView (Post post, View convertView , ViewGroup parent)
      {
      
          ViewHolder viewHolder = null;
      
          if (convertView == null)
          {
              LayoutInflater inflator = context.getLayoutInflater();
              convertView = inflator.inflate(R.layout.list_item_post_view, null);
              viewHolder = new ViewHolder();
              viewHolder.usernameTextView = (TextView)convertView.findViewById(R.id.user_name_text_view);
      
              convertView.setTag(viewHolder);
      
              convertView.setTag(R.id.user_name_text_view, viewHolder.usernameTextView);
      
      
          }
      
          else
          {
              viewHolder = (ViewHolder) convertView.getTag();
          }
      
      
          viewHolder.usernameTextView.setText(post.getNotes()); // some string
      
      
          return convertView;
      
      
      }
      

      【讨论】:

      • 不幸的是,这不起作用。此外,您应该将第一个参数从int position 切换到Post post,并将viewHolder 分配给null 是多余的。
      • 如果将object赋值为null有什么问题?请你解释一下?
      • 没什么。它的行为方式相同,但由于您总是在 if 块或 else 块中初始化 viewHolder,因此将其初始化为 null 是多余的。 viewHolder 将始终在下次读取之前分配。
      • @lschlessinger,尝试删除这一行:convertView.setTag(R.id.user_name_text_view, viewHolder.usernameTextView);并检查。
      【解决方案4】:

      问题似乎出在您的列表项布局中 -

      改成这个 -

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <com.parse.ParseImageView
              android:id="@+id/icon"
              android:layout_width="75dp"
              android:layout_height="75dp"
              android:layout_alignParentLeft="true" />
      
          <TextView
              android:id="@+id/user_name_text_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_toRightOf="@+id/icon"
              android:textAppearance="?android:attr/textAppearanceSmall"
              android:textColor="@color/link_blue" />
      
      </RelativeLayout>
      

      可能您对导致这种效果的每个列表项集都有额外的背景。 改变并观察。

      希望这能给你一些想法!

      【讨论】:

      • 感谢您的回答!但是,这种特定的布局不起作用。除了 ParseImageView 背景,你做了什么改变?我会继续玩这些布局。
      【解决方案5】:

      尝试将列表视图布局高度更改为 match_parent。

      【讨论】:

      • 你是指ID为post_list_view的ListView吗?
      • 是的,就是这个。
      【解决方案6】:

      感谢@VedPrakash 帮助我解决了这个问题。

      如果它对任何人有帮助,我通过替换 fragment 而不是 添加 来解决问题。所以我将这一行改为:

      getSupportFragmentManager().beginTransaction().add(android.R.id.content, new PostViewListViewFragment()).commit();
      

      getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new PostViewListViewFragment()).commit();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多