android - 如何在我的 fragment 和适配器中添加更多负载

标签 android android-layout android-studio listview android-fragments

我正在尝试增加负载。 我尝试应用我要搜索的内容,但没有成功。

这是我的代码。 最新成绩 fragment

public class LatestGradeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
        List<ListGradeData> sectionList;
        RecyclerView recyclerView;
        SwipeRefreshLayout mSwipeRefreshLayout;

    public static LatestGradeFragment newInstance() {
        return new LatestGradeFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_latest_grade, container, false);

        //RecyclerView+CardView for section
        recyclerView = (RecyclerView) rootView.findViewById(R.id.display_recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        sectionList = new ArrayList<>();

        mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeRefreshSection);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,
                android.R.color.holo_green_dark,
                android.R.color.holo_orange_dark,
                android.R.color.holo_blue_dark);

        mSwipeRefreshLayout.post(new Runnable() {

            @Override
            public void run() {

                mSwipeRefreshLayout.setRefreshing(true);
                // Fetching data from server
                loadSection();
            }
        });

        return rootView;
    }

    @Override
    public void onRefresh() {

        loadSection();

    }

    private void loadSection() {

        mSwipeRefreshLayout.setRefreshing(true);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.USER_GRADE,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            if(sectionList!=null) {
                                sectionList.clear();
                            }
                            //traversing through all the object
                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject sections = array.getJSONObject(i);

                                //adding the product to product list
                                sectionList.add(new ListGradeData(
                                        sections.getInt("id"),
                                        sections.getString("section"),
                                        sections.getString("level"),
                                        sections.getString("schoolyear")
                                ));
                            }

                            //creating adapter object and setting it to recyclerview
                            LatestGradeAdapter adapter = new LatestGradeAdapter(getActivity(), sectionList);
                            recyclerView.setAdapter(adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        // Stopping swipe refresh
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // Stopping swipe refresh
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);
    }

    @Override
    public String toString() {
        return "LatestGradeFragment";
    }
}`

这是我的 LatestGradeAdapter:

public class LatestGradeAdapter extends RecyclerView.Adapter<LatestGradeAdapter.RecyclerViewHolder> {


    private Context mCtx;
    private List<ListGradeData> sectionList;

    `public LatestGradeAdapter(Context mCtx, List<ListGradeData> sectionList) {
        this.mCtx = mCtx;
        this.sectionList = sectionList;
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.section_data_list, parent, false);
        return new LatestGradeAdapter.RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        final ListGradeData sections = sectionList.get(position);
        //BIND DATA
        holder.textViewSection.setText(sections.getSection());
        holder.textViewLevel.setText(sections.getLevel());
        holder.textViewSchoolYear.setText(sections.getSchoolyear());
    }
    @Override
    public int getItemCount() {
        return sectionList.size();
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        //Variables for list
        TextView textViewSection, textViewLevel, textViewSchoolYear;

        //Variables for head section
        TextView textHeaderSection, textHeaderLevel, textHeaderSchoolYear;

        public RecyclerViewHolder(final View itemView) {
            super(itemView);

            textViewSection = (TextView) itemView.findViewById(R.id.textSection);
            textViewLevel = (TextView) itemView.findViewById(R.id.textLevel);
            textViewSchoolYear = (TextView) itemView.findViewById(R.id.textYear);



        }
    }
}`

最佳答案

在您的 LatestGradeFragment.class 中添加此代码

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

        int lastVisiblePosition = layoutManager.findLastVisibleItemPosition();
        if (lastVisiblePosition == recyclerView.getChildCount()) {
               progrssBar.setVisibility(View.VISIBLE);
               loadMore(); //This methos is used for load next set of items.

        }
    }
});

public void loadMore(){
//load next set of items to adapter
adapter.notifyDataSetChanged();
progrssBar.setVisibility(View.GONE);
}

像这样创建你的 fragment_latest_grade:

<?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">

<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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    android:visibility="gone" />

</RelativeLayout>

关于android - 如何在我的 fragment 和适配器中添加更多负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51297177/

相关文章:

android-manifest - 在 Google Play 上发布后,我可以降低应用程序的 API 级别 (minSDK) 吗?

java - 以 Imageview、TextView 和按钮作为项目的自定义 ListView

android - 在本地网络上使用 TCP 还是 UDP 进行视频流传输?

android - Admob背景颜色

java - UIL 默认不支持 scheme(protocol)

java - imageview和drawables之间的碰撞检测

java - 如何将 ScrollView 添加到抽屉导航

Android:以编程方式添加的布局忽略主题

android:画圆切边框

gradle - Android Studio构建错误