2 zhangya 101314 zhangya_101314 于 2016.09.17 18:01 提问

Android 客户端 TextView表格分页显示数据

由于接口里面最多有100行数据(可能少于100),显示数据的xml布局文件是在线性布局下面定义了TextView
,不是用的ListView,现在表格里面能显示100行数据出来,现在想在手机上面每个页面显示
10行数据,下面要有页码显示最好,求大神给我指点,本人实在太菜,下面是表格的布局文件,只定义了一行,在代码里面动态添加的,本人QQ:1194660702,帮我解决这个
问题,本人发大红包

<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/ct2r_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFEEEEEE"
android:orientation="vertical"
android:padding="1px"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/ct2r_1"
        android:layout_width="130dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>
    <TextView
        android:id="@+id/ct2r_2"
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <TextView
        android:id="@+id/ct2r_3"
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <TextView
        android:id="@+id/ct2r_4"
        android:layout_width="350dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <TextView
        android:id="@+id/ct2r_5"
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <TextView
        android:id="@+id/ct2r_6"
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <TextView
        android:id="@+id/ct2r_7"
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <TextView
        android:id="@+id/ct2r_8"
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333">

        <TextView
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_centerInParent="true"
            android:layout_margin="0dp"
            android:background="@drawable/slidingmenu_ct2r_check"
            android:gravity="center"/>
    </RelativeLayout>

    <TextView
        android:id="@+id/ct2r_10"
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <TextView
        android:id="@+id/ct2r_11"
        android:layout_width="200dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>

    <TextView
        android:id="@+id/ct2r_12"
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333"
        android:gravity="center"/>


    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="35dp"
        android:layout_column="1"
        android:layout_margin="1px"
        android:background="#FF333333">

        <TextView
            android:id="@+id/feedback"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_centerInParent="true"
            android:layout_margin="0dp"
            android:background="@drawable/feed_back"
            android:gravity="center"/>
    </RelativeLayout>



</LinearLayout>

2个回答

qq_24827089
qq_24827089   2016.09.17 18:46
已采纳

RecyclerView可以完成你的需求

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

        MyScrollView rootView = new MyScrollView(this);
        rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
        horizontalScrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        RecyclerView recyclerView = new RecyclerView(this){
            @Override
            protected void onMeasure(int widthSpec, int heightSpec) {
                int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                        MeasureSpec.AT_MOST);
                super.onMeasure(widthSpec, expandSpec);
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));


        recyclerView.setAdapter(new MyAdapter());


        horizontalScrollView.addView(recyclerView);
        rootView.addView(horizontalScrollView);
        setContentView(rootView);

    }

    class MyAdapter extends RecyclerView.Adapter {
        private int size=50;
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(TestActivity.this).inflate(R.layout.activity_test, parent, false);
            TextView name = (TextView) view.findViewById(R.id.ct2r_1);
            view.setTag(name);
            return new RecyclerView.ViewHolder(view) {};
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (getItemViewType(position) ==1)
            ((TextView)holder.itemView.getTag()).setText("这是第 "+ position);
            else {
                ((TextView) holder.itemView.getTag()).setText("这是第 " + position / 10 + "页");
                holder.itemView.setBackgroundColor(Color.BLUE);
            }
        }

        @Override
        public int getItemViewType(int position) {
            return (position == 0 || position % 10 !=0) ? 1:2;
        }

        @Override
        public int getItemCount() {
            int num = size / 10;
            return size+num;
        }
    }

    class MyScrollView extends ScrollView {

    private int downY;
    private int mTouchSlop;

    public MyScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) e.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) e.getRawY();
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(e);
    }
}
zhangya_101314
zhangya_101314   2016.09.21 09:33

谢谢你了 朋友 你微信多少呀 

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!