我对这个完全感到难过。我有三个不同的列表需要在屏幕上显示。列表完全可能会延伸到屏幕的下边缘,所以我需要滚动。
我尝试将ScrollView
与LinearLayout
孩子一起使用,并将ListViews
放入LinearView
,但所有ListViews
锁定到带滚动条的固定高度。使用其他类型的布局意味着不滚动。
有没有人有任何建议,或者我是否需要以编程方式将列表项添加到某些布局中并希望最好?
答案 0 :(得分:12)
将touched
视图中的触摸事件转发到其他视图。您的所有视图也将同步展开/折叠。
OnTouchListener mOnTouch = new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
MotionEvent newEvent = MotionEvent.obtain(event);
switch(event.getAction()){
case MotionEvent.ACTION_MOVE:
if(mTouched == null){
mTouched = v;
}
mMovingFlag = true;
break;
case MotionEvent.ACTION_UP:
if(mMovingFlag==false){
newEvent.setAction(MotionEvent.ACTION_CANCEL);
}
mMovingFlag = false;
break;
default:
mMovingFlag = false;
if(mTouched != null && mTouched.equals(v)){
mTouched = null;
}
break;
}
if(mTouched == null || mTouched.equals(v)){
int items = mLayoutWithListViews.getChildCount();
for(int list=0; list<items; list++){
AbsListView listView =mLayoutWithListViews.getChildAt(list));
if(listView != v){
listView.onTouchEvent(newEvent);
}
}
}
return false;
}
};
答案 1 :(得分:6)
我还没有这样做,但我一直在考虑,因为我可能需要这样做。我可以想到三个选项:只使用一个列表和所有列表的内容。您可以创建一个ListAdapter来执行该操作并插入某种标头。这可能是非常可重复使用的东西。
另一个想法是列出清单。但这听起来像是在寻找麻烦。
答案 2 :(得分:6)
我有一个类似的问题,除了我有两个GridView和一个ListView滚动。 我通过手动设置ListViews和GridView的高度来解决它:
ListAdapter listAdapter = listView.getAdapter();
int rows = listAdapter.getCount() / columns;
int height = 60 * rows; // Insert the general cell height plus the dividers.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
希望我能帮助别人。
答案 3 :(得分:3)
虽然这个答案是针对滚动视图而不是列表视图的,但问题是相同的,这个解决方案对我来说效果很好。滚动左侧滚动视图将滚动右侧滚动视图,反之亦然。
public class MainActivity extends Activity implements OnTouchListener {
private ScrollView scrollViewLeft;
private ScrollView scrollViewRight;
private static String LOG_TAG = MainActivity.class.getName();
private boolean requestedFocus = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollViewLeft = (ScrollView) findViewById(R.id.scrollview_left);
scrollViewRight = (ScrollView) findViewById(R.id.scrollview_right);
scrollViewLeft.setOnTouchListener(this);
scrollViewRight.setOnTouchListener(this);
scrollViewLeft.setFocusableInTouchMode(true);
scrollViewRight.setFocusableInTouchMode(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.d(LOG_TAG, "--> View, event: " + view.getId() + ", " + motionEvent.getAction() + ", " + view.isFocused());
Log.d(LOG_TAG, "--> " + scrollViewLeft.isFocused() + ", " + scrollViewRight.isFocused());
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN && requestedFocus == false){
view.requestFocus();
requestedFocus = true;
}else if (motionEvent.getAction() == MotionEvent.ACTION_UP){
requestedFocus = false;
}
if (view.getId() == R.id.scrollview_left && view.isFocused()){
scrollViewRight.dispatchTouchEvent(motionEvent);
}else if (view.getId() == R.id.scrollview_right && view.isFocused()){
scrollViewLeft.dispatchTouchEvent(motionEvent);
}
return super.onTouchEvent(motionEvent);
}
}
答案 4 :(得分:1)
ListAdapter listAdapter = listView.getAdapter();
int rows = listAdapter.getCount() / columns;
int height = 60 * rows; // Insert the general cell height plus the dividers.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
答案 5 :(得分:-1)
使用expandableListView。 在这里查看我的答案以获得类似的问题: Android ScrollView layout problem