Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

android - Endless scrolling in not working in below marshmallow version

error ImageI implemented one fragment class where data is fetching in volley response and I also implements RecyclerView.OnScrollChangeListener. Data will come at scrolling time and it's working properly on marshmallow version but not working on below version i.e Kitkat, Lollipop. When I am trying to run on below version it gives an error which is max version to run this app is 23. Please help me to solve this type of error thank you in advance.

Below is my Fragment1 class.

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();
      }
    }
  } 

And below is my gradle class

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'
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you are facing error as 'max version to run this app is 23'. Please check your app gradle file. In that check you 'minSdkVersion'. Make sure it is not 23. it should be the minimum version which you wanted to support your app.

updated answer:- RecyclerView.setOnScrollChangeListener just need api 23 and that will not run in other versions.So please try using 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();
                }

        }
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...