2014-03-26 60 views
1

我具有其中包含報頭(列名稱)的活性的tablelayout,中間佈局(列數據)和頁腳(按鈕)添加垂直和水平滾動。現在我需要一個垂直滾動中間佈局和水平滾動標題和中間佈局。編程在活動

中間佈局的垂直滾動工作正常,但水平滾動不起作用。

我怎麼能在一個活動中以編程方式添加這兩種方式滾動不使用XML?

這是我的代碼到目前爲止。

RelativeLayout rlBody = new RelativeLayout(this); 
rlBody.setPadding(0, 0, 0, 0); 
RelativeLayout.LayoutParams bodyParams = new RelativeLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
rlBody.setLayoutParams(bodyParams); 

but_downl = new Button(getApplicationContext()); 
but_downl.setText("Download"); 
but_downl.setWidth(width); 

RelativeLayout footer = new RelativeLayout(this); 
RelativeLayout.LayoutParams footerParams = new RelativeLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 

footerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
footer.setLayoutParams(footerParams); 
footer.addView(but_downl); 

RelativeLayout rlmaintableLayout = new RelativeLayout(this); 
RelativeLayout.LayoutParams rlmaintableLayoutParams = new RelativeLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
rlmaintableLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
rlmaintableLayoutParams.addRule(RelativeLayout.ABOVE, footer.getId()); 
rlmaintableLayout.setLayoutParams(rlmaintableLayoutParams); 

TableLayout maintableLayout = new TableLayout(getApplicationContext()); 
maintableLayout.setVerticalScrollBarEnabled(true); 

TextView tvQnr, tvDownLType, tvPublishBy, tvPublishDate, tvMonPlan, tvLang, textViewMsg; 
CheckBox chk, chkparent; 
View v; 
tableRow = new TableRow(getApplicationContext()); 

v = new View(this); 
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
v.setBackgroundColor(Color.rgb(51, 51, 51)); 

chkparent=new CheckBox(this); 
tableRow.addView(chkparent); 

tvQnr=new TextView(getApplicationContext()); 
tvQnr.setText("Name"); 
tvQnr.setTextColor(Color.parseColor("#235C8B")); 
tvQnr.setTypeface(null, Typeface.BOLD); 
tvQnr.setTextSize(20); 
tvQnr.setPadding(20, 20, 30, 20); 
tableRow.addView(tvQnr); 

tvDownLType=new TextView(getApplicationContext()); 
tvDownLType.setText("Age"); 
tvDownLType.setTextColor(Color.parseColor("#235C8B")); 
tvDownLType.setTypeface(null, Typeface.BOLD); 
tvDownLType.setTextSize(20); 
tvDownLType.setPadding(20, 20, 20, 20); 
tableRow.addView(tvDownLType); 

tvPublishBy=new TextView(getApplicationContext()); 
tvPublishBy.setText("Gender"); 
tvPublishBy.setTextColor(Color.parseColor("#235C8B")); 
tvPublishBy.setTypeface(null, Typeface.BOLD); 
tvPublishBy.setTextSize(20); 
tvPublishBy.setPadding(20, 20, 20, 20); 
tableRow.addView(tvPublishBy); 

tvPublishDate=new TextView(getApplicationContext()); 
tvPublishDate.setText("Published Date"); 
tvPublishDate.setTextColor(Color.parseColor("#235C8B")); 
tvPublishDate.setTypeface(null, Typeface.BOLD); 
tvPublishDate.setTextSize(20); 
tvPublishDate.setPadding(20, 20, 20, 20); 
tableRow.addView(tvPublishDate); 

tvMonPlan=new TextView(getApplicationContext()); 
tvMonPlan.setText("Roll No"); 
tvMonPlan.setTextColor(Color.parseColor("#235C8B")); 
tvMonPlan.setTypeface(null, Typeface.BOLD); 
tvMonPlan.setTextSize(20); 
tvMonPlan.setPadding(20, 20, 20, 20); 
tableRow.addView(tvMonPlan); 

tvLang=new TextView(getApplicationContext()); 
tvLang.setText("Language"); 
tvLang.setTextColor(Color.parseColor("#235C8B")); 
tvLang.setTypeface(null, Typeface.BOLD); 
tvLang.setTextSize(20); 
tvLang.setPadding(20, 20, 20, 20); 
tableRow.addView(tvLang); 

maintableLayout.addView(tableRow); 
maintableLayout.addView(v); 

RelativeLayout scrollHolder = new RelativeLayout(this);   
RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

scrollHolder.setLayoutParams(scrollHolderParams); 

ScrollView myScrollView = new ScrollView(this); 
myScrollView.setEnabled(true); 

HorizontalScrollView horscrollview = new HorizontalScrollView(this); 
horscrollview.setEnabled(true); 

TableLayout tableLayout = new TableLayout(getApplicationContext()); 
int cnt = 0; 

