如何使用set visible创建动态创建的表格布局的分页?

时间:2014-02-24 06:34:56

标签: android pagination android-tablelayout

我从服务器获取数据并为该响应动态创建tablelayout,现在我需要每页只显示5行,我使用setvisible(view.invisible)隐藏所有表行,如何设置5行setvisible(view.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);

5 个答案:

答案 0 :(得分: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;

        }
    });

}
}

答案 1 :(得分: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;

           }
           });

答案 2 :(得分: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;
        }
    });

答案 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;
            }
        });

答案 4 :(得分:1)

试试this,他们已经用示例代码进行了解释..