2013-05-17 74 views
0

我正在开发Android项目。现在我必须创建我自己的可扩展列表。这是我遵循的解决方案:getGroupView设置类似于Android项目中的Simple_Expandable_List_item项目

link to source code of expandable list that I am working on

而这其中产生了我的TextView(从列表中它的元素)的代码。我想创建而不是这个TextView的东西像android.R.layout.simple_expandable_list_item项目。我的意思是把一些图片放在左边,然后是文字。我该怎么做?

我试过用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; 

       } 

回答

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; 
     } 
    } 
0

你的问题不太清楚。但从我理解这里是我的解决方案。

选项1:您可以在xml中创建自定义布局,然后膨胀并从getGroupView返回。

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

如果您想自定义向下箭头标记,您可以设置可扩展列表视图的android:groupIndicator。