for (int j = 0; j < myFormresult.size(); j++){ 
    tableRow = new TableRow(getApplicationContext()); 
    v = new View(this); 
    v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
    v.setBackgroundColor(Color.rgb(51, 51, 51)); 

    cnt = cnt + 1; 
    chk = new CheckBox(this); 

    tvQnr = new TextView(getApplicationContext()); 
    tvQnr.setTextColor(Color.parseColor("#235C8B")); 
    tvQnr.setTextSize(15); 
    tvQnr.setText(myFormresult.get(j).getFormName()); 
    tvQnr.setPadding(25, 10, 20, 10); 

    tvDownLType = new TextView(getApplicationContext()); 
    tvDownLType.setTextColor(Color.parseColor("#235C8B")); 
    tvDownLType.setTextSize(15); 
    tvDownLType.setText(myFormresult.get(j).getVersion()); 
    tvDownLType.setPadding(25, 10, 20, 10); 

    tvPublishBy = new TextView(getApplicationContext()); 
    tvPublishBy.setTextColor(Color.parseColor("#235C8B")); 
    tvPublishBy.setTextSize(15); 
    tvPublishBy.setText(myFormresult.get(j).getPublishedBy()); 
    tvPublishBy.setPadding(25, 10, 20, 10); 

    tvPublishDate = new TextView(getApplicationContext()); 
    tvPublishDate.setTextColor(Color.parseColor("#235C8B")); 
    tvPublishDate.setTextSize(15); 
    tvPublishDate.setText(myFormresult.get(j).getPublishedDate()); 
    tvPublishDate.setPadding(25, 10, 20, 10); 

    tvMonPlan = new TextView(getApplicationContext()); 
    tvMonPlan.setTextColor(Color.parseColor("#235C8B")); 
    tvMonPlan.setTextSize(15); 
    tvMonPlan.setText(myFormresult.get(j).getMonitoringPlan()); 
    tvMonPlan.setPadding(25, 10, 20, 10); 

    tvLang = new TextView(getApplicationContext()); 
    tvLang.setTextColor(Color.parseColor("#235C8B")); 
    tvLang.setTextSize(15); 
    tvLang.setText(myFormresult.get(j).getLanguage()); 
    tvLang.setPadding(25, 10, 20, 10);   

    tableRow.addView(chk); 
    tableRow.addView(tvQnr); 
    tableRow.addView(tvDownLType); 
    tableRow.addView(tvPublishBy); 
    tableRow.addView(tvPublishDate); 
    tableRow.addView(tvMonPlan); 
    tableRow.addView(tvLang);     

    tableLayout.addView(tableRow); 
    tableLayout.addView(v); 
    tableLayout.addView(vertView); 
} 


if (cnt == 0) { 
    textViewMsg = new TextView(getApplicationContext()); 
    textViewMsg.setText("No records found ..."); 
    textViewMsg.setTextColor(Color.parseColor("#235C8B")); 
    textViewMsg.setTextSize(15); 
    tableLayout.addView(textViewMsg); 
    but_downl.setEnabled(false); 
} 


myScrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

