2

次のように、グループごとに異なる子レイアウトを表示したい:

@Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {

            if (convertView == null) {

                switch (groupPosition){

                case 0:
                    LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.child_row, null);
                    TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                case 1:
                    LayoutInflater inflater1 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater1.inflate(R.layout.child_row1, null);
                    TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName1.setText(arrChildelements[groupPosition][childPosition]);
                    break; 

                case 2:
                    LayoutInflater inflater2 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater2.inflate(R.layout.child_row2, null);
                    TextView tvPlayerName2 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName2.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                case 3:             
                    LayoutInflater inflater3 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater3.inflate(R.layout.child_row3, null);
                    TextView tvPlayerName3 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName3.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                }


            } return convertView;
    }

私の問題は、グループをクリックしているときに、子リストがグループ間で入れ替わっていることです。

私が間違っていることを誰かに教えてもらえますか?

4

2 に答える 2

7

それは

if (convertView == null)

それを削除して、この if ステートメントを削除した後に正常に機能する場合は、それを返し、 if (convertView == null) 内に if ステートメントを追加して、 if (convertView == null) を使用すると、スクロールがはるかに遅くなり、より多くの RAM が使用されます。

たとえば、すべてのビューを 1 つの xml ファイルまたは 1 つのビューに配置し、switch メソッド内で setVisibility を使用して、現在のリスト項目に適したレイアウトの部分を表示できます。

于 2012-09-07T09:17:40.357 に答える
3

if (convertView == null)

子ビューのカスタム アダプタからそれを削除します

それはすべてのためにうまく働いています

于 2013-10-21T15:44:09.967 に答える