使用ViewHolder不会在CustomList上加载Android ImageView

时间:2017-03-30 17:38:26

标签: android android-viewholder

我有一个自定义列表视图,我最近升级到使用View Holder。现在项目工作正常,但两个图像视图中的图像不会显示在屏幕上。

enter image description here

这两个黑色图标显示为“透明”,但如果我点击它们就会起作用。

这是布局:

  <?xml version="1.0" encoding="UTF-8"?>

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgInfo"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="12dp"
                android:layout_weight="0.04"
                android:visibility="visible"
                app:srcCompat="@drawable/details64" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/imgInfo"
                android:layout_toLeftOf="@+id/imgEdit"
                android:layout_toRightOf="@+id/imgInfo"
                android:layout_weight="0.24"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtChamado"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:textColor="@android:color/background_dark"
                    android:textSize="25sp" />

                <TextView
                    android:id="@+id/txtSolicitante"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:textColor="@android:color/background_dark"
                    android:textSize="15sp" />

                <TextView
                    android:id="@+id/txtItemDeCatalogo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:textColor="@android:color/background_dark"
                    android:textSize="15sp" />
            </LinearLayout>

            <ImageView
                android:id="@+id/imgEdit"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginTop="12dp"
                android:layout_weight="0.03"
                android:contentDescription=""
                app:srcCompat="@drawable/pencil64" />
        </LinearLayout>


    </RelativeLayout>

</LinearLayout>

这是代码段:

 @Override
    public View getView(final int position, View view, final ViewGroup parent) {

        View convertView;
        ViewHolder holder;

        if (view == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.customized_list_view, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            convertView = view;
            holder = (ViewHolder) convertView.getTag();
        }

        //  Object declaration
        final Sette setteData = getItem(position);

        //region Put the Tasks values to the textviews
        // Get me two strings, one with the older value and one with the newest
            String oldChamado = holder.txtChamado.getText().toString();
            String newChamado = setteData.getChamado();
            // Do not repeat the string in the textview
            if (!(oldChamado.equals(newChamado))) {
                holder.txtChamado.setText(newChamado);
            }
     //region Click Listener for the button that leads to Approvals Activity
        if (!aBoolean) {
            holder.imgEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    chamadoClicked = setteData.getChamado();
                    solicitanteClicked = setteData.getSolicitante();
                    itemDeCatalogoClicked = setteData.getItemDeCatalogo();
                    id = setteData.getId();

                    Intent intent = new Intent(context.getApplicationContext(), ApprovalsActivity.class);
                    intent.putExtra("Id", id);
                    intent.putExtra("chamadoClicked", chamadoClicked);
                    intent.putExtra("solicitanteClicked", solicitanteClicked);
                    intent.putExtra("itemDeCatalogoClicked", itemDeCatalogoClicked);
                    context.startActivity(intent);
                }
            });
        }
        //endregion
 return convertView;
    }// END METHOD

ViewHolder(我从互联网示例中获得):

  public class ViewHolder {
    TextView txtChamado;
    TextView txtSolicitante;
    TextView txtItemDeCatalogo;
    ImageView imgInfo, imgEdit;
    Typeface bebasKai;
    Typeface london;

    public ViewHolder(View view){
        bebasKai = Typeface.createFromAsset(context.getAssets(), "fonts/bebaskai.otf");
        london = Typeface.createFromAsset(context.getAssets(), "fonts/london.ttf");
        txtChamado = (TextView) view.findViewById(R.id.txtChamado);
        txtChamado.setTypeface(bebasKai);
        txtSolicitante = (TextView) view.findViewById(R.id.txtSolicitante);
        txtSolicitante.setTypeface(london);
        txtItemDeCatalogo = (TextView) view.findViewById(R.id.txtItemDeCatalogo);
        txtItemDeCatalogo.setTypeface(london);
        imgEdit = (ImageView) view.findViewById(R.id.imgEdit);
        imgInfo = (ImageView) view.findViewById(R.id.imgInfo);
    }
}

我尝试清理,重建,再次从BitBucket下载项目,在手机上从头开始重新安装应用程序,无效缓存/重启并检查代码。

我错过了一些明显的东西吗?

**编辑:**谢谢SaNtoRiaN,我知道这很简单,我不知道。

1 个答案:

答案 0 :(得分:1)

app:srcCompat中的ImageView属性更改为android:src,以便正确查看您的图片。要了解有关差异的更多信息,请查看this answer。