简体   繁体   中英

Custom view inside Horizontal scroll view not scrolling

i am working on a custom view. when i put custom view inside Horizontal scroll view the Horizontal scroll is not working.I think i have made mistake in writing MeasureSpec but i dont know how to write a MeasureSpec for a custom view

my view class code like this

public class Makecell extends ViewGroup{
    private int line_height;  
    private int line_width;
    Context mContext;
    subview sub;
    int left,right,top,bottom;
    MainActivity main;
    public Makecell(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public Makecell(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

         final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
            int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();

            final int count = getChildCount();
            int line_height = 0;
            int line_width=0;

            int xpos = getPaddingLeft();  
            int ypos = getPaddingTop();
            for (int i = 0; i < count; i++) {
                 final View child = getChildAt(i);
                 if (child.getVisibility() != GONE) {

                     child.measure(
                             MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED),
                             MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));

                     final int childw = child.getMeasuredWidth();
                     final int childh = child.getMeasuredHeight();
                     line_height = Math.max(line_height, child.getMeasuredHeight());
                     line_width=Math.max(line_width, child.getMeasuredWidth());
                     if (xpos + childh > height) {
                         xpos = getPaddingLeft();
                         ypos += line_width; 
                     }
                     xpos += childw;
                 }
            }

            this.line_width=line_width;


    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

          final int count = getChildCount();
          final int width = r - l;
          final int hight = b - t;  
          int xpos = 0;
          int ypos = 0;

          for (int i = 0; i < count; i++) {
              final View child = getChildAt(i);
              if (child.getVisibility() != GONE) {
                  final int childw = child.getMeasuredWidth();
                  final int childh = child.getMeasuredHeight();
//                
                  if (xpos + childh > hight) {
                      xpos = getPaddingLeft();
                      ypos += line_width;


                  }
                child.layout(ypos, xpos,ypos + childw, xpos + childw );
                xpos += childh;

              }
          }
    }
}

and xml look like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
   <HorizontalScrollView 
       android:layout_height="match_parent"
       android:layout_width="match_parent"
       android:fillViewport="true"
       android:id="@+id/rootlin"
       > 
   </HorizontalScrollView>
</RelativeLayout>

adding view in code look like

cell=new Makecell(getApplicationContext());
        mainlin=(HorizontalScrollView)findViewById(R.id.rootlin);
        LinearLayout lin=new LinearLayout(this);
        lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
        lin.setOrientation(LinearLayout.HORIZONTAL);
        for(int i=0;i<100;i++)
        {
            text=new TextView(this);
            text.setText("Text "+i);
            text.setTextSize(15);
            text.setBackgroundColor(Color.CYAN);
            text.setWidth(screenwidth/2);
            cell.addView(text);
        }
        lin.addView(cell);
        mainlin.addView(lin);

can any one help me to find me where i done a mistakes

Thank's in Advance..

Both of them is worked:

in java:

    LinearLayout lin=new LinearLayout(this);
    lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
    lin.setOrientation(LinearLayout.HORIZONTAL);
    lin.setHorizontalScrollBarEnabled(true);

and in xml file:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scrollbars="horizontal"
      android:orientation="horizontal"
      tools:context=".MainActivity" >
   </LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM