如何实现无尽的Recylerview?

时间:2016-08-18 05:59:23

标签: android-recyclerview

如何实施无休止的Recylerview?

这是活动代码:

public class ShopList extends AppCompatActivity {
    RecyclerView rview;
    RatingBar ratingbar;
    private String  `urlParameters`;
    Recyclerviewshopl adapter;
    String category;
    JSONArray arr = null;
    private Boolean Isinternetpresent = false;
    ConnectionDetector cd;
    String cat;
    ProgressDialog dialog;
    String lat,lon;
    TextView nodata;
    ImageView oops;
    double latitude, longitude;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ratingbar = (RatingBar) findViewById(R.id.ratingbar);
        List<Itemshopl> rowListItem = getAllItemList();
        rview=(RecyclerView)findViewById(R.id.recycleshop);
        nodata=(TextView)findViewById(R.id.nodata);
        oops=(ImageView)findViewById(R.id.oops);
        nodata.setVisibility(View.GONE);
        oops.setVisibility(View.GONE);
        //  LayerDrawable stars = (LayerDrawable) ratingbar.getProgressDrawable();
        //stars.getDrawable(5).setColorFilter(Color.parseColor("#26ce61"),
        //   PorterDuff.Mode.SRC_ATOP);
        // stars.getDrawable(1).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);

       /* RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, //width
                ViewGroup.LayoutParams.WRAP_CONTENT);//height
        rview.setLayoutParams(lp);*/
        Bundle extras = getIntent().getExtras();
        cat  = extras.getString("category");
        lat=extras.getString("lat");
        lon=extras.getString("lon");
        System.out.println("gr++"+cat);
        cd = new ConnectionDetector(getApplicationContext());
        Isinternetpresent = cd.isConnectingToInternet();
      //  onBackPressed();
        if(Isinternetpresent)
        {
            shoplist tasku=new shoplist();
            tasku.execute(new String[]{"http://abc**.com/****/getshoplist"});
        }else{
            // Toast.makeText(UserProfileActivity.this,"No Internet connection",Toast.LENGTH_SHORT).show();
            showAlertDialog(ShopList.this, "No Internet Connection", "You don't have internet connection.", false);
        }
    }

    public void showAlertDialog(Context context, String title, String message, Boolean status)
    {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        //         alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
            }
        });
        alertDialog.show();
    }

    private List<Itemshopl> getAllItemList() {
        List<Itemshopl> allItems = new ArrayList<Itemshopl>();

        allItems.add(new Itemshopl());
        allItems.add(new Itemshopl());
        allItems.add(new Itemshopl());
        allItems.add(new Itemshopl());
        return allItems;
    }

    private class shoplist extends AsyncTask<String, String, List<Itemshopl>> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = ProgressDialog.show(ShopList.this, "Loading", "Please Wait...", true);
            dialog.show();
        }
        @Override
        protected List<Itemshopl> doInBackground(String... urls) {
            URL url;
            HttpURLConnection connection = null;
            BufferedReader reader = null;
            try {

                urlParameters = "&cat=" + URLEncoder.encode(cat, "UTF-8")+
                                  "&lat="+ URLEncoder.encode(lat, "UTF-8")+
                        "&lon="+ URLEncoder.encode(lon, "UTF-8");
                url = new URL(urls[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded");
                connection.setUseCaches(false);
                connection.setDoInput(true);
                connection.setDoOutput(true);

                DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                InputStream stream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line + "\n");
                }
                String finalJson = buffer.toString();

                List<Itemshopl> itemshoplist = new ArrayList<>();

                arr = new JSONArray(finalJson);
                for (int i = 0; i < arr.length(); i++) {
                    JSONObject obj = arr.getJSONObject(i);
                    /// String state = obj.getString("status");

                    Itemshopl model = new Itemshopl();

                    model.setName(obj.getString("shopname"));
                    model.setcat1(obj.getString("subcat1"));
                    model.setcat2(","+obj.getString("subcat2"));
                    model.setcat3(","+obj.getString("subcat3"));
                    model.setcat4(","+obj.getString("subcat4"));
                    model.setThumbnailUrl(obj.getString("logo"));
                    model.setid(obj.getString("id"));
                    model.setrating(obj.getString("rating"));
                    model.setreview(obj.getString("reviews")+"Reviews");
                    model.setcat(obj.getString("category"));
                    itemshoplist.add(model);
                }
               // cacheThis.writeObject(ShopList.this, "name", "hai");

                return itemshoplist;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<Itemshopl> detailsModels) {
            super.onPostExecute(detailsModels);
            dialog.dismiss();
            if (detailsModels != null && detailsModels.size() > 0) {
                nodata.setVisibility(View.GONE);
                oops.setVisibility(View.GONE);
                rview=(RecyclerView)findViewById(R.id.recycleshop);
                rview.setHasFixedSize(true);
                adapter = new Recyclerviewshopl(getApplicationContext(), detailsModels);
                rview.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
                rview.setAdapter(adapter);
            }else
            {
                nodata.setVisibility(View.VISIBLE);
                oops.setVisibility(View.VISIBLE);
            }
        }
    }}

适配器:

public class Recyclerviewshopl extends RecyclerView.Adapter<Recyclerviewshopl.ViewHolder> {
    private List<Itemshopl> itemList;
    private Context context;

    public Recyclerviewshopl(Context context, List<Itemshopl> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.name.setText(itemList.get(position).getName());
        holder.icons.setImageResource(itemList.get(position).getPhoto());
        holder.cat1.setText(itemList.get(position).getcat1());
        holder.cat2.setText(itemList.get(position).getcat2());
        holder.cat3.setText(itemList.get(position).getcat3());
        holder.cat4.setText(itemList.get(position).getcat4());
        holder.id.setText(itemList.get(position).getid());
        //   holder.review.setText(itemList.get(position).getreview());
        holder.image.setText(itemList.get(position).getimg());
        Glide.with(context).load(itemList.get(position).getThumbnailUrl()).into(holder.icons );
        holder.phone.setText(itemList.get(position).getPhone());
        holder.cat.setText(itemList.get(position).getcat());
        if(itemList.get(position).getrating().equals(""))
        {
            itemList.get(position).getrating().equals("0");
        } else {
            //int value= Integer.parseInt(holder.rate.toString());
            holder.rate.setRating(Float.parseFloat(itemList.get(position).getrating()));
        }
        holder.review.setText(itemList.get(position).getreview());
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent, int i)
    {
        View layoutview = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardshoplist, null);
        ViewHolder sg = new ViewHolder(layoutview);
        return sg;
    }

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

    public  class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, cat1,cat2,cat3,cat4,review,image,id,phone,cat;
        ImageView photo;
        ImageView icons;
        RatingBar rate;

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

            icons = (ImageView) itemView.findViewById(R.id.img1);

            name = (TextView) itemView.findViewById(R.id.shopname);

            cat=(TextView)itemView.findViewById(R.id.cat);
            cat1=(TextView)itemView.findViewById(R.id.cat1);
            cat2=(TextView)itemView.findViewById(R.id.cat2);
            cat3=(TextView)itemView.findViewById(R.id.cat3);
            cat4=(TextView)itemView.findViewById(R.id.cat4);
            review=(TextView)itemView.findViewById(R.id.review);
            image=(TextView)itemView.findViewById(R.id.img);
            id=(TextView)itemView.findViewById(R.id.idvalue);
            phone=(TextView)itemView.findViewById(R.id.phone);
            rate=(RatingBar)itemView.findViewById(R.id.ratingbar);

            itemView.setOnClickListener(new View.OnClickListener() {
                int pos = getAdapterPosition();
                @Override
                public void onClick(View v) {
                    int pos = getAdapterPosition();
                    Intent in = new Intent(v.getContext(),ShopeProfile.class);
                    in.putExtra("id",id.getText().toString());
                    in.putExtra("shopname",name.getText().toString());
                    in.putExtra("phone",phone.getText().toString());
                    in.putExtra("rate",rate.getRating());
                    in.putExtra("cat",cat.getText().toString());
                    v.getContext().startActivity(in);
                }
            });
        }
    }
}

0 个答案:

没有答案