我已经制作了一个View Pager以及一些片段,在这些片段中,我使用了Recycler视图来显示从互联网上获取的动态数据。我也使用了Pull to Refresh来处理我的片段。有时由于互联网连接速度慢当我的片段页面被加载时,它会显示我的回收器视图数据的awkard行为。数据在回收器视图中加载,但它可以在我的电话屏幕后面滚动并且回收器视图"仍然"图像显示在前面没有滚动效果。因此我无法正常运行我的Recycler视图。当我尝试滚动后面形成的页面滚动而且首页是"仍然&#34 ;。 可能是什么问题???
片段文件
package com.global.market;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Asia extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
// ProgressDialog pDialog;
private RecyclerView mRecyclerView;
//private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private CountryDataAdapter adapter;
private ArrayList<CountriesData> data;
private SwipeRefreshLayout swipe;
public Asia(){
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
// setContentView(R.layout.countrydata);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.countrydata, container, false);
// pDialog = new ProgressDialog(this.getContext(), ProgressDialog.THEME_HOLO_LIGHT);
//mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view1);
mRecyclerView = (RecyclerView)v.findViewById(R.id.my_recycler_view1);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(adapter);
// loadJSON();
swipe=(SwipeRefreshLayout)v.findViewById(R.id.swipe_refresh_layout);
swipe.setOnRefreshListener(this);
swipe.setColorSchemeColors(R.color.b, R.color.p, R.color.g, R.color.o);
swipe.post(new Runnable() {
@Override
public void run() {
swipe.setRefreshing(true);
loadRetro();
}
}
);
return v;
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refresh) {
boolean x=isNetworkAvailable();
if(x)
// pDialog.setMessage("Please wait...");
// pDialog.setCancelable(false);
// pDialog.show();
// swipe.setRefreshing(true);
{
loadRetro();
return true;
}
else Toast.makeText(getActivity(), "No Internet Connection!",
Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onRefresh(){
// swipe.setRefreshing(true);
loadRetro();
//pDialog.dismiss();
}
public void loadRetro(){
swipe.setRefreshing(true);
// pDialog.setMessage("Please wait...");
// pDialog.setCancelable(false);
// pDialog.show();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.appuonline.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonrequest request = retrofit.create(jsonrequest.class);
Call<JsonResponse> call = request.getJSON();
call.enqueue(new Callback<JsonResponse>() {
@Override
public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
try{
JsonResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getData()));
adapter = new CountryDataAdapter(data);
mRecyclerView.setAdapter(adapter);
// if (pDialog.isShowing())
// pDialog.dismiss();
swipe.setRefreshing(false);
}catch (Exception e){}
}
@Override
public void onFailure(Call<JsonResponse> call, Throwable t) {
try{
Log.d("Error1", "no connection");
swipe.setRefreshing(false);}
catch(Exception e){}
}
});
}
}
请告诉我是否需要其他代码才能进一步澄清
这是我的截图。
您在上面的屏幕截图中看到的内容有时会变成“#34;仍然&#34;并且整个内容在此页面后面变为活动状态,并且首页内容不起作用。
答案 0 :(得分:2)
您再次在new Adapter
方法中设置onRefresh
,这是不推荐的。尝试使用以下方法在适配器中添加新项目:
在onCreateView
中使用以下内容初始化您的适配器:
adapter = new MyAdapter(getActivity());
还将此方法放在适配器中:
public void addItems(List<Object> newList){
//put this method in your adapter
adapterList = newList;
notifyDatasetChanged();
//it's better to call notifyItemRangeInserted()
}
在新数据发布时你需要刷新它的适配器。像这样的东西:
adapter.addItems(your_new_fetch_list_data);
retrofit
使用this设计模式可以帮助您的代码变得更清晰。