horscrollview.addView(tableLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

myScrollView.addView(horscrollview); 

scrollHolder.addView(myScrollView); 
maintableLayout.addView(scrollHolder); 
rlmaintableLayout.addView(maintableLayout); 
rlBody.addView(rlmaintableLayout); 
rlBody.addView(footer); 

在此先感謝...

+0

你可以發佈你的代碼?你是什​​麼意思'但水平滾動不起作用。' – InnocentKiller

+0

我已經發布了我的代碼。垂直滾動工作正常,但當我添加水平滾動,中間佈局消失。 – Suniel

回答

0

我創建表的佈局,水平和垂直滾動。另外標題固定在垂直滾動。

我給你的類和XML文件,您可以根據您的要求定製的代碼。 我希望它能幫助你。

創建MainActivity類別:

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.GestureDetector; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewTreeObserver; 
import android.view.View.OnTouchListener; 
import android.view.ViewTreeObserver.OnGlobalLayoutListener; 
import android.widget.HorizontalScrollView; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TableRow.LayoutParams; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
HorizontalScrollView hori,hori2; 
int viewWidth; 
TableLayout scrollablePart; 
GestureDetector gestureDetector = null; 
ArrayList<LinearLayout> layouts; 
    LinearLayout linear; 
    private int scrollMax; 
int currPosition, prevPosition; 
/** Called when the activity is first created. */ 
@Override 
public 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, 20}; 
    int[] scrollableColumnWidths = new int[]{20,20, 20, 20, 20, 20}; 
    int fixedRowHeight = 50; 
    int fixedHeaderHeight = 60; 

    TableRow row = new TableRow(this); 
    //header (fixed horizontally) 

    TableLayout fixedColumn = (TableLayout) findViewById(R.id.fixed_column); 
    TableLayout fixedColumn2 = (TableLayout) findViewById(R.id.fixed_header_column); 
    TextView fixedView2 = makeTableRowWithText("col 1 ", scrollableColumnWidths[0], fixedRowHeight); 
    fixedView2.setBackgroundColor(Color.LTGRAY); 
    fixedColumn2.addView(fixedView2); 

    //rest of the table (within a scroll view) 

    scrollablePart = (TableLayout) findViewById(R.id.scrollable_part); 

    //header (fixed vertically) 
    hori=(HorizontalScrollView)findViewById(R.id.horizon); 
    hori.setVerticalScrollBarEnabled(false); 
    hori.setHorizontalScrollBarEnabled(false); 
    hori2=(HorizontalScrollView)findViewById(R.id.hr_2); 
      //hori.onScrollChanged 
    hori2.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      moveScrollView(); 
      cal(); 

      //startAutoScrolling(); 
      return false; 
     } 
    }); 
    ScrollView scrol=(ScrollView)findViewById(R.id.Scroll); 
    //scrol.onScrollChanged(fixedHeaderHeight, fixedHeaderHeight, fixedHeaderHeight, fixedHeaderHeight); 

    linear=(LinearLayout)findViewById(R.id.fillable_area); 

    TableLayout header = (TableLayout) findViewById(R.id.table_header); 
    //scrollablePart.onGenericMotionEvent(MotionEvent.ACTION_MOVE); 

    ViewTreeObserver vto = linear.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      linear.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
      getScrollMaxAmount(); 

     } 
    }); 
    hori2.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      moveScrollView(); 
      //startAutoScrolling(); 
      return false; 
     } 
    }); 
    row.setLayoutParams(wrapWrapTableRowParams); 
    row.setGravity(Gravity.CENTER); 
    row.setBackgroundColor(Color.GRAY); 

    row.addView(makeTableRowWithText("col 2", fixedColumnWidths[1], fixedHeaderHeight)); 
    row.addView(makeTableRowWithText("col 3", fixedColumnWidths[2], fixedHeaderHeight)); 
    row.addView(makeTableRowWithText("col 4", fixedColumnWidths[3], fixedHeaderHeight)); 
    row.addView(makeTableRowWithText("col 5", fixedColumnWidths[4], fixedHeaderHeight)); 
    row.addView(makeTableRowWithText("col 6", fixedColumnWidths[5], fixedHeaderHeight)); 
    header.addView(row); 


    for(int i = 0; i < 50; 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)); 
     row.addView(makeTableRowWithText("value 6", scrollableColumnWidths[5], fixedRowHeight)); 
     scrollablePart.addView(row); 
    } 
} 
    private void cal(){  
final Handler handler2 = new Handler(); 
     Runnable runnable = new Runnable() { 
      int i=0; 
     public void run() 
     {          
     if(i==0) 
     {  
      try{     
       moveScrollView(); 
      }catch (Exception e){ 
      }   
      i++; 
     } 
     handler2.postDelayed(this,500); 
     } 
    }; 
     handler2.postDelayed(runnable, 500); 
    }  
    public void getScrollMaxAmount(){ 
    int actualWidth = (linear.getMeasuredWidth()-512); 
    scrollMax = actualWidth; 


    System.out.println("scrollMax"+scrollMax); 
} 
    private Timer scrollTimer  = null; 
private Timer clickTimer  = null; 
private Timer faceTimer   = null; 
private TimerTask clickSchedule; 
private TimerTask scrollerSchedule; 
private TimerTask faceAnimationSchedule; 
private int scrollPos = 0; 
public void startAutoScrolling(){ 
    if (scrollTimer == null) { 
     scrollTimer     = new Timer(); 
     final Runnable Timer_Tick = new Runnable() { 
      public void run() { 
       moveScrollView(); 
      } 
     }; 

     if(scrollerSchedule != null){ 
      scrollerSchedule.cancel(); 
      scrollerSchedule = null; 
     } 
     scrollerSchedule = new TimerTask(){ 
      @Override 
      public void run(){ 
       runOnUiThread(Timer_Tick); 
      } 
     }; 

     scrollTimer.schedule(scrollerSchedule, 30, 30); 
    } 
} 
public void moveScrollView(){ 
    scrollPos       = (int) (hori.getScrollX() + 1.0); 
    scrollPos       = (int) (hori2.getScrollX() + 1.0); 
    System.out.println("scrollPos"+scrollPos); 

    if(scrollPos >= scrollMax){ 
     scrollPos = 0; 
    } 
    hori2.scrollTo(scrollPos, 0); 
    hori.scrollTo(scrollPos, 0); 

} 

private TextView recyclableTextView; 
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; 
} 
} 

創建activity_main.xmml像:

<?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"> 
<LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="100dp" 
     android:gravity="center_horizontal" 
     android:id="@+id/fillable_area1"> 
     <TableLayout 
      android:id="@+id/fixed_header_column" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
<HorizontalScrollView 
    android:id="@+id/horizon" 
    android:layout_width="fill_parent" 

    android:layout_height="wrap_content"> 

      <TableLayout 
       android:id="@+id/table_header" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"/> 
</HorizontalScrollView> 
</LinearLayout> 
<ScrollView 
    android:id="@+id/Scroll" 
    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" 
     android:id="@+id/fillable_area"> 
     <TableLayout 
      android:id="@+id/fixed_column" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

     <HorizontalScrollView 
      android:id="@+id/hr_2" 
      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>