android:listview中的listview
我正在尝试将listview放置在listviewitem中。内部listview不应该是可滚动的,而是采用显示所有行所需的所有大小。有没有更好的办法?桌子,网格。。。?我现在面临的问题是,内部listview没有占用它所需的空间,所以它被剪切在第一个listitem的末尾。如果我尝试滚动,只有外部listview正在滚动,这正是我想要的 谢谢,我的最终解决方案是android:listview中的listview,android,listview,Android,Listview,我正在尝试将listview放置在listviewitem中。内部listview不应该是可滚动的,而是采用显示所有行所需的所有大小。有没有更好的办法?桌子,网格。。。?我现在面临的问题是,内部listview没有占用它所需的空间,所以它被剪切在第一个listitem的末尾。如果我尝试滚动,只有外部listview正在滚动,这正是我想要的 谢谢,我的最终解决方案是 LinearLayout layout = (LinearLayout) row.findViewById(R.id.LLBroa
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);
}
来自Android文档-:ListView是一个视图组,显示可滚动项目的列表 您并不是真的想要滚动内部列表视图,而是想要滚动外部列表视图。但是,我认为内部listview可能因其包含的元素数量而异 您可以使用
- ,看看这个或看看
//访问线性布局
LinearLayout布局=(LinearLayout)findViewById(R.id.layout);
//加载行的xml结构
View child=GetLayoutFlater().充气(R.layout.row);
//现在像使用listview一样填充该行
//e、 g.(TextView)child.findViewById(。。。
...
//然后再加上它
layout.addView(子级);
您应该将线性布局保存在视图保持架中(请参阅)。我认为只有当当前行的内部行数小于重用行数时,removeAllViews()
才是必需的,因此我还将保存视图保持架中的行数
如果内部行的最大数量不是很高,您也可以考虑将它们缓存在视图保持架中,以避免充气和FindByViewWid(比方说在ArrayList中)。我尝试创建这个精确的结构(在
列表视图中的列表视图)同样的问题是,它只显示内部列表视图的第一项。我通过将内部列表的布局高度从match\u parent
更改为一组dp
来修复它
它似乎完全按照我所希望的那样工作。我的应用程序中也有同样的问题,但我需要使用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();
也许有人会发现我的解决方案很有用。
它基于@ChrLipp answer并使用线性布局
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"
/>
公共类NotScrollableListView扩展了LinearLayout{
专用列表适配器;
私有DataChangeObserver DataChangeObserver;
专用可拉丝分隔器;
私人内部分割权;
private List reusableview=new ArrayList();
公共NotScrollableListView(上下文){
超级(上下文);
}
public NotScrollableListView(上下文、属性集属性){
超级(上下文,attrs);
设置属性(attrs);
}
公共NotScrollableListView(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
设置属性(attrs);
}
公共ListAdapter getAdapter(){
返回适配器;
}
public void setAdapter(ListAdapter适配器){
if(this.adapter!=null&&dataChangeObserver!=null){
this.adapter.unregisterDataSetObserver(dataChangeObserver);
}
this.adapter=适配器;
}
@凌驾
受保护的无效数据附加到DOWINDOW(){
super.onAttachedToWindow();
if(适配器!=null){
dataChangeObserver=新的dataChangeObserver();
adapter.registerDataSetObserver(dataChangeObserver);
填充内容();
}
}
@凌驾
受保护的无效onDetachedFromWindow(){
super.onDetachedFromWindow();
if(适配器!=null){
adapter.unregisterDataSetObserver(dataChangeObserver);
dataChangeObserver=null;
}
}
私有void fillContents(){
//清除内容
此参数为.removeAllViews();
final int count=adapter.getCount();//项计数
final int reusableCount=reusableViews.size();//缓存的可重用视图的计数
//切割器特性的计算
ViewGroup.LayoutParams dividerLayoutParams=null;
if(除法器!=null&÷rHeight>0){
dividerLayoutParams=newviewgroup.LayoutParams(ViewGroup.LayoutParams.MATCH_父项,dividerHeight);
}
//添加项目
for(int i=0;i=reusableCount){//缓存视图
可重用视图。添加(视图);
}
addView(视图);
//加法除法器
if(除法器!=null&÷rHeight>0){
如果(i<计数-1){
ImageView dividerView=新的ImageView(getContext());
dividerView.setImageDrawable(分割器);
dividerView.setLayoutParams(dividerLayoutParams);
addView(dividerView);
}
}
}
}
私有void集合属性(AttributeSet属性){
int[]divideratters=newint[]{android.R.attr.divider,android.R.attr.dividerHeight};
TypedArray a=getContext()。获取样式属性(属性、分隔符);
试一试{
分隔器=a.getDrawable(0);
dividerHeight=a.getDimensionPixelSize(1,0);
}最后{
a、 回收();
}
设置方向(垂直);
}
私有类DataChangeObserver扩展了DataSetObserver{
@凌驾
更改后的公共无效(){
super.onChanged();
填充内容();
}
@凌驾
未验证的公共无效(){
super.onInvalidated();
填充内容();
}
}
}
@试试这个嵌套类
这适用于在listView
内滚动listView
或在同一活动中滚动2listView
s
嵌套列表视图:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
public class NestedListView extends ListView implements OnTouchListener, OnScrollListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}
导入androi
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
public class NestedListView extends ListView implements OnTouchListener, OnScrollListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}