我正在尝试合并2个列表"部分"通过高度权重进入LinearLayout。我遇到的问题是,虽然是"部分"的容器。显示正确的高度(背景为sub_gray
),并且适配器计数正确,我的实际列表行的高度为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);
}
}
完整列表
现在我使用上面的代码并在我的活动中使用它来创建一个列表(回想一下;我的活动中实际上有两个列表,构造方式相似,并且都导致行高为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;
}
}
对于代码墙感到抱歉,但我已经尝试过调整所有内容,但似乎无法正常工作。所以最后一点:
设置适配器,getCount()返回列表中有效的项目数,然后单击布局以调用checkAdapterSize()也会生成具有正确值的Toast。
< / LI>第一个视图是&#34;膨胀&#34;但结果是视图高度为0.
我的列表&#34;部分&#34;的容器布局(例如sub_activity_quick_guide中的checklist_section)具有sub_gray背景可以看到的高度。
我尝试将高度属性从match_parent更改为wrap_content,反之亦然,没有运气。我已经尝试为我的列表项和所有好东西明确声明minHeight。我似乎无法获得列表行项目来实际膨胀。
答案 0 :(得分:1)
有趣的是,在经过几天的尝试后,我总是设法找出自己问题的答案:P
无论如何,the issue was in placing my ListViews inside of LinearLayouts with heights determined by weight.
我删除了我的“部分”的LinearLayout包装器,并将权重直接应用到我的ListViews,瞧...好的去。
新的完整工作活动布局
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>
希望它能帮助将来的某个人,没有意识到权重会像那样惹恼它,但显然ListViews不喜欢加权容器。