以下是我当前代码中的几个片段,但是当用力滚动时,动画就像它已经到达列表的底部,我想在fb / insta feed中添加progressbar / spinner。
虽然再次向下滚动时新列表会在列表中更新,但我希望转换顺利进行,而不是每次看到Feed都已结束。
TIA。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.userList);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
UserViewModel viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
viewModel.init();
final UserAdapter userUserAdapter = new UserAdapter();
viewModel.userList.observe(this, pagedList -> {
userUserAdapter.setList(pagedList);
});
recyclerView.setAdapter(userUserAdapter);
}
}
DataSource:
public class MyDataSource extends TiledDataSource<User> {
Webservice webservice;
MyDataSource(){
webservice = WebServiceImpl.createGitHubService();
}
@Override
public int countItems() {
return DataSource.COUNT_UNDEFINED;
}
@Override
public List<User> loadRange(int startPosition, int count) {
List<User> gitHubUser = new ArrayList();
try {
Response<List<User>> response = webservice.getUsers(startPosition,count).execute();
if (response.isSuccessful() && response.code() == 200) {
gitHubUser.addAll(response.body());
} else {
Log.e("API CALL", response.message());
}
} catch (IOException e) {
e.printStackTrace();
}
return gitHubUser;
} }
ViewModel:
public class UserViewModel extends ViewModel {
public LiveData<PagedList<User>> userList;
MyDataSource tDataSource;
public UserViewModel() {
}
public void init() {
userList = new LivePagedListProvider<Integer, User>() {
@Override
protected DataSource<Integer, User> createDataSource() {
tDataSource = new MyDataSource();
return tDataSource;
}
}.create(0, new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(20)
.setInitialLoadSizeHint(20)
.build());
}
}
答案 0 :(得分:-1)
创建帮助程序类<BR>
。
将以下代码粘贴到其中。
EndlessRecyclerOnScrollListener
在您的活动中,添加以下代码。
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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 = 5; // 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) {
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);
}
在活动xml中的public class SampleActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
// make progress bar visible.
//call API
}
});
}
}
下方放置一个进度条。
这里,当recyclerview
滚动到底部时,将调用onLoadMore()。
有关详细信息,请参阅https://gist.github.com/ssinss/e06f12ef66c51252563e