ListView中的图像/ TextView转移了一点

时间:2018-02-08 16:32:34

标签: android listview android-imageview android-adapter

如果我滚动列表,我的ListView中的TextViews会将位置更改几厘米。我认为问题是如果滚动移动后它们不可见,listView将回收行。但我不知道如何解决这个问题。而且我没有发现TextView的移位何时出现。有时没有任何问题,有时几乎所有条目都有一点转变

private class ListAdapter extends ArrayAdapter<Player> {
    public ListAdapter() {
        super(Activity.this, R.layout.entry2, playerList);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View itemView = convertView;

        final Player player = playerList.get(position);

        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.entry2, parent, false);
        }else{

        }


        TextView playerName = (TextView) itemView.findViewById(R.id.PlayerName);
        ImageButton delButton = (ImageButton) itemView.findViewById(R.id.playerDel);
        TextView playerBild = (TextView)  itemView.findViewById(R.id.PlayerBild);
        playerName.setText(player.getName());
        playerBild.setBackgroundResource(player.getColor());

        playerBild.setForeground(player.playerPic);


        return itemView;
    }
}
<android.support.design.widget.CoordinatorLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.app.app.Main">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:orientation="vertical"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginTop="10dp"
        android:clipToPadding="false"
        android:gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/PlayerBild"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:background="@drawable/color_player0" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="212dp"
            android:layout_height="90dp"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/PlayerName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="Let's Drink"
                android:textColor="@color/black"
                android:textSize="24sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/playerDel"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@android:drawable/ic_menu_close_clear_cancel"
                android:clickable="true"
                android:padding="10dp" />

        </LinearLayout>
    </LinearLayout>


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用Holder Pattern ...

private class ListAdapter extends ArrayAdapter < Player > {
 Holder holder;
 public ListAdapter() {
  super(Activity.this, R.layout.entry2, playerList);
 }

 @Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  View itemView = convertView;

  final Player player = playerList.get(position);

  if (itemView == null) {
   itemView = getLayoutInflater().inflate(R.layout.entry2, parent, false);
   holder = new Holder();
   holer.playerName = (TextView) itemView.findViewById(R.id.PlayerName);
   holder.delButton = (ImageButton) itemView.findViewById(R.id.playerDel);
   holder.playerBild = (TextView) itemView.findViewById(R.id.PlayerBild);
   itemvView.setTag(holder);
  } else {
   holder = (Holder) itemView.getTag();
  }



  holder.playerName.setText(player.getName());
  holder.playerBild.setBackgroundResource(player.getColor());

  holder.playerBild.setForeground(player.playerPic);


  return itemView;
 }
 class Holder {
  TextView playerName, playerBild;
  ImageButton delButton;
 }
}