グリッドのRecycleViewで「スクロールしてさらに読み込む」の不確定な円形インジケータを取得する方法は?
パターンはそこで説明されています。 http://www.google.com/design/spec/components/progress-activity.html#progress-activity-behavior 「2段階の負荷」と「例2:上にスクロールしてさらに読み込む」サンプル動画。
私は新しいRecyclerViewを使用してこれを達成しようとしていますが、それを行うには「あまりにもハックのない」方法を見つけることができません。最初にグリッドの行全体をカバーするフッターを追加する方法がないためです。助言がありますか?
それはとても簡単です。
解決策は、LinearLayoutManager
でGridLayoutManager
と同じアプローチを使用し、次にsetSpanSizeLookup
でLayoutManager
メソッドを次のように使用することです。
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(myAdapter.getItemViewType(position)){
case MyAdapter.VIEW_TYPES.Product:
return 1;
case MyAdapter.VIEW_TYPES.Progress:
return 2; //number of columns of the grid
default:
return -1;
}
}
});
これにより、アイテムがグリッドの行全体に自動的に表示されます(行が完全に空でない場合、このアイテムは次の行に移動します)。
以下のソリューションにはいくつかの潜在的な問題と制限があります。修正されたソリューションについてはこちらを確認してください下部のProgressBarでEndless Scroll RecyclerViewにアイテムを追加する
ここに私が最近思いついた解決策があります:2つのタイプのアイテムを備えたRecyclerViewを持ち、1つは通常のアイテム、2つ目はプログレスバーです。アイデアからコード例まで
progress_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<ProgressBar
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:id="@+id/progressBar"
Android:indeterminate="true"
style="@Android:style/Widget.Holo.ProgressBar"
Android:layout_gravity="center_horizontal"/>
</LinearLayout>
activity_main.xml
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
xmlns:ring="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context=".MainActivity">
<Android.support.v7.widget.RecyclerView
Android:id="@+id/my_recycler_view"
Android:scrollbars="vertical"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>
</RelativeLayout>
EndlessRecyclerOnScrollListener.Java
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 1; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
private LinearLayoutManager mLinearLayoutManager;
public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal+1) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
MyAdapter.Java
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<String> mDataset;
public static class TextViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public TextViewHolder(View v) {
super(v);
mTextView = (TextView)v.findViewById(Android.R.id.text1);
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar)v.findViewById(R.id.progressBar);
}
}
public MyAdapter(List<String> myDataset) {
mDataset = myDataset;
}
@Override
public int getItemViewType(int position) {
return mDataset.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(Android.R.layout.simple_list_item_1, parent, false);
vh = new TextViewHolder(v);
}else {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.progress_item, parent, false);
vh = new ProgressViewHolder(v);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof TextViewHolder){
((TextViewHolder)holder).mTextView.setText(mDataset.get(position));
}else{
((ProgressViewHolder)holder).progressBar.setIndeterminate(true);
}
}
@Override
public int getItemCount() {
return mDataset.size();
}
}
そして最後にMainActivity.Java
package virtoos.com.testapps;
import Android.app.Activity;
import Android.os.Bundle;
import Android.os.Handler;
import Android.support.v7.widget.LinearLayoutManager;
import Android.support.v7.widget.RecyclerView;
import Java.util.ArrayList;
import Java.util.List;
public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
private MyAdapter mAdapter;
private final List<String> myDataset = new ArrayList<>();
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
addItems(20);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
//mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
@Override
public void onLoadMore(int current_page) {
//add progress item
myDataset.add(null);
mAdapter.notifyItemInserted(myDataset.size());
handler.postDelayed(new Runnable() {
@Override
public void run() {
//remove progress item
myDataset.remove(myDataset.size() - 1);
mAdapter.notifyItemRemoved(myDataset.size());
//add items one by one
for (int i = 0; i < 15; i++) {
myDataset.add("Item"+(myDataset.size()+1));
mAdapter.notifyItemInserted(myDataset.size());
}
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
}, 2000);
System.out.println("load");
}
});
}
}
以下は、_RecyclerView.Adapter
_の@Vilen Melkumyanの回答を少し修正したものです。 EndlessRecyclerOnScrollListener
は、データの読み込みに任意の方法で使用できます。また、いつでもフッターを有効または無効にできます。
PS:GridLayoutManager
で機能しました。
_public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_ITEM = 1;
private final int VIEW_TYPE_PROGRESSBAR = 0;
private boolean isFooterEnabled = true;
private List<String> items;
public static class TextViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public TextViewHolder(View v) {
super(v);
mTextView = (TextView)v.findViewById(Android.R.id.text1);
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar)v.findViewById(R.id.progressBar);
}
}
public MyRecyclerViewAdapter(List<String> myDataset) {
items = myDataset;
}
@Override
public int getItemCount() {
return (isFooterEnabled) ? items.size() + 1 : items.size();
}
@Override
public int getItemViewType(int position) {
return (isFooterEnabled && position >= items.size() ) ? VIEW_TYPE_PROGRESSBAR : VIEW_TYPE_ITEM;
}
/**
* Enable or disable footer (Default is true)
*
* @param isEnabled boolean to turn on or off footer.
*/
public void enableFooter(boolean isEnabled){
this.isFooterEnabled = isEnabled;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
if(viewType== VIEW_TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext())
.inflate(Android.R.layout.simple_list_item_1, parent, false);
vh = new TextViewHolder(v);
}else {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.progressbar, parent, false);
vh = new ProgressViewHolder(v);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof ProgressViewHolder){
((ProgressViewHolder)holder).progressBar.setIndeterminate(true);
} else if(items.size() > 0 && position < items.size()) {
((TextViewHolder)holder).mTextView.setText(items.get(position));
}
}
}
_
私の2セント、平和!
https://github.com/ramirodo/endless-recycler-view-adapter または https://bintray.com/ramiro/Android/endless-recycler-で解決策を確認してくださいview-adapter 。そこには例があり、プロジェクトにライブラリを設定する手順もあります。
必要なメソッドを実装するだけで、リサイクラビューアダプタを拡張する必要があります。また、進行状況フッターのレイアウトを設定できます。
アダプタ内にコードを挿入することにより、ブロンクスの答えを簡素化できます。
public class ArticleGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_ITEM = 0;
private final int VIEW_LOADING = 1;
private Context mContext;
private List<Article> mArticles = new ArrayList<>();
private RecyclerView mRecyclerView;
private GridLayoutManager mManager;
public ArticleGridAdapter(Context context, List<Article> articles, RecyclerView recyclerView) {
this.mContext = context;
this.mArticles = articles;
this.mRecyclerView = recyclerView;
this.mManager = (GridLayoutManager) recyclerView.getLayoutManager();
mManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return getItemViewType(position) == VIEW_LOADING ? mManager.getSpanCount() : 1;
}
});
}
}