简体   繁体   中英

Border of cells in a TableView inside a ScrollView

I have a very strange problem; i hope somebody knows what this is. I build a table in a scrollable view, with a 2px thick cells border. On a tablet (Samsung galaxy 10.1) it's perfect, while on galaxy tab 2 it 'stops' drawing the borders only on the visible part of the scroll view. I attach 2 pictures to show the difference 三星Galaxy Tab 10.1

三星银河标签2(向右滚动一点)

Here is the code the java part:

  @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            scrollView1=(HorizontalScrollView) firstView.findViewById(R.id.horizontalScrollView1);
            //position the scroller to the present time
            scrollView1.post(new Runnable() { 
                public void run() { 
                    if((Calendar.getInstance().get(Calendar.HOUR_OF_DAY)>3))
                     scrollView1.scrollTo(CELL_WIDTH*((Calendar.getInstance().get(Calendar.HOUR_OF_DAY))-3), 0);
                } 
        });

             headerPaint = new Paint();
             headerPaint.setStyle(Paint.Style.STROKE);
             headerPaint.setColor(Color.GRAY);
             headerPaint.setStrokeWidth(2);

             cellPaint = new Paint();
             cellPaint.setStyle(Paint.Style.STROKE);
             cellPaint.setColor(Color.GRAY);
             cellPaint.setStrokeWidth(2);

            textViewData.setText(formatter.format(dateToShow.getTime()));
            imageButtonDateAfter.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Calendar oggi = Calendar.getInstance();
                    oggi.set(Calendar.HOUR_OF_DAY, 23);
                    oggi.set(Calendar.MINUTE, 59);
                    dateToShow.add(Calendar.DAY_OF_MONTH, 1);   
                }
            });
            loadElementiInTable();
            return firstView;
        }

//for space reasons i show you only the first row code (the hours of the day)
 private void loadElementiInTable() {
        tableLayoutElementi.removeAllViews();
        tableLayoutHeader.removeAllViews();
        ArrayList<Integer> orari = new ArrayList<Integer>();
        orari = loadListOrari();
        listPasti = new ArrayList<Pasto>();
        listGlicemie = new ArrayList<Glicemia>();
        listBoli = new ArrayList<Administration>();
        TableRow hoursRow=new TableRow(getActivity().getApplicationContext());
        TableRow hoursHeaderRow=new TableRow(getActivity().getApplicationContext());
        TableRow hoursSepRow = new TableRow(getActivity().getApplicationContext());
        TextView legendanew=new TextView(getActivity().getApplicationContext());

        legendanew.setText("Campo");
        legendanew.setWidth(HEADER_WIDTH);
        legendanew.setGravity(Gravity.CENTER);
        hoursHeaderRow.addView(legendanew);

        for (final Integer j : orari) {
        TextView ora1=new TextView(getActivity().getApplicationContext()){
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Rect rect = new Rect();
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(Color.GRAY);
                paint.setStrokeWidth(2);
                getLocalVisibleRect(rect);
                canvas.drawRect(rect, paint);       
            }

        };
        ora1.setTextColor(Color.DKGRAY);
        ora1.setGravity(Gravity.CENTER);
        TextView sepOra=new TextView(getActivity().getApplicationContext());
        ora1.setText(j+":00");
        if(j==Calendar.getInstance().get(Calendar.HOUR_OF_DAY)){
            ora1.setBackgroundColor(Color.YELLOW);
        }
        ora1.setWidth(CELL_WIDTH);
        hoursRow.addView(ora1);
        //hoursRow.addView(sepOra);
        }

        TableRow glicemiaRow = new TableRow(getActivity().getApplicationContext());
        TableRow glicemiaHeaderRow = new TableRow(getActivity().getApplicationContext());
        TextView glicemiaLabel=new TextView(getActivity().getApplicationContext()){
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Rect rect = new Rect();
                getLocalVisibleRect(rect);
                canvas.drawRect(rect, headerPaint);       
            }

        };
        glicemiaLabel.setText("Glicemia");
        formatAsHeader(glicemiaLabel);  
        glicemiaHeaderRow.addView(glicemiaLabel);
        TextView glicemiaTuttoIlGiorno=new TextView(getActivity().getApplicationContext()){
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Rect rect = new Rect();
                getLocalVisibleRect(rect);
                canvas.drawRect(rect, cellPaint);       
            }
        };
        if(showTuttoIlGiorno){
        glicemiaTuttoIlGiorno.setText(EMPTY_CELL);
        formatAsTuttoIlGiorno(glicemiaTuttoIlGiorno);
        glicemiaHeaderRow.addView(glicemiaTuttoIlGiorno);
        }

        tableLayoutElementi.setStretchAllColumns(true);
        legendanew.setWidth(100);
        noteRow.setBaselineAligned(false);
        noteHeaderRow.setBaselineAligned(false);
        tableLayoutElementi.addView(noteRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(noteHeaderRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        boloRow.setBaselineAligned(false);
        boloHeaderRow.setBaselineAligned(false);
        tableLayoutElementi.addView(boloRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(boloHeaderRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        pastoRow.setBaselineAligned(false);
        pastoHeaderRow.setBaselineAligned(false);
        tableLayoutElementi.addView(pastoRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(pastoHeaderRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        glicemiaRow.setBaselineAligned(false);
        glicemiaHeaderRow.setBaselineAligned(false);
        tableLayoutElementi.addView(glicemiaRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(glicemiaHeaderRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(hoursHeaderRow, 0, new TableLayout.LayoutParams(100, heightColonna));
        tableLayoutElementi.addView(hoursRow, 0, new TableLayout.LayoutParams(100, heightColonna));
    }
    }

the xml file (only the table part):

 <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10" >

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="1080dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

            <TableLayout
                android:id="@+id/tableLayoutElementi"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@color/white" >
            </TableLayout>

 </LinearLayout>

            </HorizontalScrollView>

        <LinearLayout
            android:id="@+id/headerleft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/tableLayoutHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />

                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />

                </TableRow>

                <TableRow
                    android:id="@+id/tableRow3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />
                </TableRow>
                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </RelativeLayout>

I didn't understand why this is happening, but i solved removing these textview declarations:

TextView cell = new TextView(getActivity().getApplicationContext()) {
                @Override
                protected void onDraw(Canvas canvas) {
                    super.onDraw(canvas);
                    Rect rect = new Rect();
                    getLocalVisibleRect(rect);
                    canvas.drawRect(rect, cellPaint);       
                }
            };

and putting this instead:

TextView cell = new TextView(getActivity().getApplicationContext());
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.borded_cell));

where borded_cell is an xml file i put in res/drawable:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

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