ViewGroup を拡張しました。カスタム ViewGroup を ScrollView に入れ、一連のコンテンツを動的に追加しましたが、スクロールしません。画面からはみ出すコンテンツは見ることができません。以下の xml ファイルで、Scrollview を削除すると、次のようになります。
<ScrollView
android:id="@+id/myalltags_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
それは働くことができます。
私は何が欠けていますか?ViewGroup が ScrollView と互換性を持つようにするにはどうすればよいですか?
package com.android.picker;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyViewGroup extends ViewGroup {
private final static String TAG = "MyViewGroup";
public final static int VIEW_MARGIN=2;
private final static int MAX_TAG_LINES=3;
private Button lastMoreButton;
public int totalHeight=0;
public MyViewGroup(Context context) {
super(context);
Log.i(TAG,"myViewgroup");
}
public void fullTags(Context context,List<String> rs,Button button){
for(String s:rs){
Button v=new Button(context);
v.setText(s);
this.addView(v);
};
if(button!=null){
this.addView(button);
this.lastMoreButton=button;
}
}
public void fullTags(Context context,List<String> rs){
this.fullTags(context,rs,null);
}
public MyViewGroup(Context context,AttributeSet attributeSet){
super(context,attributeSet);
Log.i(TAG,"myViewgroupattributeSet");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(TAG, "widthMeasureSpec = "+widthMeasureSpec+" heightMeasureSpec"+heightMeasureSpec);
for (int index = 0; index < getChildCount(); index++) {
final View child = getChildAt(index);
// measure
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
totalHeight+=child.getMeasuredHeight();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean arg0, int l, int t, int r, int b) {
Log.d(TAG, "changed = "+arg0+" left = "+l+" top = "+t+" right = "+r+" botom = "+b);
final int count = getChildCount();
int row=0;// which row lay you view relative to parent
int lengthX=l; // right position of child relative to parent
int lengthY=t; // bottom position of child relative to parent
for(int i=0;i<count;i++){
final View child = this.getChildAt(i);
Log.i("aa",((Button)child).getText().toString());
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
lengthX+=width+VIEW_MARGIN;
//if it can't drawing on a same line , skip to next line
if(lengthX>r){
lengthX=width+VIEW_MARGIN+l;
row++;
}
lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+t;
child.layout(lengthX-width, lengthY-height, lengthX, lengthY);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/myalltags_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<com.android.picker.MyViewGroup android:id="@+id/myalltags"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.android.picker.MyViewGroup>
</ScrollView>
</LinearLayout>
package com.android.picker;
import java.util.ArrayList;
import java.util.List;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;
public class MyTagsDialog extends Dialog{
private Context context;
private MyViewGroup myAllTagsView;
public MyTagsDialog(Context context) {
super(context);
this.context=context;
this.setContentView(R.layout.mytagslayout);
initView();
initData();
}
private void initData() {
List<String> rs=new ArrayList<String>();
rs.add("test1");
rs.add("test2");
rs.add("test3");
rs.add("test4");
rs.add("test5");
rs.add("test6");
rs.add("test7");
rs.add("test8");
rs.add("test9");
rs.add("test10");
rs.add("test11");
rs.add("test12");
myAllTagsView.fullTags(context,rs);
// MyViewGroupPackage mypackages=new MyViewGroupPackage();
// mypackages.hasmore=true;
// mypackages.imems=rs;
// myAllTagsView.fullTags(context, rs);
// ViewGroup.LayoutParams params = myAllTagsView.getLayoutParams();
// params.height = myAllTagsView.totalHeight + 100;
// Log.i("cccc",""+params.height);
// myAllTagsView.setLayoutParams(params);
}
private void initView() {
myAllTagsView=(MyViewGroup)this.findViewById(R.id.myalltags);
}
}