2014-09-02 27 views
1

我想通过高度权重将2个列表“部分”合并到LinearLayout中。我遇到的问题是,尽管“部分”的容器以适当的高度显示(背景为sub_gray),并且适配器计数正确,但我的实际列表行的高度为0,这使得我的列表看起来不可见。ListView项不会膨胀以包装内容,高度== 0但不应该

列表项

下面是我的名单列/项直接与工作的代码。 To avoid the wall of code getting too long, I will include my code for one of the lists seeing as they are both behaving the same.

layout_quick_guide_checklist_item

<?xml version="1.0" encoding="utf-8"?> 
<com.my_project.project.QuickGuideChecklistListItemView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="50dp" 
    android:background="@color/sub_gray" 
    android:orientation="horizontal" > 

    <CheckBox 
     android:id="@+id/checkbox_checklist" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_gravity="center" 
     android:checked="true" 
     android:clickable="false" /> 

    <TextView 
     android:id="@+id/text_field" 
     style="@style/default_text_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:text="@string/height_wrap_string"/> 

</com.my_project.project.QuickGuideChecklistListItemView> 

QuickGuideChecklistListItemView

public class QuickGuideChecklistListItemView extends LinearLayout{ 

    private TextView listText; 

    public QuickGuideChecklistListItemView(Context context) { 
     super(context); 
    } 

    public QuickGuideChecklistListItemView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public QuickGuideChecklistListItemView(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public static QuickGuideChecklistListItemView inflate(ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) parent.getContext(). 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     QuickGuideChecklistListItemView itemView = (QuickGuideChecklistListItemView) 
       inflater.inflate(R.layout.layout_quick_guide_checklist_item, parent, false); 

     return itemView; 
    } 

    @Override 
    public void onFinishInflate(){ 
     super.onFinishInflate();   
     listText = (TextView) findViewById(R.id.text_field); 
    } 

    public void setText(String text){ 
     listText.setText(text); 
    } 
} 

COMPLETE LIST

现在我采取上面的代码和M中使用它y活动来创建一个列表(召回;在我的活动中实际上有两个列表,构造类似,都导致行高为0)。

QuickGuideDetailActivity

public class QuickGuideDetailActivity extends NavigationDrawerActivity implements OnClickListener{ 

    private final String TAG = getClass().getSimpleName(); 

    private QuickGuideModel selectedQuickGuide; 
    private QuickGuideChecklistAdapter checklistAdapter; 
    private QuickGuideTipAdapter tipsAdapter; 
    private TextView quickGuidesTitle, quickGuidesDescription, inflaterButton; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     selectedQuickGuide = QuickGuidesActivity.selectedQuickGuide;  
     buildLists(); 
    } 

    @Override 
    protected int getLayoutId(){ 
     return R.layout.sub_activity_quick_guide; 
    } 

    public void checkAdapterSize(View v){ 
     String checklistString = (checklistAdapter == null)? "null": "" + checklistAdapter.getCount(); 
     String tipString = (tipsAdapter == null)? "null": "" + tipsAdapter.getCount(); 
     Toast.makeText(this, checklistString + " : " + tipString, 
      Toast.LENGTH_SHORT).show(); 
    } 

    private void buildLists(){ 
     TextView checklistTitle = (TextView) findViewById(R.id.checklist_title_bar); 
     checklistTitle.setText(getResources().getString(R.string.title_checklist)); 

     ArrayList<String> checklistStrings = new ArrayList<String>(); 
     for(Object guide : selectedQuickGuide.checklist){ 
      checklistStrings.add((String) guide.toString()); 
     } 
     checklistAdapter = new QuickGuideChecklistAdapter(checklistStrings); 
     ListView checklist = (ListView) findViewById(R.id.checklist_list); 
     checklist.setAdapter(checklistAdapter); 
     Log.d(TAG, "List Size - Checklist = " + selectedQuickGuide.checklist.length); 


     TextView tipsTitle = (TextView) findViewById(R.id.tips_title_bar); 
     tipsTitle.setText(getResources().getString(R.string.title_tips)); 

     tipsAdapter = new QuickGuideTipAdapter(selectedQuickGuide.topTips); 
     ListView tips = (ListView) findViewById(R.id.tips_list); 
     tips.setAdapter(tipsAdapter); 
     Log.d(TAG, "List Size - Tips = " + selectedQuickGuide.topTips.length); 

    } 
} 

