2016-09-06 8 views
3

私のアプリケーション(RecyclerView)にサーバーからデータをロードして、このジョブでアプリケーションが起動すると10個の投稿が表示され、recyclerViewが別の投稿を表示します。RecyclerViewにレイアウトを表示する方法

私は以下のコードを書いていますが、他の10個の投稿を取得すると、が表示されません読み込みレイアウト!私は他の10の記事を得るとき、最初にを表示するのレイアウトを表示し、次に他を表示する10 投稿

のために、私はRetrofit v2を使用し、カスタムのためにrecyclerViewためEndless methosが、私はこのクラスを使用してインターネットに接続:EndLess Class

My活動コード:

public class Category_page extends AppCompatActivity implements ConnectivityReceiver.ConnectivityReceiverListener { 

    private static final long RIPPLE_DURATION = 250; 
    private Toolbar toolbar; 
    private TextView toolbar_title; 
    private ImageView toolbar_menuImage; 
    private Button categoryCheckNet_button; 
    private RelativeLayout root; 
    private CategoryAdapter mAdapter; 
    private RecyclerView cat_recyclerView; 
    private LinearLayoutManager mLayoutManager; 
    private RelativeLayout loadLayout, checkNetLayout; 
    private String catTitle = ""; 
    private Integer catID; 
    private Bundle bundle; 
    private int pageCount = 1; 
    private Context context; 

    private List<R_CatModel> models; 

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

