在棉花糖版本下面无法工作的无尽滚动

时间:2017-11-29 06:00:07

标签: android

error Image我实现了一个片段类,其中数据在凌空响应中获取,我还实现了RecyclerView.OnScrollChangeListener。数据将在滚动时出现,并且它在棉花糖版本上正常工作,但不能在以下版本上工作,即Kitkat,Lollipop。当我试图在下面的版本上运行它会给出一个错误,即运行此应用程序的最大版本是23.请帮我解决这类错误,谢谢你提前。

下面是我的Fragment1类。

public class Fragment1 extends Fragment implements RecyclerView.OnScrollChangeListener{
private List<SuperHero> listSuperHeroes;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
public ProgressBar progressBar;
private RequestQueue requestQueue;
private int requestCount = 1;
public Fragment1() {}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.activity_main, container, false);
    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);
    listSuperHeroes = new ArrayList<>();
    requestQueue = Volley.newRequestQueue(getContext());
    getData();
    recyclerView.setOnScrollChangeListener(Fragment1.this);
    adapter = new CardAdapter(listSuperHeroes, getActivity());
    recyclerView.setAdapter(adapter);
    progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
    return view;
}
private JsonArrayRequest getDataFromServer(int requestCount) {
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.e("response",response.toString());
                    parseData(response);
                    progressBar.setVisibility(View.GONE);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
                }
            });

    return jsonArrayRequest;
}

private void getData() {
    requestQueue.add(getDataFromServer(requestCount));
    requestCount++;
}

private void parseData(JSONArray array) {
    for (int i = 0; i < array.length(); i++) {
        Log.e("array",array.toString().trim());
        SuperHero superHero = new SuperHero();
        JSONObject json = null;
        try {
            json = array.getJSONObject(i);
            superHero.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
            superHero.setMglId(json.getString(Config.TAG_MGLID));
            superHero.setAgeHeight(json.getString(Config.TAG_AGEHEIGHT));
            superHero.setCommunity(json.getString(Config.TAG_COMMUNITY));
            superHero.setOccupation(json.getString(Config.TAG_OCCUPATION));
            superHero.setIncome(json.getString(Config.TAG_INCOME));
            superHero.setShortlist(json.getString(Config.TAG_SHORTLIST));
            superHero.setExpress_Intrest(json.getString(Config.TAG_EXPRESSINTREST));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        listSuperHeroes.add(superHero);
    }
adapter.notifyDataSetChanged();
}
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
    if (recyclerView.getAdapter().getItemCount() != 0) {
        int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
        if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
            return true;
    }
    return false;
}
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
    if (isLastItemDisplaying(recyclerView)) {
        getData();
      }
    }
  } 

以下是我的gradle课程

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "com.news.user.feedexample"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner"
    }
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
   }
}

   dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     androidTestCompile('com.android.support.test.espresso:espresso-
core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:+'
testCompile 'junit:junit:4.12'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:design:25.2.0'
 }

1 个答案:

答案 0 :(得分:1)

您将面临错误,因为“运行此应用的最高版本为23”。 请检查您的应用程序gradle文件。 在那次检查中你'minSdkVersion'。 确保它不是23。 它应该是您希望支持您的应用程序的最低版本。

更新回答: - RecyclerView.setOnScrollChangeListener只需要api 23,并且不会在其他版本中运行。所以请尝试使用addOnScrollListener

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
       @Override
       public void onScrollStateChanged(RecyclerView recyclerView, int 
      newState) {
               super.onScrollStateChanged(recyclerView, newState);

               //onScrollStateChanged will be fire every time you scroll
              if (isLastItemDisplaying(recyclerView)) {
                  getData();
                }

        }
    }
});