如何在Recyclerview中进行更多展示?

时间:2016-03-11 05:33:26

标签: android

我正在开发一个应用程序,其中有 RecyclerView ,我所做的是,当用户滚动回收站视图时,会向他们显示更多按钮,然后单击按钮从服务器加载更多数据使用json,请帮助我。

public class CDealAppListing extends Fragment {
    public static String m_DealListingURL = "http://192.168.0.110:8080/ireward/rest/json/metallica/getDealListInJSON";
    public static String s_szresult = " ";
    public ArrayList<CDealAppDatastorage> m_oDataList;
    public int[] m_n_FormImage;
    public View m_Main;
    public CRegistrationSessionManagement m_oSessionManagement;
    public String m_szMobileNumber, m_szEncryptedPassword;
    private RecyclerView m_RecyclerView;
    private RecyclerView.Adapter m_oAdapter;
    private CJsonsResponse m_oJsonsResponse;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        m_Main = inflater.inflate(R.layout.deal_app_listing, container, false);//intialize mainLayout
        new CDealDataSent().execute(m_DealListingURL);
        init();//initialize method
        return m_Main;
    }

    public void init() {
        m_n_FormImage = new int[]{
                R.drawable.amazon,
                R.drawable.whatsapp,
                R.drawable.zorpia,
                R.drawable.path,
                R.drawable.app_me,
                R.drawable.evernote,
                R.drawable.app_me};
        m_RecyclerView = (RecyclerView) m_Main.findViewById(R.id.my_recycler_view);//finding id of recyclerview
        m_RecyclerView.setItemAnimator(new DefaultItemAnimator());//setting default animation to recyclerview

        m_RecyclerView.setHasFixedSize(true);//fixing size of recyclerview
        //Layout manager for Recycler view
        m_RecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//showing odata vertically to user.

//            m_RecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,1));
        m_oSessionManagement = new CRegistrationSessionManagement(getActivity());
        HashMap<String, String> user = m_oSessionManagement.getRegistrationDetails();
        m_szEncryptedPassword = user.get(m_oSessionManagement.s_szKEY_PASSWORD);
        m_szMobileNumber = user.get(m_oSessionManagement.s_szKEY_MOBILENUMBER);
    }

    //sending deal data to retreive response from server
    public String DealListing(String url, CRegistrationDataStorage login) {
        InputStream inputStream = null;
        m_oJsonsResponse = new CJsonsResponse();
        try {
            // 1. create HttpClient
            HttpClient httpclient = new DefaultHttpClient();
            // 2. make POST request to the given URL
            HttpPost httpPost = new HttpPost(url);
            String json = "";
            // 3. build jsonObject
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("agentCode", "99999999");
            jsonObject.put("pin", "08556CB2450231B0D7235C3446B078A6");
            jsonObject.put("recordcount", "5");
            jsonObject.put("lastcountvalue", "0");
            // 4. convert JSONObject to JSON to String
            json = jsonObject.toString();
            // 5. set json to StringEntity
            StringEntity se = new StringEntity(json);
            // 6. set httpPost Entity
            httpPost.setEntity(se);
            // 7. Set some headers to inform server about the type of the content
            httpPost.setHeader("Content-type", "application/json");
            // 8. Execute POST request to the given URL
            HttpResponse httpResponse = httpclient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            // 9. receive response as inputStream
            inputStream = entity.getContent();
            System.out.println("InputStream....:" + inputStream.toString());
            System.out.println("Response....:" + httpResponse.toString());

            StatusLine statusLine = httpResponse.getStatusLine();
            System.out.println("statusLine......:" + statusLine.toString());
            ////Log.d("resp_body", resp_body.toString());
            int statusCode = statusLine.getStatusCode();
            // 10. convert inputstream to string
            if (statusCode == 200) {
                // 10. convert inputstream to string
                if (inputStream != null)
                    s_szresult = m_oJsonsResponse.convertInputStreamToString(inputStream);
                //String resp_body =
                EntityUtils.toString(httpResponse.getEntity());
            } else
                s_szresult = "Did not work!";
        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }
        System.out.println("resul.....:" + s_szresult);
        // 11. return s_szResult
        return s_szresult;
    }

    //  sending deal data to server and retreive response......
    class CDealDataSent extends AsyncTask<String, Void, String> {
        public JSONObject m_oResponseobject;
        public ProgressDialog m_PDialog;
        public CRegistrationDataStorage oRegisterStorage;
        public CDealAppDatastorage item;

        //      @Override
        protected void onPreExecute() {
            super.onPreExecute();
            m_PDialog = new ProgressDialog(getActivity());
            m_PDialog.setMessage("Please wait while Loading Deals...");
            m_PDialog.setCancelable(false);
            m_PDialog.show();
        }

        @Override
        protected String doInBackground(String... urls) {
            return DealListing(urls[0], oRegisterStorage);// sending data to server...

        }

        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            m_PDialog.dismiss();
            try {
                m_oResponseobject = new JSONObject(result);// getting response from server
                final JSONArray posts = m_oResponseobject.optJSONArray("dealList");

                m_oDataList = new ArrayList<CDealAppDatastorage>();
                for (int i = 0; i < posts.length(); i++) {
                    JSONObject post = posts.getJSONObject(i);
                    item = new CDealAppDatastorage();
                    item.setM_szHeaderText(post.getString("dealname"));
                    item.setM_szsubHeaderText(post.getString("dealcode"));
                    item.setM_n_Image(m_n_FormImage[i]);
                    m_oDataList.add(item);

                }
                if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {

                    m_oAdapter = new CDealAppListingAdapter(m_oDataList);//creating object of adapter and addd setting odata to adapter for use.
                    m_RecyclerView.setAdapter(m_oAdapter);//adding adapter to recyclerview
                } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {
                    Toast.makeText(getActivity(), "Connection not avaliable", Toast.LENGTH_SHORT).show();
                }
                System.out.println("agentCode...." + m_szMobileNumber);
                System.out.println("password...." + m_szEncryptedPassword);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

有两种方法可以解决这个问题 - 编写自己的实现或使用库。

没有第三方库 - https://guides.codepath.com/android/Endless-Scrolling-with-AdapterViews-and-RecyclerView#implementing-with-recyclerview

使用第三方图书馆 - https://github.com/cymcsg/UltimateRecyclerViewhttps://github.com/Malinskiy/SuperRecyclerView

我个人会选择UltimateRecyclerView而不是其他选项,因为它有很多额外的功能和庞大的用户群,但没有&#34;对&#34;答案。