0

我正在开发 Android 项目。现在我必须创建自己的可扩展列表。这是我遵循的解决方案:

链接到我正在处理的可扩展列表的源代码

这就是生成我 TextView 的代码(它是列表中的元素)。我想创建类似 android.R.layout.simple_expandable_list_item 项的东西而不是这个 TextView。我的意思是在左边放一些图片,然后是文字。我怎么能这样做?

我尝试过使用 Button 并在 Button 上设置左图标,但没有帮助。按钮不会展开/折叠我的列表。

 @Override
              public View getGroupView(int groupPosition, boolean isExpanded,
                           View convertView, ViewGroup parent) {
                     TextView tv = new TextView(Vincent_ThreeelevellistActivity.this);
                     tv.setText("->FirstLevel");
                     tv.setTextColor(Color.BLACK);
                     tv.setTextSize(20);
                     tv.setBackgroundColor(Color.BLUE);
                     tv.setPadding(10, 7, 7, 7);
                     return tv;

              }
4

2 回答 2

0

你需要像这样使用。此代码只是一个示例。请不要更改参数并相应地实现您的代码。

@Override
       public View getGroupView(int position, View convertView, ViewGroup parent) {   

       ViewHolder holder = null;
       TextView title = null;
       TextView detail = null;
       ImageView i11=null;
       RowData rowData= getItem(position);
       if(null == convertView){
            convertView = mInflater.inflate(R.layout.list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
 }
             holder = (ViewHolder) convertView.getTag();
             title = holder.gettitle();
             itle.setText(rowData.mTitle);
             detail = holder.getdetail();
             detail.setText(rowData.mDetail);                                                     

             i11=holder.getImage();
             i11.setImageResource(imgid[rowData.mId]);
             return convertView;
}
            private class ViewHolder {
            private View mRow;
            private TextView title = null;
            private TextView detail = null;
            private ImageView i11=null; 

            public ViewHolder(View row) {
            mRow = row;
 }
         public TextView gettitle() {
             if(null == title){
                 title = (TextView) mRow.findViewById(R.id.title);
                }
            return title;
         }     

         public TextView getdetail() {
             if(null == detail){
                  detail = (TextView) mRow.findViewById(R.id.detail);
                    }
           return detail;
         }
        public ImageView getImage() {
             if(null == i11){
                  i11 = (ImageView) mRow.findViewById(R.id.img);
                                      }
                return i11;
        }
     }
于 2013-05-17T08:47:59.717 回答
0

你的问题不是很清楚。但是,据我了解,这是我的解决方案。

Option1 :您可以在 xml 中创建自定义布局,然后对其进行充气并从 getGroupView 返回。

选项 2:您可以为您的 textview 项目设置一个 complexDrawable,以便它在其旁边显示一个图像。

如果要自定义向下箭头标记,可以为可展开列表视图设置 android:groupIndicator。

于 2013-05-17T08:49:38.850 回答