     // Hide StatusBar color 
     getWindow().getDecorView().setSystemUiVisibility(
       View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 

     // Initializing 
     context = Category_page.this; 
     toolbar = (Toolbar) findViewById(R.id.category_toolbar); 
     cat_recyclerView = (RecyclerView) findViewById(R.id.category_recycler); 
     toolbar_title = (TextView) toolbar.findViewById(R.id.toolbar_pages_title); 
     mLayoutManager = new LinearLayoutManager(this); 
     root = (RelativeLayout) findViewById(R.id.category_root); 
     loadLayout = (RelativeLayout) findViewById(R.id.category_empty_layout); 
     checkNetLayout = (RelativeLayout) findViewById(R.id.category_checkInternet_layout); 
     categoryCheckNet_button = (Button) checkNetLayout.findViewById(R.id.checkNet_button); 
     // Toolbar 
     setSupportActionBar(toolbar); 
     if (toolbar != null) { 
      getSupportActionBar().setTitle(""); 
     } 

     // Receive Data 
     bundle = getIntent().getExtras(); 
     catID = bundle.getInt("categoryID"); 
     if (bundle != null) { 
      catTitle = bundle.getString("categoryTitle"); 
     } 
     if (catTitle != null) { 
      toolbar_title.setText(catTitle); 
     } 

     // Load Data 
     loadData(); 

     // Load Progress 
     loadLayout.setVisibility(View.VISIBLE); 

     // Menu 
     View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.menu_layout, null); 
     root.addView(guillotineMenu); 
     toolbar_menuImage = (ImageView) toolbar.findViewById(R.id.toolbar_pages_logo); 
     new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.menu_layout_image), toolbar_menuImage) 
       .setStartDelay(RIPPLE_DURATION) 
       .setActionBarViewForAnimation(toolbar) 
       .setClosedOnStart(true) 
       .build(); 
     // RecyclerView 
     cat_recyclerView.setLayoutManager(mLayoutManager); 
     cat_recyclerView.setHasFixedSize(true); 
     cat_recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) { 
      @Override 
      public void onLoadMore(int current_page) { 

       Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class); 
       Call<R_CatModelResponse> call = apiInterface.getCatMoreResponse(catID, current_page); 

       call.enqueue(new Callback<R_CatModelResponse>() { 
        @Override 
        public void onResponse(Call<R_CatModelResponse> call, Response<R_CatModelResponse> response) { 

         if (response != null) { 
          mAdapter.addNewItem(response.body().getCat_posts()); 
          //loadLayout.setVisibility(View.GONE); 
         } else { 
          //loadLayout.setVisibility(View.VISIBLE); 
          Toast.makeText(Category_page.this, "Error 2", Toast.LENGTH_SHORT).show(); 
          TastyToast.makeText(context, "خطایی رخ داده است", TastyToast.LENGTH_LONG, TastyToast.ERROR); 
         } 
        } 

        @Override 
        public void onFailure(Call<R_CatModelResponse> call, Throwable t) { 

        } 
       }); 
      } 
     }); 

    } 

    private void loadData() { 
     boolean isConnected = ConnectivityReceiver.isConnected(); 

     retrofitData(isConnected); 
    } 

    private void retrofitData(boolean isConnect) { 

     if (isConnect) { 
      Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class); 
      Call<R_CatModelResponse> call = apiInterface.getCatResponse(catID); 

      call.enqueue(new Callback<R_CatModelResponse>() { 
       @Override 
       public void onResponse(Call<R_CatModelResponse> call, Response<R_CatModelResponse> response) { 

        if (response != null) { 
         models = response.body().getCat_posts(); 

         mAdapter = new CategoryAdapter(context, cat_recyclerView, models); 
         cat_recyclerView.setAdapter(mAdapter); 
         loadLayout.setVisibility(View.GONE); 

        } else { 
         //loadLayout.setVisibility(View.VISIBLE); 
         Toast.makeText(Category_page.this, "Error 2", Toast.LENGTH_SHORT).show(); 
         TastyToast.makeText(context, "خطایی رخ داده است", TastyToast.LENGTH_LONG, TastyToast.ERROR); 
        } 

        checkNetLayout.setVisibility(View.GONE); 
       } 

       @Override 
       public void onFailure(Call<R_CatModelResponse> call, Throwable t) { 

        //loadLayout.setVisibility(View.VISIBLE); 
        //TastyToast.makeText(context, "لطفا برنامه را مجددا باز کنید", TastyToast.LENGTH_LONG, TastyToast.ERROR); 
        Toast.makeText(Category_page.this, "Error 2", Toast.LENGTH_SHORT).show(); 
        //Cat_EmptyLayout.setVisibility(View.VISIBLE); 
        Log.e("CatResponseError", "Error : " + t); 

       } 
      }); 
     } else { 
      //loadLayout.setVisibility(View.GONE); 
      checkNetLayout.setVisibility(View.VISIBLE); 
      if (mAdapter != null) { 
       mAdapter.clear(); 
       cat_recyclerView.setAdapter(mAdapter); 
      } 
      categoryCheckNet_button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        loadData(); 
       } 
      }); 
     } 
    } 

    public void post_back(View view) { 
     onBackPressed(); 
    } 

    @Override 
    protected void attachBaseContext(Context newBase) { 
     super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     // register connection status listener 
     MyApplication.getInstance().setConnectivityListener(this); 
    } 

    @Override 
    public void onNetworkConnectionChanged(boolean isConnected) { 
     retrofitData(isConnected); 
    } 
} 

マイアダプターコード:

public class CategoryAdapter extends RecyclerView.Adapter { 

    private List<R_CatModel> mDateSet; 
    private Context mContext; 

    private final int VIEW_ITEM = 1; 
    private final int VIEW_PROG = 0; 

    // The minimum amount of items to have below your current scroll position 
    // before loading more. 
    private int visibleThreshold = 10; 
    private int lastVisibleItem, totalItemCount; 
    private boolean loading; 
    private OnLoadMoreListener onLoadMoreListener; 

    public CategoryAdapter(Context context, RecyclerView recyclerView, List<R_CatModel> dataSet) { 
     this.mContext = context; 
     this.mDateSet = dataSet; 

     if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) { 

      final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView 
        .getLayoutManager(); 
      recyclerView 
        .addOnScrollListener(new RecyclerView.OnScrollListener() { 
         @Override 
         public void onScrolled(RecyclerView recyclerView, 
               int dx, int dy) { 
          super.onScrolled(recyclerView, dx, dy); 
          totalItemCount = linearLayoutManager.getItemCount(); 
          lastVisibleItem = linearLayoutManager 
            .findLastVisibleItemPosition(); 
          if (!loading 
            && totalItemCount <= (lastVisibleItem + visibleThreshold)) { 
           // End has been reached 
           // Do something 
           if (onLoadMoreListener != null) { 
            onLoadMoreListener.onLoadMore(); 
           } 
           loading = true; 
          } 
         } 
        }); 
     } 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return mDateSet.get(position) != null ? VIEW_ITEM : VIEW_PROG; 
    } 

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

     RecyclerView.ViewHolder vh; 
     if (viewType == VIEW_ITEM) { 
      View v = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.post_card_layout, parent, false); 

      vh = new DataViewHolder(v); 
     } else { 
      View v = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.progressbar_item, parent, false); 

      vh = new ProgressViewHolder(v); 
     } 
     return vh; 
    } 

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

     if (holder instanceof DataViewHolder) { 
      ((DataViewHolder) holder).main_post_title.setText(Html.fromHtml(mDateSet.get(position).getTitle())); 

      Glide.with(mContext) 
        .load(mDateSet.get(position).getThumbnail_images().getMedium().getUrl()) 
        .placeholder(R.drawable.post_image) 
        .crossFade() 
        .override(600, 350) 
        .into(((DataViewHolder) holder).main_post_image); 

      ((DataViewHolder) holder).main_post_content.setText(Html.fromHtml(mDateSet.get(position).getContent())); 

      // Convert Date //////// 
      String date = mDateSet.get(position).getDate(); 
      String[] parts = date.split(" "); 
      String datePart = parts[0]; 
      String timePart = parts[1]; 

      int year; 
      int month; 
      int day; 

      String[] dateParts = datePart.split("-"); 
      year = Integer.parseInt(dateParts[0]); 
      month = Integer.parseInt(dateParts[1]); 
      day = Integer.parseInt(dateParts[2]); 

      JalaliCalendar.YearMonthDate georgianDate = new JalaliCalendar.YearMonthDate(year, month, day); 
      JalaliCalendar.YearMonthDate JalaliDate = JalaliCalendar.gregorianToJalali(georgianDate); 
      String jalaliDateTime = JalaliDate.toString(); 

      ((DataViewHolder) holder).main_dateTime.setText(jalaliDateTime); 
      //////////////////////// 

      ((DataViewHolder) holder).main_author.setText(Html.fromHtml(mDateSet.get(position).getCatAuthor().getAuthorName())); 
      ((DataViewHolder) holder).main_author.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        R_CatModel model = mDateSet.get(position); 
        v.getContext().startActivity(new Intent(v.getContext(), Profile_page.class)); 
        //.putExtra("author", model.getAuthor()) 
        //.putExtra("authorID", model.getAuthorID()) 
        //.putExtra("authorStatus", model.getAuthorStatus())); 
       } 
      }); 

      ((DataViewHolder) holder).main_category.setText(Html.fromHtml(String.valueOf(mDateSet.get(position).getCategories().get(0).getCatTitle()))); 
      ((DataViewHolder) holder).main_category.setTextColor(mContext.getResources().getColor(R.color.colorAccent)); 

      ((DataViewHolder) holder).main_post_image.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        int pos = holder.getPosition(); 
        R_CatModel model = mDateSet.get(pos); 
        v.getContext().startActivity(new Intent(v.getContext(), PostShow_page.class) 
          .putExtra("title", model.getTitle()) 
          .putExtra("image", model.getThumbnail()) 
          .putExtra("content", model.getContent()) 
          .putExtra("dateTime", model.getDate()) 
          //.putExtra("author", model.getAuthor()) 
          .putExtra("category", model.getTitle())); 

       } 
      }); 

     } else { 
      ((ProgressViewHolder) holder).progressBar.setVisibility(View.VISIBLE); 
     } 
    } 

    public void setLoaded() { 
     loading = false; 
    } 

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

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) { 
     this.onLoadMoreListener = onLoadMoreListener; 
    } 

    public void remove(int position) { 
     mDateSet.remove(position); 
     notifyItemRemoved(position); 
    } 

    public void clear() { 
     mDateSet.clear(); 
     notifyDataSetChanged(); 
    } 

    public void addNewItem(List<R_CatModel> newContent) { 
     int start = this.mDateSet.size();//contents is a List of your items initialize it your constructor 
     int end = newContent.size(); 
     mDateSet.addAll(newContent); 
     notifyItemRangeInserted(start + 1, end); 
    } 


    public void add(List<R_CatModel> models) { 
     mDateSet.addAll(models); 
     notifyDataSetChanged(); 
    } 

    public void update(List<R_CatModel> models) { 
     mDateSet.clear(); 
     mDateSet.addAll(models); 
     notifyDataSetChanged(); 
    } 

    public class DataViewHolder extends RecyclerView.ViewHolder { 

     private TextView main_post_title, main_post_content, main_dateTime, main_author, main_category; 
     private ImageView main_post_image; 

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

      main_post_title = (TextView) itemView.findViewById(R.id.post_content_title); 
      main_post_image = (ImageView) itemView.findViewById(R.id.post_picture_image); 
      main_post_content = (TextView) itemView.findViewById(R.id.post_content_text); 
      main_dateTime = (TextView) itemView.findViewById(R.id.post_date_text); 
      main_author = (TextView) itemView.findViewById(R.id.post_name_text); 
      main_category = (TextView) itemView.findViewById(R.id.post_category_text); 
     } 
    } 

    public static class ProgressViewHolder extends RecyclerView.ViewHolder { 
     public AVLoadingIndicatorView progressBar; 

     public ProgressViewHolder(View v) { 
      super(v); 
      progressBar = (AVLoadingIndicatorView) v.findViewById(R.id.avloadingIndicatorView); 
     } 
    } 
} 

他の10個の投稿を受け取ったら、レイアウトを表示する方法を教えてください。

答えて

0

私はあなたにこの問題を解決するための簡単な方法を説明してみましょう:

の1-あなたRecyclerViewの下にリニアprogressBar入れgoneで、デフォルトではその可視性:

のご onLoadMore変更の可視性で
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/gray_lightest"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/lv_paging" 
     style="@style/RecycleView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="4dp" 
     android:paddingTop="4dp" 
     tools:listitem="@layout/row_list_base" /> 

    <com.rey.material.widget.ProgressView 
     android:id="@+id/pvl_loading" 
     style="@style/LinearProgress" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

2 - VisibleからProgressBar

scrollListener = new EndlessRecyclerOnScrollListener((LinearLayoutManager) mRecyclerView.getLayoutManager()) { 
      @Override 
      public void onLoadMore(int page) { 
       progressBar.setVisiblity(View.VISIBLE); 
       callWebService(page); 
      } 
     }; 
     mRecyclerView.addOnScrollListener(scrollListener); 

、3- goneにあなたの要求仕上げの変更の可視性:

public void onResponse(Call<YOUR_OBJECT> call, Response<YOUR_OBJECT> response) {  
       progressBar.setVisiblity(View.GONE); 
       processResponse(response); 
      } 

代替方法:あなたのアダプタでプログレスバーを処理したい場合は、完全なコードhereを参照してください。コードの主キー部分:

1-アダプタにshowLoadingViewメソッドを追加しますへ

public void showLoadingView() { 
     showLoadingView = true; 
     notifyItemChanged(0); 
    } 

2-変化getViewType

@Override 
    public int getItemViewType(int position) { 
     if (showLoadingView && position == 0) { 
      return VIEW_TYPE_LOADER; 
     } else { 
      return VIEW_TYPE_DEFAULT; 
     } 
    } 
0

あなたは常に、あなたのリストの最後に項目としてこのレイアウトを保持することができます例えば は、レイアウトは常にrecyclerViewであなたの無限のアイテムとなってロードする、このトリックにより

if (position = dataList.size-1) //it means we are at the end of list 
    add your loading layout as an item in recyclerView 

を確認し、スクロールダウンあなたは最初にあなたの読み込みレイアウトを見て、最後に他の投稿を追加してください、そして、あなたの読み込みレイアウトは、リストの最後に行きます。

+0

または別のトリックの底にこのレイアウトを追加することですあなたのrecyclerは関連するXMLで表示され、リストの最後に来ると可視/不可視になります – MehDi

+0

私にコードを送ることができますか?私は素人です、あなたは私にコードを送ることができますか? –

+0

申し訳ありません私は最近忙しくて2つのプロジェクトに取り組んでいますが、おそらく次の週にお手伝いできます:) – MehDi

関連する問題