2

Textview と画像を持つカスタム リスト ビューがあります。テキストビューをクリックすると、その特定の行の非表示のレイアウトが展開されます。しかし、たとえば、2行目をクリックすると、10行目も展開されます。ここに私のコードがあります、

CustomListAdapter.java

public View getView(final int position, View convertView, ViewGroup parent) {
    holder = null;
    DataFields rowItems = (DataFields) getItem(position);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.home_field_row, null);

        holder = new ViewHolder();
        holder.dataFields = items.get(position);
        holder.mName = (TextView) convertView
                .findViewById(R.id.hmFieldName);
        holder.mDeleteImage = (ImageView) convertView
                .findViewById(R.id.hmFieldDeleteImage);
        holder.deleteMainRL = (RelativeLayout) convertView
                .findViewById(R.id.hmdeleteMainRL);
        holder.mDeleteImage.setTag(position);
        holder.mName.setTag(position);
        holder.deleteMainRL.setTag(position);
        final View clickView = convertView;

        holder.mName.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RelativeLayout displayAddInfo = (RelativeLayout)clickView.findViewById(R.id.displayRecordRL);
                Animation expandAnim = expand(displayAddInfo,
                                true);
                displayAddInfo
                        .startAnimation(expandAnim);
                    }
                });

        convertView.setTag(holder);
    }

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

    holder.mName.setText(rowItems.getName());

    return convertView;
}

どうすればこれを修正できますか? どんな種類の助けや提案も大歓迎です。ありがとう。

アップデート

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hmFieldMainRL"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/grid_shape" >

    <TextView
        android:id="@+id/hmFieldName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/displayRecordRL"
        android:layout_alignParentLeft="true"
        android:gravity="left"
        android:padding="15dp"
        android:shadowColor="#000000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:clickable="false"
        android:shadowRadius="2"
        android:text="@string/no_data"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#F2F2F2" />

    <RelativeLayout
        android:id="@+id/displayRecordRL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/hmFieldName"
        android:layout_centerVertical="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:background="@drawable/display_record_bg"
        android:visibility="gone" >

        <EditText
            android:id="@+id/displayRecordName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_toLeftOf="@+id/displayRecordUpdate"
            android:padding="10dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/displayRecordPwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/displayRecordName"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/displayRecordShow"
            android:inputType="textPassword"
            android:paddingLeft="10dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/displayRecordAddInfoImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:layout_below="@id/displayRecordPwd"
            android:contentDescription="@string/right_arrow"
            android:visibility="gone"
            android:src="@drawable/info" />

        <EditText
            android:id="@+id/displayRecordAddInfo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/displayRecordAddInfoImg"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="2dp"
            android:hint="Additional Information"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/displayRecordUpdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/display_record_img"
            android:contentDescription="@string/right_arrow"
            android:padding="10dp"
            android:src="@drawable/update_rec" />

        <ImageView
            android:id="@+id/displayRecordShow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/displayRecordUpdate"
            android:layout_marginTop="10dp"
            android:contentDescription="@string/right_arrow"
            android:padding="10dp"
            android:src="@drawable/eye" />

        <ImageView
            android:id="@+id/displayRecordShowRed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/displayRecordUpdate"
            android:layout_marginTop="10dp"
            android:contentDescription="@string/right_arrow"
            android:padding="10dp"
            android:src="@drawable/redeye"
            android:visibility="gone" />
    </RelativeLayout>

</RelativeLayout>
4

4 に答える 4

2

しかし、たとえば、2行目をクリックすると、10行目も展開されます。

ListViews が行レイアウトをリサイクルする方法と戦っています。しかし、これは簡単に対処できます。まず、いくつかの新しいクラス変数を追加しましょう。

SparseBooleanArray expanded = new SparseBooleanArray();
LayoutInflater inflater; // initialize this in your constructor

次に、表示する必要があるexpandedかどうかを確認するために使用します。displayRecordRL

