-1

我想要 TableView 的示例并基于按钮单击实现页面导航,例如 NExt 和 prev 按钮。请为我提供 android 中 TableView/TableLayout 分页的链接。我是一个新人,所以请提供对布局进行解释的链接,我可能听起来含糊不清,但我没有其他选择可以在这里询问。

4

3 回答 3

1

请在此处找到完整的分页示例

这是我的活动课

    public class TestActivity extends AppCompatActivity {

        RecyclerView recyclerView;
        LinearLayoutManager layoutManager;
        TestAdapter testAdapter;
        List<TestM> testMS;


        private boolean loading = true;
        int pastVisiblesItems, visibleItemCount, totalItemCount;

        int testLast = 0;
        ProgressBar progressbar;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test);

            setInit();
        }

        public void setInit() {

            recyclerView = findViewById(R.id.recyclerView);
            progressbar = findViewById(R.id.progressbar);


            layoutManager = new LinearLayoutManager(TestActivity.this, LinearLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(layoutManager);


            testMS = new ArrayList<>();

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

                testLast = i;
                testMS.add(new TestM("alex"+i));
            }

            testAdapter = new TestAdapter(TestActivity.this, testMS, recyclerView);
            recyclerView.setAdapter(testAdapter);


            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                    progressbar = findViewById(R.id.progressbar);

                    if (dy > 0)
                    {
                        visibleItemCount = layoutManager.getChildCount();
                        totalItemCount = layoutManager.getItemCount();
                        pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

                        if (loading) {
                            if ((visibleItemCount + pastVisiblesItems) >= totalItemCount-1) {
                                loading = false;
                                Log.v("...", "Last Item Wow !");

                                progressbar.setVisibility(View.VISIBLE);
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        loadNextDataFromApi();
                                    }
                                }, 1000);


                            }
                        }
                    }
                }
            });

        }


        public void click(int position) {

            TestAdapter.ViewHolder viewHolder = (TestAdapter.ViewHolder) recyclerView.findViewHolderForAdapterPosition(position);

            viewHolder.text_color.setTextColor(Color.parseColor("#245251"));
        }

        public void loadNextDataFromApi() {

            loading = true;

            int j = testLast+1;
            int k = j+5;

            for (int i = j; i < k; i++){
                testLast = i;
                testMS.add(new TestM("alex"+i));
            }

            progressbar.setVisibility(View.GONE);
            testAdapter.notifyDataSetChanged();
        }
    }

这是我的 Testactivity xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.production.myextracode.recyclerview.TestActivity">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/progressbar"/>
        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_below="@+id/cv_test"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:visibility="gone"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>

这是适配器

    package com.production.myextracode.recyclerview;

    import android.support.v7.widget.CardView;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    import com.production.myextracode.R;

    import java.util.List;

    public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder>  {

        private TestActivity context;
        private List<TestM> testMS;
        private RecyclerView recyclerView;

        public TestAdapter(TestActivity context, List<TestM> testMS, RecyclerView recyclerView) {
            this.context = context;
            this.testMS = testMS;
            this.recyclerView = recyclerView;

        }



        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test,parent,false);
            ViewHolder rcv = new ViewHolder(itemView);
            return rcv;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {

            TestM model = testMS.get(position);

            holder.text_color.setText(model.getName());

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

                    context.click(position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return testMS.size();
        }


        public class ViewHolder extends RecyclerView.ViewHolder {

            TextView text_color ;
            CardView cv_test;

            private ViewHolder(View itemView) {

                super(itemView);

                text_color      = itemView.findViewById(R.id.tv_test);
                cv_test     = itemView.findViewById(R.id.cv_test);
            }
        }
    }

适配器的项目 xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
        <android.support.v7.widget.CardView
            android:id="@+id/cv_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="10dp"
            card_view:cardPreventCornerOverlap="false"
            card_view:cardUseCompatPadding="false">
            <TextView
                android:id="@+id/tv_test"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Test"
                android:textSize="16sp" />
        </android.support.v7.widget.CardView>
    </RelativeLayout>

这是模型类

    package com.production.myextracode.recyclerview;

    public class TestM {

        String name;

        public TestM(String name) {

            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

希望这会帮助你。

于 2018-08-10T12:42:35.200 回答
0

你可以参考这个 url,iOS 和 Android 的无限加载表视图。

于 2012-11-22T10:14:05.000 回答
-1

请检查以下链接:

链接1

链接2

在此处输入图像描述

链接提供了带有布局的源代码。我已经突出显示了该部分,请检查页面。

于 2012-11-22T10:19:16.280 回答