2017-01-10 83 views
0

我是新來的android。我有問題,我不知道如何在我的項目中添加無盡的recyclerview。以下是我的項目代碼。現在在我的recyclerview中,我可以使用Retrofit 2從服務器獲取JSON。無盡的recyclerview android with retrofit 2

這是適配器。

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


private ArrayList<Rec> recs; 

public DataAdapter(ArrayList<Rec> recs) { 
    this.recs = recs; 
} 


@Override 
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false); 
    return new ViewHolder(view); 
} 

@Override 
public void onBindViewHolder(final DataAdapter.ViewHolder viewHolder,final int i) { 
    String decode ="^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$"; 

    viewHolder.tv_link.setText(recs.get(i).getLink()); 
    viewHolder.imageText.setText(recs.get(i).getImage()); 

    Bitmap bmimage = null; 
    if(recs.get(i).getImage().toString().matches(decode)) { 
     byte[] imageByteArray = decodeImage(recs.get(i).getImage().toString()); 
     bmimage = BitmapFactory.decodeByteArray(imageByteArray, 0, 
       imageByteArray.length); 
     viewHolder.image.setImageBitmap(bmimage); 
    } 
     else { 
     Picasso.with(viewHolder.imageText.getContext()).load(recs.get(i).getImage()).fit().centerInside() .into(viewHolder.image); 
    } 
} 

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

public class ViewHolder extends RecyclerView.ViewHolder{ 

    private TextView tv_id,tv_title,tv_link,imageText,body; 
    private ImageView image; 
    public ViewHolder(View view) { 
     super(view); 




     tv_link = (TextView)view.findViewById(R.id.tv_link); 
     imageText = (TextView)view.findViewById(R.id.imageText); 
     image = (ImageView)view.findViewById(R.id.image); 


     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       Intent intent = new Intent(view.getContext(), PromotionDetails.class); 

       intent.putExtra("link", tv_link.getText().toString()); 

       view.getContext().startActivity(intent); 



      } 
     }); 
    } 



} 

}

而下面是我的活動課,

public class MainActivity extends AppCompatActivity { 
    private RecyclerView recyclerView; 
    private ArrayList<Rec> data; 
    private DataAdapter adapter; 
    private Button BtnHistory,BtnBiller,BtnPromo; 
    private static final long CACHE_SIZE = 10 * 1024 * 1024; 
    float x1,x2; 
    float y1, y2; 


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


     BtnHistory.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Context context = getApplicationContext(); 
       CharSequence text = "Please Login!!!"; 
       int duration = Toast.LENGTH_LONG; 

       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 

      } 
     }); 

     BtnBiller.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       BtnBiller.setBackgroundResource(R.drawable.btn_find_selected); 
       BtnPromo.setBackgroundResource(R.drawable.btn_promotion); 
       Intent i = new Intent(getApplicationContext(), 
         BillerCodeActivity.class); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(i); 
       finish(); 
      } 
     }); 

     BtnPromo.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       BtnPromo.setBackgroundResource(R.drawable.btn_promotion_selected); 
       Intent i = new Intent(getApplicationContext(), 
         MainActivity.class); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(i); 
       finish(); 
      } 
     }); 
    } 



    private void initViews(){ 
      BtnHistory =(Button) findViewById(R.id.btn_history); 
      BtnBiller =(Button) findViewById(R.id.btn_biller); 
      BtnPromo =(Button) findViewById(R.id.btn_promo); 
      recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view); 
      recyclerView.setHasFixedSize(true); 
      RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); 
      recyclerView.setLayoutManager(layoutManager); 

      getPromotions(0,3); 
    } 

    private void getPromotions(int a,int b){ 

      OkHttpClient client = new OkHttpClient.Builder() 
       .connectTimeout(30, TimeUnit.SECONDS) 
       .readTimeout(30, TimeUnit.SECONDS) 
       .writeTimeout(30, TimeUnit.SECONDS) 
       .cache(new Cache(getApplication().getCacheDir(), CACHE_SIZE)) 
       .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) 
       .build(); 

      final ProgressDialog loading = ProgressDialog.show(this,"Fetching Data","Please wait...",false,false); 

      /** Runnable progressRunnable = new Runnable() { 

      @Override 
      public void run() { 
       loading.cancel(); 
      } 
      }; 

      Handler pdCanceller = new Handler(); 
      pdCanceller.postDelayed(progressRunnable, 10000);*/ 

      Retrofit retrofit = new Retrofit.Builder() 
        .baseUrl("http://jpmockapi.azurewebsites.net/") 
        .addConverterFactory(GsonConverterFactory.create()) 
        .client(client) 
        .build(); 
      //Log.d(String.valueOf(client),String.valueOf(client)); 
      RequestInterface request = retrofit.create(RequestInterface.class); 
      Call<JsonResponse> call = request.getPromotions(a,b); 
      call.enqueue(new Callback<JsonResponse>() { 
        @Override 
        public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) { 
         if(response.isSuccess()) { 
          loading.dismiss(); 
          JsonResponse jsonResponse = response.body(); 
          data = new ArrayList<>(Arrays.asList(jsonResponse.getPromotion())); 
          adapter = new DataAdapter(data); 
          recyclerView.setAdapter(adapter); 
          //DBCreate(); 
          //SubmitDataToSQLiteDB(); 

         } 
         else{ 
          loading.dismiss(); 
          Context context = getApplicationContext(); 
          CharSequence text = "Having Problem On Fetching Data!!! Try Again..."; 
          int duration = Toast.LENGTH_LONG; 

          Toast toast = Toast.makeText(context, text, duration); 
          toast.show(); 

         } 

        } 

        @Override 
        public void onFailure(Call<JsonResponse> call, Throwable t) { 
         loading.dismiss(); 
         Log.d("Error",t.getMessage()); 
         /**Context context = getApplicationContext(); 
         CharSequence text = "No Network Connection!!! Try Again..."; 
         int duration = Toast.LENGTH_SHORT; 

         Toast toast = Toast.makeText(context, text, duration); 
         toast.show();*/ 

        } 


      }); 

    } 

    @Override 
    public void onStop(){ 
     super.onStop(); 
    } 

    @Override 
    public void onBackPressed() { 

     Intent i = new Intent(getApplicationContext(), 
       Start.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(i); 
     finish(); 

    } 

}

這是我改造GET

public interface RequestInterface { 

@GET("/cmsapi/promos/{startrowindex}/{maxcount}") 

Call<JsonResponse>getPromotions(@Path("startrowindex") int a, 
           @Path("maxcount") int b); 

}

我想要做的就是當像滾動瀏覽器那樣瀏覽recycleview時獲取JSON。希望有人會幫助我。謝謝。 `

+0

試試這個工作https://codentrick.com/load-more-recyclerview-bottom-progressbar/ –

回答

0

你可以試試這個。

Learn2Crack link

希望這將解決您的問題。

+0

嗨,歡迎來到Stack Overflow。儘管歡迎有用的外部資源鏈接,但也請引用您鏈接到的頁面的關鍵摘錄*。這是因爲如果網站出現故障或頁面被重新移動,那麼這個答案不會變得無用。 –