我正在尝试将listview放在listviewitem中.内部列表视图不应该是可滚动的,而是需要显示其所有行的所需大小.有更好的方法吗?表,网格,......?我现在面临的问题是内部列表视图没有占用它所需的空间,因此它在第一个列表项的末尾被切断.如果我尝试滚动,只是外部列表视图滚动这正是我想要的.
谢谢,我的最终解决方案是
LinearLayout layout = (LinearLayout) row.findViewById(R.id.LLBroadcasts);
layout.removeAllViews();
for (Item b : bs.getItems()) {
View child = _inflater.inflate(R.layout.item_row, null);
TextView tvTitle = (TextView) child.findViewById(R.id.TVItemTitle);
tvTitle.setText(b.getTitle());
TextView tvDesc = (TextView) child.findViewById(R.id.TVItemDescription);
tvDesc.setText(b.getDescription());
layout.addView(child);
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*ipp 38
从Android文档 - Listview:ListView是一个显示可滚动项列表的视图组
你真的不想滚动内部列表视图,你想滚动外部列表视图.但是我认为内部列表视图可能会因其包含的元素数量而异.
您可以使用a而不是内部列表视图
对于线性布局(一些示例代码):
// access your linear layout
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
// load the xml structure of your row
View child = getLayoutInflater().inflate(R.layout.row);
// now fill the row as you would do with listview
//e.g. (TextView) child.findViewById(...
...
// and than add it
layout.addView(child);
Run Code Online (Sandbox Code Playgroud)
您应该将线性布局保存在视图支架中(请参阅查看支架图案).我认为removeAllViews()仅当当前行的内行比重用行少时才需要,所以我也会保存视图持有者中的行数.
如果内部行的最大数量不是很高,您还可以考虑在视图持有者中缓存它们以避免膨胀和findByViewId(假设在ArrayList中).
也许有人会发现我的解决方案很有用.它基于@ChrLipp的答案并使用LinearLayout.
public class NotScrollableListView extends LinearLayout {
private ListAdapter adapter;
private DataChangeObserver dataChangeObserver;
private Drawable divider;
private int dividerHeight;
private List<View> reusableViews = new ArrayList<>();
public NotScrollableListView(Context context) {
super(context);
}
public NotScrollableListView(Context context, AttributeSet attrs) {
super(context, attrs);
setAttributes(attrs);
}
public NotScrollableListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setAttributes(attrs);
}
public ListAdapter getAdapter() {
return adapter;
}
public void setAdapter(ListAdapter adapter) {
if (this.adapter != null && dataChangeObserver != null) {
this.adapter.unregisterDataSetObserver(dataChangeObserver);
}
this.adapter = adapter;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (adapter != null) {
dataChangeObserver = new DataChangeObserver();
adapter.registerDataSetObserver(dataChangeObserver);
fillContents();
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (adapter != null) {
adapter.unregisterDataSetObserver(dataChangeObserver);
dataChangeObserver = null;
}
}
private void fillContents() {
// clearing contents
this.removeAllViews();
final int count = adapter.getCount(); // item count
final int reusableCount = reusableViews.size(); // count of cached reusable views
// calculating of divider properties
ViewGroup.LayoutParams dividerLayoutParams = null;
if (divider != null && dividerHeight > 0) {
dividerLayoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dividerHeight);
}
// adding items
for (int i = 0; i < count; i++) {
// adding item
View converView = null;
if (i < reusableCount) { // we have cached view
converView = reusableViews.get(i);
}
View view = adapter.getView(i, converView, this);
if (i >= reusableCount) { // caching view
reusableViews.add(view);
}
addView(view);
// adding divider
if (divider != null && dividerHeight > 0) {
if (i < count - 1) {
ImageView dividerView = new ImageView(getContext());
dividerView.setImageDrawable(divider);
dividerView.setLayoutParams(dividerLayoutParams);
addView(dividerView);
}
}
}
}
private void setAttributes(AttributeSet attributes) {
int[] dividerAttrs = new int[]{android.R.attr.divider, android.R.attr.dividerHeight};
TypedArray a = getContext().obtainStyledAttributes(attributes, dividerAttrs);
try {
divider = a.getDrawable(0);
dividerHeight = a.getDimensionPixelSize(1, 0);
} finally {
a.recycle();
}
setOrientation(VERTICAL);
}
private class DataChangeObserver extends DataSetObserver {
@Override
public void onChanged() {
super.onChanged();
fillContents();
}
@Override
public void onInvalidated() {
super.onInvalidated();
fillContents();
}
}
}
<com.sample.ui.view.NotScrollableListView
android:id="@+id/internalList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider_color"
android:dividerHeight="@dimen/list_divider_width"
/>
Run Code Online (Sandbox Code Playgroud)
小智 6
我在我的应用程序中遇到同样的问题,但我需要使用ListView,因为它是一个共享项目,我不想复制相同的组件.所以..我只是以编程方式修改内部ListView的大小以显示所有行和..瞧!问题解决了:
ViewGroup.LayoutParams layoutParams = innerListView.getLayoutParams();
layoutParams.height = (int) context.getResources().getDimension(R.dimen.rowheight) * innerListView.getCount();
innerListView.setLayoutParams(layoutParams);
CustomAdapter adapter = new CustomAdapter(context, blabla..);
innerListView.setAdapter(adapter);
rowListView.invalidate();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51246 次 |
| 最近记录: |