sub_activity_quick_guide

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/nav_drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     style="@style/default_container" 
     android:layout_margin="15dp" 
     android:orientation="vertical" > 

     <include 
      android:id="@+id/expandable_section" 
      layout="@layout/section_expandable_details" /> 

     <LinearLayout 
      style="@style/default_container" 
      android:orientation="vertical" 
      android:weightSum="2"> 
      <LinearLayout 
       android:id="@+id/checklist_section" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:layout_marginBottom="8dp" 
       android:background="@color/sub_gray" 
       android:onClick="checkAdapterSize" > 

       <View 
        android:layout_width="1dp" 
        android:layout_height="15dp" /> 

       <TextView 
        android:id="@+id/checklist_title_bar" 
        style="@style/default_label_text_view" 
        android:background="@color/random_blue" 
        android:text="@string/loading" /> 

       <ListView 
        android:id="@+id/checklist_list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:divider="@color/off_white" 
        android:dividerHeight="1dp" /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tips_list_section" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:layout_marginTop="7dp" 
       android:background="@color/sub_gray" 
       android:onClick="checkAdapterSize" > 

       <View 
        android:layout_width="1dp" 
        android:layout_height="15dp" /> 

       <TextView 
        android:id="@+id/tips_title_bar" 
        style="@style/default_label_text_view" 
        android:background="@color/random_blue" 
        android:text="@string/loading" /> 

       <ListView 
        android:id="@+id/tips_list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:divider="@color/off_white" 
        android:dividerHeight="1dp" /> 
      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 


    <RelativeLayout 
     android:id="@+id/main_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView 
     android:id="@+id/nav_drawer_list_view" 
     android:layout_width="@dimen/drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@android:color/white" /> 

</android.support.v4.widget.DrawerLayout> 

QuickGuideChecklistAdapter

public class QuickGuideChecklistAdapter extends BaseAdapter{ 

    private ArrayList<String> guides; 
    private List<Boolean> checklistItemTicked = new ArrayList<Boolean>(); 

    public QuickGuideChecklistAdapter(ArrayList<String> guides){ 
     this.guides = guides; 
     for(int i = 0; i < guides.size(); i++) 
      checklistItemTicked.add(false); 
    } 

    @Override 
    public int getCount() { 
     return guides.size(); 
    } 

    @Override 
    public String getItem(int position) { 
     if(position < 0 || position >= guides.size()) 
      throw new IndexOutOfBoundsException("Position is out of range of list with size " + guides.size()); 
     return guides.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     QuickGuideChecklistListItemView itemView = (QuickGuideChecklistListItemView)convertView; 
     if (null == itemView) 
      itemView = QuickGuideChecklistListItemView.inflate(parent); 

     itemView.setText(guides.get(position)); 
     Log.d(getClass().getSimpleName(), "Setting text to " + guides.get(position) + " in position " + position); 

     return itemView; 
    } 
} 

对不起,代码墙,但我已经尝试调整一切,只是似乎无法得到任何工作。因此,正如最后的一点:

  1. 适配器设置,getCount将()返回列表中的项目的有效计数和为了调用checkAdapterSize(点击布局)也会导致吐司与适当的价值。

  2. 第一视图是“膨胀”,但导致的0

  3. 容器布局我的列表“部分”(在sub_activity_quick_guide例如checklist_section)的图高度具有可以通过可见的高度sub_gray背景。

我试着改变高度属性从match_parent到wrap_content,反之亦然,没有运气。我已经尝试明确地为我的列表项目和所有这些好东西声明一个minHeight。我似乎无法得到一个列表行项目实际膨胀。

Screenshot

回答

1

有趣,我怎么总是设法回答弄清楚我自己的问题之后我张贴问题上的SO试图天后:P

反正the issue was in placing my ListViews inside of LinearLayouts with heights determined by weight.我删除了的LinearLayout封装器我的“部分”,并直接将权重应用于我的ListView,瞧...很好。

新的完整的工作活动布局

sub_activity_quick_guide

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/nav_drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     style="@style/default_container" 
     android:layout_margin="15dp" 
     android:orientation="vertical" 
     android:weightSum="2" > 

     <include 
      android:id="@+id/expandable_section" 
      layout="@layout/section_expandable_details" /> 

     <View 
      android:layout_width="1dp" 
      android:layout_height="15dp" /> 

     <TextView 
      android:id="@+id/checklist_title_bar" 
      style="@style/default_label_text_view" 
      android:background="@color/random_blue" 
      android:text="@string/loading" /> 

     <ListView 
      android:id="@+id/checklist_list" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:divider="@color/off_white" 
      android:dividerHeight="1dp" /> 

     <View 
      android:layout_width="1dp" 
      android:layout_height="15dp" /> 

     <TextView 
      android:id="@+id/tips_title_bar" 
      style="@style/default_label_text_view" 
      android:background="@color/random_blue" 
      android:text="@string/loading" /> 

     <ListView 
      android:id="@+id/tips_list" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:divider="@color/off_white" 
      android:dividerHeight="1dp" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/main_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView 
     android:id="@+id/nav_drawer_list_view" 
     android:layout_width="@dimen/drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@android:color/white" /> 

</android.support.v4.widget.DrawerLayout> 

希望它可以帮助别人的未来,不知道权重应该惹它这样的,但显然列表视图不像加权容器。