2016-07-22 49 views
2

我该如何改变与其他人的地方蓝色列,像在最右边的蓝色列?请参阅this screenshot如何交换表中的列?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_horizontal" 
    android:id="@+id/fillable_area"> 

    <TableLayout 
     android:id="@+id/table_header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center_horizontal" 
      > 
      <TableLayout 
       android:id="@+id/fixed_column" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 

      <HorizontalScrollView 
       android:id="@+id/scroller" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 

       <TableLayout 
        android:id="@+id/scrollable_part" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/> 
      </HorizontalScrollView> 


     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

我的代码:

package com.example.freez_t00; 

import java.util.ArrayList; 

import android.R.layout; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.HorizontalScrollView; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TableRow.LayoutParams; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    private boolean showSummaries; 

    private TableLayout summaryTable; 
    private TableLayout frozenTable; 
    private TableLayout contentTable; 
    private Button backButton ; 
    private HorizontalScrollView hor; 
     private TextView recyclableTextView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_main); 
      TableRow.LayoutParams wrapWrapTableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      int[] fixedColumnWidths = new int[]{20, 20,20, 20, 20}; 
      int[] scrollableColumnWidths = new int[]{20, 20, 20, 30, 30}; 
      int fixedRowHeight = 50; 
      int fixedHeaderHeight = 60; 
      TableRow row = new TableRow(this); 
      //header (fixed vertically) 
      TableLayout header=(TableLayout)findViewById(R.id.table_header); 
      row.setLayoutParams(wrapWrapTableRowParams); 
      row.setGravity(Gravity.CENTER); 
      row.setBackgroundColor(Color.YELLOW); 
      row.addView(makeTableRowWithText("col1", fixedColumnWidths[0],fixedHeaderHeight)); 
      row.addView(makeTableRowWithText("col2", fixedColumnWidths[1],fixedHeaderHeight)); 
      row.addView(makeTableRowWithText("col3", fixedColumnWidths[2],fixedHeaderHeight)); 
      row.addView(makeTableRowWithText("col4", fixedColumnWidths[3],fixedHeaderHeight)); 
      row.addView(makeTableRowWithText("col5", fixedColumnWidths[4],fixedHeaderHeight)); 
      header.addView(row); 
      hor = (HorizontalScrollView)findViewById(R.id.scroller); 
      hor.postDelayed(new Runnable(){ 
       public void run() { 
        hor.fullScroll(HorizontalScrollView.FOCUS_RIGHT); 
       } 
      }, 1L); 

      TableLayout fixedColumn = (TableLayout) findViewById(R.id.fixed_column); 
      //rest of the table (within a scroll view) 
      TableLayout scrollablePart = (TableLayout) findViewById(R.id.scrollable_part); 

      for(int i = 0; i < 10; i++) { 

       TextView fixedView = makeTableRowWithText("row number " + i, scrollableColumnWidths[0], fixedRowHeight); 

       fixedView.setBackgroundColor(Color.BLUE); 

       fixedColumn.addView(fixedView); 

       row = new TableRow(this); 
       row.setLayoutParams(wrapWrapTableRowParams); 
       row.setGravity(Gravity.CENTER); 
       row.setBackgroundColor(Color.WHITE); 
       row.addView(makeTableRowWithText("value 2", scrollableColumnWidths[1], fixedRowHeight)); 
       row.addView(makeTableRowWithText("value 3", scrollableColumnWidths[2], fixedRowHeight)); 
       row.addView(makeTableRowWithText("value 4", scrollableColumnWidths[3], fixedRowHeight)); 
       row.addView(makeTableRowWithText("value 5", scrollableColumnWidths[4], fixedRowHeight)); 
       scrollablePart.addView(row); 


      } 

     } 

    public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth, int fixedHeightInPixels) { 
      int screenWidth = getResources().getDisplayMetrics().widthPixels; 
      recyclableTextView = new TextView(this); 
      recyclableTextView.setText(text); 
      recyclableTextView.setTextColor(Color.BLACK); 
      recyclableTextView.setTextSize(20); 
      recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth/100); 
      recyclableTextView.setHeight(fixedHeightInPixels); 
      return recyclableTextView; 
     } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

回答

0

我想你通过所有的TableRows需要循环和交换自己的看法。例如,交换第一和第二列:

 TableLayout table = (TableLayout)findViewById(R.id.scrollable_part); 
     int sourceColumnIndex = 0; 
     int targetColumnIndex = 1; 
     for (int i = 0; i < table.getChildCount(); i++) 
     { 
      TableRow tr = (TableRow)table.getChildAt(i); 
      View v = tr.getChildAt(sourceColumnIndex); 
      tr.removeViewAt(sourceColumnIndex); 
      tr.addView(v, targetColumnIndex); 
     } 

编辑: 我做了检查,在我自己的小项目。我现在看到,在你的项目中,交换的视图有不同的父母。我改变了你的代码中添加“的TableRow”即使是固定部分:

row = new TableRow(this); 
    row.setLayoutParams(wrapWrapTableRowParams); 
    row.setGravity(Gravity.CENTER); 
    row.addView(fixedView); 
    fixedColumn.addView(row); 

这交换程序之后:

   TableLayout fixedColumnTable = (TableLayout) findViewById(R.id.fixed_column); 
       TableLayout table = (TableLayout)findViewById(R.id.scrollable_part); 
       if (table.getChildCount() == fixedColumnTable.getChildCount()) 
       { 
        int fixedColumnIndex = 0; 
        int targetColumnIndex = 2; 
        for (int i = 0; i < table.getChildCount(); i++) 
        { 
         TableRow fixedTR = (TableRow)fixedColumnTable.getChildAt(i); 
         View fixedView = fixedTR.getChildAt(fixedColumnIndex); 

         TableRow targetTR = (TableRow)table.getChildAt(i); 
         View targetView = targetTR.getChildAt(targetColumnIndex); 

         fixedTR.removeViewAt(fixedColumnIndex); 
         targetTR.removeViewAt(targetColumnIndex); 
         fixedTR.addView(targetView, fixedColumnIndex); 
         targetTR.addView(fixedView, targetColumnIndex); 
        } 
       } 
+0

谢谢你,但我盘复制你的代码和过去的我项目ater循环,但没有改变的地方列你有没有尝试过这段代码? 你成功了吗? –

+0

对不起,兄弟 我都试过,但没有结果 看这里: –

+0

Thnks你,但没有工作,我想永远是恒定的蓝色柱体随时随地可以添加代码,我加在一个以上的地方不工作,请记住,我想成为一个恒定的蓝色列在右侧 而我想知道的地方输入的代码正好 并谢谢你 –