2013-04-10 4 views
0

Я новичок в android, теперь я пытаюсь использовать ExpandableListView, чтобы отображать первые типы работ на уровне группы, а затем работать с элементами на уровне ребенка. Но очень странно, что макет fill_parent не работает в методе getChildView, он просто действует как wrap_content, а также высота соответствующих текстовых просмотров становится равной 0, когда я регистрирую их в отношении того, что я задал в макете, и, таким образом, все текстовые эффекты исчезают, если я вручную не установил размер текста в коде. Самая странная вещь - проблема только для детей, уровень группы отлично работает, где мне не нужно устанавливать какой-либо текст или высоту.expandablelistview getchildview неверно отображен

Ниже приведены некоторые фрагменты кода, не могли бы вы помочь мне узнать, в чем дело? Огромное спасибо!!

  1. активность
public class WorkListActivity extends Activity implements 
    ExpandableListView.OnGroupClickListener { 
private ExpandableListView elv; 
List<WorkTypeBean> worktypes = null; 
ExpandableAdapter viewAdapter; 
WorkTypeBean worktypeBean = null; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.work_items); 
    elv = (ExpandableListView) findViewById(R.id.workList_ev); 
    elv.setOnGroupClickListener(this); 
    getWorkTypes(); //get all work types user can visit from server 
} 
@Override 
public boolean onGroupClick(ExpandableListView parent, View v, 
     int groupPosition, long id) { 
    if (!parent.isGroupExpanded(groupPosition)){ 
     getWorkItems(groupPosition); 
     //fetch work items from server only when specific group clicked 
    } 
    return false; 
} 
class ExpandableAdapter extends BaseExpandableListAdapter { 
    @Override 
    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView==null){ 
      LayoutInflater layoutInflater = (LayoutInflater) getBaseContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(
        R.layout.work_items_c, null); 
      holder = new ViewHolder(); 
      holder.tv1 = (TextView) convertView.findViewById(R.id.tv_work_item_title); 
      holder.tv2 = (TextView) convertView.findViewById(R.id.tv_work_item_applier); 
      holder.iv1 = (ImageView) convertView.findViewById(R.id.iv_work_item_hl); 
      convertView.setTag(holder); 
     }else{ 
      holder = (ViewHolder)convertView.getTag(); 
     } 
     convertView.setBackgroundColor(Color.WHITE); 
     WorkTypeBean typeBean = worktypes.get(groupPosition); 
     if (typeBean==null){ 
      return convertView; 
     } 
     WorkItemBean workitem = typeBean.getItem(childPosition); 
     if (workitem==null){ 
      return convertView; 
     } 
     String title = workitem.getTitle(); 
     String applier = workitem.getApplier(); 
     String postdate = workitem.getArriveTime(); 
     int waitdays = Integer.parseInt(workitem.getWaitDays()); 
     holder.tv1.setTextColor(Color.BLACK); 
     holder.tv1.setText(title); 
     holder.tv1.setTextSize(15); 
     holder.tv2.setTextColor(Color.BLACK); 
     holder.tv2.setTextSize(10); 
     //if not set, tv2 remains original text "loading..." from work_item_c.xml 
     holder.tv2.setText(applier+" ("+postdate+")"); 
     if (waitdays > 2){ 
      holder.iv1.setImageResource(R.drawable.wl_urgent); 
     }else{ 
      holder.iv1.setImageResource(R.drawable.wl_normal); 
     } 
     return convertView; 
    } 
    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     ViewHolder holder; 
if (convertView == null) { 
LayoutInflater inflater = (LayoutInflater) getBaseContext() 
.getSystemService(LAYOUT_INFLATER_SERVICE); 
convertView = inflater.inflate(R.layout.work_items_h, null); 
      holder = new ViewHolder(); 
      holder.tv1 = (TextView) convertView.findViewById(R.id.tv_worktype); 
      holder.iv1 = (ImageView) convertView.findViewById(R.id.iv_worktype_counter); 
      convertView.setTag(holder); 
     }else{ 
      holder = (ViewHolder)convertView.getTag(); 
} 
     WorkTypeBean worktype = worktypes.get(groupPosition); 
     if (worktype==null){ 
      return convertView; 
     } 
     holder.tv1.setTextColor(Color.BLACK); 
     holder.tv1.setText(worktype.getTitle()); 
     return convertView;  
    } 
    //... some other code 
    } 
    } 
  1. раскладка Уровень группы: work_items_h.xml // работает правильно
<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" > 

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

     <TextView 
      android:id="@+id/tv_worktype" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="50dp" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <ImageView 
      android:id="@+id/iv_worktype_counter" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" /> 

    </RelativeLayout> 

</LinearLayout> 
    </LinearLayout> 
  1. раскладка уровня ребенка: work_items_c.xml // fill_parent не работает, ни высота TextView
<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/iv_work_item_hl" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/wl_normal" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/tv_work_item_title" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="loading..." 
       android:textSize="15dp" /> 
      <!--tried android:layout_height="fill_parent" but not work--> 

      <TextView 
       android:id="@+id/tv_work_item_applier" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="right" 
       android:text="loading..." 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textSize="10dp" /> 

     </LinearLayout> 

    </LinearLayout> 

ответ

0

Просто переопределите расположение некоторых других, и теперь это правильно. Вот мои новые макеты:

  1. work_item_h.xml

<TextView 
     android:id="@+id/tv_worktype" 
     android:layout_width="259dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left|center" 
     android:paddingBottom="10px" 
     android:paddingLeft="40px" 
     android:paddingTop="10px" 
     android:textColor="#ff0000" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20.0px" /> 

    <ImageView 
     android:id="@+id/iv_worktype_counter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|center" 
     android:src="@drawable/counter" /> 

</LinearLayout> 
  1. work_item_c.xml

<ImageView 
    android:id="@+id/iv_work_item_hl" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginBottom="2.0px" 
    android:layout_marginLeft="10.0px" 
    android:layout_marginTop="10.0px" 
    android:paddingTop="6.0px" /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="2.0px" 
    android:baselineAligned="@+id/iv_work_item_hl" 
    android:orientation="vertical" 
    android:paddingTop="2.0px" > 

    <TextView 
     android:id="@+id/tv_work_item_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left|center" 
     android:layout_marginLeft="10.0px" 
     android:textColor="#ff000000" 
     android:textSize="20.0px" /> 

    <TextView 
     android:id="@+id/tv_work_item_applier" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left|center" 
     android:layout_marginLeft="10.0px" 
     android:textColor="#ff838383" 
     android:textSize="13.0px" /> 
</LinearLayout> 

Но я до сих пор не знаю, что это проблема на моей предыдущей схеме он правильно показал в моем верстальщик затмение. Во всяком случае, я рад видеть, что это работает наконец. Спасибо всем ..., кажется, никто не заботится о моем вопросе, а затем благодарим себя, слава богу :)

С уважением, Jeff

Смежные вопросы