2017-04-10 105 views
1

我是新來的android.I有表格佈局的XML和動態添加行,我試圖把動態行放入scrollview.please任何人都可以幫助如何做。如何將動態滾動視圖添加到動態表格行

這裏我的xml:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 


    <include layout="@layout/header" 
     android:id="@+id/header" /> 
<TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tv_message" 
     android:textSize="18sp" 
     android:textAlignment="center" 
     android:background="#deb8" 
     android:layout_below="@id/header" 
     android:textColor="#000000" 
     android:text="Please Confirm the Quote and check for Errors "/> 


    <ScrollView 
     android:id="@+id/ScrollView03" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_message" > 
<TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_message" 
     android:id="@+id/table_orderfulfillment" 
     android:stretchColumns="*"> 


       </TableLayout> 
    </ScrollView> 

</Relative Layout> 
+0

您可以使用RecyclerView作爲網格來顯示信息並自定義項目佈局,而不是使用TableLayout。在這裏看到很好的例子:http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html – Alexandre

+0

這裏也是一個很好的例子:http://stackoverflow.com/questions/40587168/simple-android-網格示例-使用-recyclerview與 - gridlayoutmanager樣的 – Alexandre

回答

0

好吧,我試圖通過動態字符串數組

這裏來填充Tablelayout是代碼:也被添加滾動視圖。

public class MainActivity extends Activity 
{ 


    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6", 
         "Row 7" 
         }; 
     String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4", 
          "COLUMN5", "COLUMN6" 
          }; 
     int rl=row.length; 
     int cl=column.length; 

     ScrollView sv = new ScrollView(this); 
     TableLayout tableLayout = createTableLayout(row, column,rl, cl); 
     HorizontalScrollView hsv = new HorizontalScrollView(this); 

     hsv.addView(tableLayout); 
     sv.addView(hsv); 
     setContentView(sv); 

    } 

    private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount) 
    { 
     // 1) Create a tableLayout and its params 
     TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(); 
     TableLayout tableLayout = new TableLayout(this); 
     tableLayout.setBackgroundColor(Color.BLACK); 

     // 2) create tableRow params 
     TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(); 
     tableRowParams.setMargins(1, 1, 1, 1); 
     tableRowParams.weight = 1; 

     for (int i = 0; i < rowCount; i++) 
     { 
      // 3) create tableRow 
      TableRow tableRow = new TableRow(this); 
      tableRow.setBackgroundColor(Color.BLACK); 

      for (int j= 0; j < columnCount; j++) 
      { 
       // 4) create textView 
       TextView textView = new TextView(this); 
       // textView.setText(String.valueOf(j)); 
       textView.setBackgroundColor(Color.WHITE); 
       textView.setGravity(Gravity.CENTER); 

       String s1 = Integer.toString(i); 
       String s2 = Integer.toString(j); 
       String s3 = s1 + s2; 
       int id = Integer.parseInt(s3); 
       Log.d("TAG", "-___>"+id); 
       if (i ==0 && j==0) 
       { 
        textView.setText("0==0"); 
       } 
       else if(i==0) 
       { 
        Log.d("TAAG", "set Column Headers"); 
        textView.setText(cv[j-1]); 
       } 
       else if(j==0) 
       { 
        Log.d("TAAG", "Set Row Headers"); 
        textView.setText(rv[i-1]); 
       } 
       else 
       { 
        textView.setText(""+id); 
        // check id=23 
        if(id==23) 
        { 
         textView.setText("ID=23"); 

        } 
       } 

       // 5) add textView to tableRow 
       tableRow.addView(textView, tableRowParams); 
      } 

      // 6) add tableRow to tableLayout 
      tableLayout.addView(tableRow, tableLayoutParams); 
     } 

     return tableLayout; 
    } 
} 

輸出:

enter image description here

0

我使用此代碼加載上滾動更多的數據!

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 

        @Override 
        public void onScrollStateChanged(RecyclerView recyclerView, 
                int newState) { 

         super.onScrollStateChanged(recyclerView, newState); 

         // If scroll state is touch scroll then set userScrolled 
         // true 
         if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { 
          userScrolled = true; 

         } 

        } 

        @Override 
        public void onScrolled(RecyclerView recyclerView, int dx, 
              int dy) { 
         super.onScrolled(recyclerView, dx, dy); 

         // Here get the child count, item count and visibleitems 
         // from layout manager 

         visibleItemCount = mLayoutManager.getChildCount(); 
         totalItemCount = mLayoutManager.getItemCount(); 
         pastVisiblesItems = mLayoutManager 
           .findFirstVisibleItemPosition(); 

         // Now check if userScrolled is true and also check if 
         // the item is end then update recycler view and set 
         // userScrolled to false 
         if (userScrolled 
           && (visibleItemCount + pastVisiblesItems) == totalItemCount) { 
          userScrolled = false; 

          RefreshData(oldestPostId); 


          // implementScrollListener(); 
         } 

        } 

       });