2014-02-24 88 views
4

我從服務器獲取數據併爲該響應動態創建表佈局,現在我需要僅顯示每頁5行,我使用setvisible(view.invisible)來隱藏所有錶行,如何設置5行setvisible(view.visible)以顯示行像分頁?如何使用set visible爲動態創建的表格佈局創建分頁?

我用來動態創建表的代碼如下。

TableLayout tableLayout = (TableLayout) findViewById(R.id.tablenew); 
     TableRow tableRow = new TableRow(this); 
     tableRow.setId(1); 
     tableRow.setBackgroundColor(Color.LTGRAY); 
     tableRow.setPadding(0,0,1,1); 
     tableRow.setLayoutParams(new TableLayout.LayoutParams()); 
     tableRow.setVisibility(View.INVISIBLE); 
     TextView text = new TextView(this); 
     text.setText(name); 
     text.setBackgroundColor(Color.WHITE); 
     text.setPadding(5, 5, 5, 5); 
     text.setTextSize(30); 
     text.setLayoutParams(new TableRow.LayoutParams()); 
     final Button button = new Button(this); 
     button.setPadding(5, 5, 5, 5); 
     button.setTextSize(30); 
     button.setTag(value); 
     button.setText("Install"); 
     button.setLayoutParams(new TableRow.LayoutParams()); 
     button.setOnClickListener(new View.OnClickListener() 
     { 
       @Override 
       public void onClick(View v) 
       { 
         TableLayout tableLayout = (TableLayout)  findViewById(R.id.tablenew); 
        final TableRow parent = (TableRow) v.getParent(); 
        tableLayout.removeView(parent); 
          String id=(String) v.getTag(); 

       } 
      });//button click listener 
     tableRow.addView(text); 
     tableRow.addView(button); 
     tableLayout.addView(tableRow); 
+0

你會怎麼做?解釋一下更多 –

+0

@MD -i需要用setvisible(view.visible)以編程方式使用行索引在每頁顯示5行。 – Strawberry

+0

@ MD-最初我需要使用setvisible(view.visible)來顯示錶格的前5行,如果用戶單擊下一個按鈕,則需要隱藏可見行並使下5行可見。 – Strawberry

回答

1

嘗試this,他們用示例代碼說明了..

1

試試這個代碼:

tableraw.xml

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TableLayout 
    android:id="@+id/tablenew" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/next" 
    android:layout_alignParentTop="true" > 
</TableLayout> 

<Button 
    android:id="@+id/next" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="next" /> 

</RelativeLayout> 

TableViewDemo.java

package com.test; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class TableViewDemo extends Activity { 

int index = 5; 
Button nextButton; 
TableLayout tableLayout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tableraw); 
    tableLayout = (TableLayout) findViewById(R.id.tablenew); 
    nextButton = (Button) findViewById(R.id.next); 

    for (int i = 1; i <= 20; i++) { 

     TableRow tableRow = new TableRow(this); 
     tableRow.setTag(i); 
     tableRow.setBackgroundColor(Color.LTGRAY); 
     tableRow.setPadding(0, 0, 1, 1); 
     tableRow.setLayoutParams(new TableLayout.LayoutParams()); 

     TextView text = new TextView(this); 
     text.setText("name" + i); 
     text.setBackgroundColor(Color.WHITE); 
     text.setPadding(5, 5, 5, 5); 
     text.setTextSize(30); 
     text.setLayoutParams(new TableRow.LayoutParams()); 

     final Button button = new Button(this); 
     button.setPadding(5, 5, 5, 5); 
     button.setTextSize(30); 
     button.setTag("value"); 
     button.setText("Install" + i); 
     button.setLayoutParams(new TableRow.LayoutParams()); 
     tableRow.addView(text); 
     tableRow.addView(button); 

     if (i < 6) { 
      tableRow.setVisibility(View.VISIBLE); 
     } else { 
      tableRow.setVisibility(View.GONE); 
     } 

     tableLayout.addView(tableRow); 

    } 

    nextButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

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

       TableRow parent = (TableRow) tableLayout.getChildAt(i); 
       // tableLayout.removeView(parent); 
       Integer id = ((Integer) parent.getTag()); 
       if (id > index && id <= (index + 5)) { 
        parent.setVisibility(View.VISIBLE); 
       } else { 
        parent.setVisibility(View.GONE); 
       } 
      } 
      index = index + 5; 

     } 
    }); 

} 
} 
+0

