简体   繁体   English

滚动时在彼此上方绘制的ListView项目(Android)

[英]ListView items drawn on top of each other when scrolling (Android)

In a fragment, I have a ListView that has a custom ParseQueryAdapter<T> . 在一个片段中,我有一个具有自定义ParseQueryAdapter<T>的ListView。 The problem may not have anything to do with Parse, although I'm not sure. 尽管我不确定,但问题可能与“解析”无关。

As I was testing my app, I noticed something very strange. 在测试应用程序时,我发现了一些非常奇怪的东西。 When I would scroll down my ListView, all the visible ListView items would be drawn on top of the next ListView item as seen in the second image below. 当我向下滚动ListView时,所有可见的ListView项都将绘制在下一个ListView项的顶部,如下面的第二个图像所示。

The list initialized properly as such: 该列表已正确初始化,如下所示:

正确初始化

As you can see, in my list item layout, I have an ImageView ( ParseImageView to be specific) and a TextView. 如您所见,在我的列表项布局中,我有一个ImageView( ParseImageViewParseImageView )和一个TextView。 The TextView now displays some notes (don't mind the ID user_name_text_view ) and the ImageView displays a placeholder blank profile picture. 现在,TextView将显示一些注释(不必介意ID user_name_text_view ),而ImageView将显示一个占位符空白个人资料图片。

When I scrolled down, the list looked like: 当我向下滚动时,列表如下所示:

堆叠查看

Here's my list view layout named fragment_post_view_list_view : 这是我的名为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>

Here's my list item layout named list_item_post_view : 这是名为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>

Here's my adapter named PostViewListViewAdapter : 这是我的名为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;
    }
}

How can I fix this problem? 我该如何解决这个问题?

Is this an issue with XML or Java ? 这是XMLJava的问题吗?

I was following the two tutorials from Parse and the example from the Parse docs : 我正在关注Parse的两个教程和Parse docs示例

I set the adapter and ListView here: 我在这里设置适配器和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);

I've tried getting rid of the ParseImageView in my list item layout, but my TextViews still draw on top of each other when I scroll. 我尝试摆脱列表项布局中的ParseImageView ,但是在滚动时,我的TextView仍会相互绘制。

Edit: 编辑:

I forgot to mention that the list items display on top of each other after an orientation change. 我忘了提到方向改变 ,列表项彼此重叠显示。

I tested this on my Galaxy S5 (Android version 4.4.2 and Parse 1.4.1). 我在Galaxy S5(Android 4.4.2和Parse 1.4.1)上对此进行了测试。

In my Activity, I show the Fragment here (called PostViewListViewFragment ): 在我的活动中,我在此处显示片段(称为PostViewListViewFragment ):

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

Try below layout : 请尝试以下布局:

<?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 >

Make Sure your adapter like this: 确保您的适配器是这样的:

@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:You should watch this Google IO video about Listview,and here is the slides . PS:您应该观看有关Listview的Google IO 视频 ,这是幻灯片

First create a ViewHolder class 首先创建一个ViewHolder类

static class ViewHolder {

        protected TextView usernameTextView;
    }

Then change your getItemView method like below 然后如下更改您的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;


}

The problem seems to be in your list item layout - 问题似乎出在您的列表项布局中-

Change it to this - 更改为此 -

<?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>

Probably you have extra background for each list item set that is causing such effect. 可能每个列表项集都有额外的背景,导致这种效果。 Alter and watch. 改变并观看。

Hope this gives you idea! 希望这能给您想法!

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

Credit to @VedPrakash for helping me fix this. 感谢@VedPrakash帮助我解决此问题。

In case it helps anyone, I fixed the problem by replacing the fragment not adding it. 万一它对任何人都有帮助,我通过替换片段而不添加 片段来解决问题。 So I changed this line from : 所以我以下更改了这一行:

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

to :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM