我正在实施无尽的recyclerView
并使用this库进行滑动。我遇到的问题是,一次只能在recyclerview
中滑动多个项目。如果我拖动recyclerview
的第二个视图并且如果第一个视图打开则应该隐藏意味着recyclerview
中只有一个项目显示相同的时间。下面是我的适配器
public class Adapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<MyModel> mDataset;
// The minimum amount of items to have below your current scroll position before loading more.
private int visibleThreshold = 2;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
private Context context;
public Adapter(List<MyModel> myDataSet, RecyclerView recyclerView) {
mDataset = myDataSet;
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 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 itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_row, parent, false);
vh = new TextViewHolder(itemView);
} else {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.progressbar_item, parent, false);
vh = new ProgressViewHolder(v);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof TextViewHolder) {
MyModel model = mDataset.get(position);
((TextViewHolder) holder).tvFname.setText(model.getStrFname());
((TextViewHolder) holder).tvLname.setText(model.getStrLname());
// SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(Adapter.this);
// mItemManger.bindView(holder.itemView, position);
}
else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
public void setLoaded() {
loading = false;
}
@Override
public int getItemCount() {
return mDataset.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
public interface OnLoadMoreListener {
void onLoadMore();
}
public static class TextViewHolder extends RecyclerView.ViewHolder {
public TextView tvFname, tvLname;
public TextViewHolder(View v) {
super(v);
tvFname = (TextView) v.findViewById(R.id.tvName);
tvLname = (TextView) v.findViewById(R.id.tvEmailId);
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
}
}
}
这是我的MainActivity
。
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
public SearchView search;
private ArrayList<MyModel> arrayList = new ArrayList<MyModel>();
private Adapter mAdapter;
protected Handler handler;
private MyModel model;
int test=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRecycler();
}
private void setRecycler() {
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
createlist(); // in this method, Create a list of items.
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//add devider to recycleview.
mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext()));
mAdapter = new Adapter(arrayList, mRecyclerView);
mRecyclerView.setAdapter(mAdapter);
handler = new Handler();
mAdapter.setOnLoadMoreListener(new Adapter.OnLoadMoreListener() {
@Override
public void onLoadMore() {
//add progress item
arrayList.add(null);
mAdapter.notifyItemInserted(arrayList.size() - 1);
handler.postDelayed(new Runnable() {
@Override
public void run() {
//remove progress item
arrayList.remove(arrayList.size() - 1);
mAdapter.notifyItemRemoved(arrayList.size());
//add items one by one
// arrayList.add("Item " + (arrayList.size() + 1));
createlist();
mAdapter.notifyItemInserted(arrayList.size());
mAdapter.setLoaded();
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
}, 2000);
System.out.println("load");
}
});
}
// this method is used to create list of items.
public void createlist() {
for (int i = 1; i <= 20; i++) {
model = new MyModel();
model.setStrFname("Testing"+test);
model.setStrLname("Testing"+i+"@gmail.com");
arrayList.add(model);
test++;
}
}
}
答案 0 :(得分:1)
我使用this库和[here]解决了我的问题(https://drive.google.com/file/d/0B8nJi3D_SXMzTzFVMm52WjR0UVk/view?usp=sharing )是我的完整代码,用于加载更多或无尽的recyclerview,使用滑动布局,这可能对将来有所帮助。