謝謝你的回答。我會嘗試更新。 – Strawberry

+0

你最歡迎.... :),但你可以使用pageview指標代替..它很好的分頁 – Jayesh

+0

你爲什麼不鼓勵使用'ListView'?當需要顯示很多行時,'ListView'可以有效處理內存...... –

1
  prev.setOnClickListener(new View.OnClickListener() 
      { 
        @Override 
        public void onClick(View v) 
        { 
          TableLayout tl = (TableLayout) findViewById(R.id.tablenew); 
       for (int i = 10; i < tl.getChildCount(); i--) 
       { 

        if(i>0) 
        { 
          TableRow row = (TableRow) tl.getChildAt(i); 
          Integer id = ((Integer) row.getTag()); 
        if (id < 11 && id <= (10 - 5)) //index - 5)) { 
        { 
          Log.d("iiiiiiiiiiiiiiiii",Integer.toString(i)); 
         row.setVisibility(View.VISIBLE); 
        } 
        else 
        { 
          Log.d("aaaaaaaaaaaaaaaaaaa",Integer.toString(i)); 
         row.setVisibility(View.GONE); 
        } 
        } 

       } 
       //index = index - 5; 

      } 
      }); 
+0

@jayesh我已經嘗試過以前這樣的按鈕代碼,但它不會停止,當我達到0值,請建議我.. – Strawberry

1
prev.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      TableLayout tl = (TableLayout) findViewById(R.id.tablenew); 
      for (int i = 6; i > 0; i--) 
      { 
       Log.d("aaaaaaaaaaaaaaaaaaa",Integer.toString(index)); 
       TableRow row = (TableRow) tl.getChildAt(i); 
       Integer id = ((Integer) row.getTag()); 

       if (id < index && id <= (index - 3)) //index - 3)) { 
       { 
        Log.d("iiiiiiiiiiiiiiiii",Integer.toString(i)); 
        row.setVisibility(View.VISIBLE); 
       } 
       else 
       { 
        Log.d("aaaaaaaaaaaaaaaaaaa",Integer.toString(i)); 
        row.setVisibility(View.GONE); 
       } 

      } 
      index = index - 3; 
     } 
    }); 
1
public class Tabledemo extends Activity { 
int index = 5; 
int i,preval,preival; 
int nexval=1; 
Button prev,next; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tabledemo); 
    session = new SessionManager(getApplicationContext()); 
    prev = (Button) findViewById(R.id.pre); 
    next = (Button) findViewById(R.id.nex); 

    prev.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      TableLayout tl = (TableLayout) findViewById(R.id.tablenew); 
      for (i = preval ; i > preival; i--) 
      { 
       TableRow row = (TableRow) tl.getChildAt(i); 
       Integer id = ((Integer) row.getTag()); 

       if (id < index && id <= (index - 5)) 
       { 
        row.setVisibility(View.VISIBLE); 
        nexval=i; 
       } 
       else 
       { 
        row.setVisibility(View.GONE); 
       } 

      } 
      preival=preival-5; 
      index = index - 5; 
     } 
    }); 


    next.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       TableLayout tl = (TableLayout) findViewById(R.id.tablenew); 
       for (i = nexval; i < tl.getChildCount() ; i++) 
       { 
        TableRow row = (TableRow) tl.getChildAt(i); 
        Integer id = ((Integer) row.getTag()); 
        if (id > index && id <= (index + 5)) 
        { 
         row.setVisibility(View.VISIBLE); 
         preval=i; 
        } 
        else 
        { 
         row.setVisibility(View.GONE); 
        } 
       } 
       preival=index; 
       preival=preival-5; 
       index = index + 5; 
      } 
     }); 
+0

完美工作的下一個和以前的代碼段... – Chawla