不滚动的Android ListView? [英] Android ListView that does not scroll?
问题描述
我正在尝试制作一个类似于 android 市场的布局......在评论下说有什么似乎是一个 ListView
但它不滚动(整个页面滚动但不是评论).我不确定它是否甚至是一个 ListView,但我想要一些看起来像列表视图的东西(即.有那些分隔条和什么不是但不可滚动).有人建议使用 LinearLayout
而不是 ListView
但我也希望项目可点击并打开一个新活动.请帮忙?
I'm trying to make a layout that is something similar to how the android market is...where say under comments there is what appears to be a ListView
but it does not scroll (the whole page scroll but not the comments). I'm not sure if its even a ListView but I want something that looks like the list view (ie. have those divider bars and what not but NOT SCROLLABLE). There are people suggesting to use a LinearLayout
instead of a ListView
but I also want the items to be clickable and open a new activity. Please help?
我目前的布局树是这样的
My current layout tree is like so
<LinearLayout>
<ScrollView>
<RelativeLayout>
我希望将内容放在 RelativeLayout
中.
I am looking to put content inside the RelativeLayout
.
推荐答案
正如在这个 video 从未将 ListView 放在滚动视图中.如果您的列表不应该滚动,请使用类似线性布局的 ViewGroup,并在您的代码循环中将所有项目添加到此 ViewGroup.如果您希望整行可点击,您必须使用另一个 ViewGroup 作为每行的根节点,并将 OnClickListener 添加到此视图.
As explained by the programmers that did the listView in this video from GoogleIo never put a ListView inside a scroll View. If your list should not scroll use a ViewGroup like a linear Layout and add all the items to this ViewGroup in a loop in your code. If you want a whole row to be clickable you have to use another ViewGroup as the root node for each row and add the OnClickListener to this View.
示例代码:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int current = 0; current < itemCount; current++) {
View view = inflater.inflate(R.layout.layout_id, parent, false);
//initialize the view
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CLASS_TO_START)
startActivity(intent);
}
});
viewGroup.addView(view);
if (current < itemCount - 1) {
inflater.inflate(R.layout.line, viewGroup);
}
}
此代码将为您拥有的每个项目生成一个视图并将其放入视图组.在除最后一项之外的每一项之后,它还会向 viewGroup 添加一个分隔符.
This code will generate one View for every item that you have and put it into the viewGroup. After every item but the last it will also add a divider to the viewGroup.
这篇关于不滚动的Android ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!