public View getView(final int position, View convertView, ViewGroup parent) {
    DataFields rowItems = (DataFields) getItem(position);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.home_field_row, null);

        holder = new ViewHolder();
        holder.dataFields = items.get(position);
        holder.mName = (TextView) convertView
                .findViewById(R.id.hmFieldName);
        holder.mDeleteImage = (ImageView) convertView
                .findViewById(R.id.hmFieldDeleteImage);
        holder.deleteMainRL = (RelativeLayout) convertView
                .findViewById(R.id.hmdeleteMainRL);

        // Add this to your holder
        holder.mAddInfo = (RelativeLayout) convertView
                .findViewById(R.id.displayRecordRL);

        holder.mDeleteImage.setTag(position);
        holder.mName.setTag(position);
        holder.deleteMainRL.setTag(position);

        holder.mName.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get the ViewHolder
                holder = (ViewHolder) ((View) v.getParent()).getTag(); 
                Animation expandAnim = expand(holder.mAddInfo, true);
                holder.mAddInfo.startAnimation(expandAnim);

                // Remember that this View is expanded
                int pos = (Integer) v.getTag();
                expanded.put(pos, true);
            }
        });

        convertView.setTag(holder);
    }

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

    holder.mName.setText(rowItems.getName());
    if(expanded.get(position, false)) 
        holder2.mAddInfo.setVisibility(View.VISIBLE);
    else
        holder2.mAddInfo.setVisibility(View.GONE);

    return convertView;
}

これが各行を表示するかどうかを追跡し、単純な if ステートメントを使用して、再利用されたレイアウトが "Add Info" セクションを正しく表示または非表示にしていることを確認する方法に注意してください。

于 2013-02-25T21:17:21.330 に答える
1

アダプターに単純な属性を追加する必要がありますint。アイテムをクリックすると、現在選択されている位置が更新されます。ビューを作成するときは、位置が選択した位置と等しいかどうかを確認してください。

public View getView(final int position, View convertView, ViewGroup parent) {
     if(this.selectedPosition == position){
     //add a holder
     else{
     //don't add a holder

     return convertview;
}

私が言っているのは、View のリサイクルの問題が発生しているということです。アイテム n°2 を変更し、アイテム n°10 のビューがビュー n°2 から作成された場合、不要なアイテムになります (クリックされたように見えます)。

リストの項目がクリックされると:

1) 選択した項目 (アダプターの属性) を更新します。たとえば、リストビューのアイテム 12 をクリックすると、selectedItem = 12;

2) メソッド notifyDataSetChanged() を呼び出します。これにより、リストの表示が更新されます。

3) ビュー (アダプターの getView()) を作成するときに、選択したアイテムに対応するかどうかを各位置で確認します。もしそうなら、あなたの特別なビューを構築してください(あなたが何をしたいのか正確にはわかりません)。選択した項目に対応していない場合は、ビューを「通常どおり」構築します

于 2013-02-25T19:50:43.000 に答える
0

発生する問題は、スクロール時にリスト ビュー内の別のアイテムに再利用されているビュー (converView) をアニメーション化することです。

この YouTube ビデオを見ると、問題を解決する方法がわかるかもしれません。Chet Haase (Google エンジニア) "DevBytes" シリーズ "ListView Animations" :

https://www.youtube.com/watch?v=8MIfSxgSHIs# !

于 2013-02-25T20:11:28.967 に答える
0

R.id.displayRecordRL をビューホルダーの一部として配置し、OnClick メソッドで呼び出します。

holder.displayAddInfo = (RelativeLayout)clickView.findViewById(R.id.displayRecordRL);

および OnclickMethod で:

Animation expandAnim = expand(holder.displayAddInfo,
                                true);
                holder.displayAddInfo
                        .startAnimation(expandAnim);
                    }
于 2013-02-25T19:24:39.120 に答える