Mainactivity.java:
p + theme(plot.margin=unit(c(0.1,0.5,0.1,0.1),"cm"))
TaskRow.java:
public class MainActivity extends AppCompatActivity {
private List<Item> colors;
private boolean hasMore;
private AsyncTask asyncTask;
private RecyclerView recyclerView;
private List<TaskRow> mUsers = new ArrayList<>();
String userid="3",Response;
static int pageno=1 ,recordsperpage=5;
private ArrayList<HashMap<String, String>> postList=new ArrayList<HashMap<String, String>>();
static boolean errored = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
colors = buildColors();
hasMore = true;
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setAdapter(new MaterialPaletteAdapter(colors, new RecyclerViewOnItemClickListener() {
@Override
public void onClick(View v, int position) {
if (colors.get(position) instanceof TaskRow) {
Toast toast = Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT);
toast.show();
}
}
}));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this));
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (hasMore && !(hasFooter())) {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
//position starts at 0
if (layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 2) {
/* asyncTask = new BackgroundTask();
Void[] params = null;
asyncTask.execute(params);*/
new GetPostData().execute();
}
}
}
});
}
@Override
protected void onPause() {
super.onPause();
if (asyncTask != null) {
asyncTask.cancel(false);
}
}
private class GetPostData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
colors.add(new Footer());
recyclerView.getAdapter().notifyItemInserted(colors.size() - 1);
/*pDialog = new ProgressDialog(getActivity());
pDialog.setIndeterminate(true);
pDialog.setMessage("Please Wait...");*/
//pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
return null;
}
protected void onPostExecute(String result) {
//pDialog.dismiss();
//rprogress.setVisibility(View.GONE);
if(pageno <5)
{
int size = colors.size();
colors.remove(size - 1);//removes footer
colors.addAll(buildColors());
recyclerView.getAdapter().notifyItemRangeChanged(size - 1, colors.size() - size);
Toast.makeText(getApplicationContext(), Integer.toString(pageno), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "No More Post", Toast.LENGTH_LONG).show();
hasMore = false;
}
errored = false;
}
}
private boolean hasFooter() {
return colors.get(colors.size() - 1) instanceof Footer;
}
private ArrayList<Item> buildColors() {
ArrayList<Item> colors = new ArrayList<Item>(13);
Response = SocialWebService.invokeGetPostWS(userid, pageno, recordsperpage, "selectPostData", MainActivity.this);
try {
//JSONObject jsonRootObject = new JSONObject(MainCategory);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = new JSONArray(Response);
//Iterate the jsonArray and print the info of JSONObjects
String CityName, PostID,PostTypeID,PostedByID,PostDate,PostImageID,PostContent,Lang,Lat;
String LocationName,FirstName,LastName;
String Picture,ImageFull,UserID,UserName;
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
CityName=jsonObject.getString("CityName");
PostID=jsonObject.getString("PostID");
PostTypeID=jsonObject.getString("PostTypeID");
PostedByID=jsonObject.getString("PostedByID");
PostDate=jsonObject.getString("PostDate");
PostImageID=jsonObject.getString("PostImageID");
PostContent=jsonObject.getString("PostContent");
Lang=jsonObject.getString("Lang");
Lat=jsonObject.getString("Lat");
LocationName=jsonObject.getString("LocationName");
FirstName=jsonObject.getString("FirstName");
LastName=jsonObject.getString("LastName");
Picture=jsonObject.getString("Picture");
ImageFull=jsonObject.getString("ImageFull");
UserID=jsonObject.getString("UserID");
UserName=jsonObject.getString("UserName");
HashMap<String, String> data=new HashMap<String,String>();
data.put("CityName",CityName);
data.put("PostID",PostID);
data.put("PostTypeID",PostTypeID);
data.put("PostedByID",PostedByID);
data.put("PostDate",PostDate);
data.put("PostImageID",PostImageID);
data.put("PostContent",PostContent);
data.put("Lang",Lang);
data.put("Lat",Lat);
data.put("LocationName",LocationName);
data.put("FirstName",FirstName);
data.put("LastName",LastName);
data.put("Picture",Picture);
data.put("ImageFull",ImageFull);
data.put("UserID",UserID);
data.put("UserName",UserName);
postList.add(data);
}
for(int i=0;i<postList.size();i++)
{
String cityname = postList.get(i).get("CityName");
String postid=postList.get(i).get("PostID");
String posttypeid = postList.get(i).get("PostTypeID");
String postedbyid=postList.get(i).get("PostedByID");
String postdate=postList.get(i).get("PostDate");
String postimageid=postList.get(i).get("PostImageID");
String postcontent=postList.get(i).get("PostContent");
String lang=postList.get(i).get("Lang");
String lat=postList.get(i).get("Lat");
String locationname=postList.get(i).get("LocationName");
String firstname=postList.get(i).get("FirstName");
String lastname=postList.get(i).get("LastName");
String picture=postList.get(i).get("Picture");
String imagefull=postList.get(i).get("ImageFull");
String userid=postList.get(i).get("UserID");
String username=postList.get(i).get("UserName");
colors.add(new TaskRow(cityname, postid,posttypeid,postedbyid,postdate,postimageid,postcontent,lang,lat,locationname,
firstname,lastname,picture,imagefull,userid,username ));
}
pageno++;
} catch (JSONException e) {e.printStackTrace();}
return colors;
}
}
我的适配器类:
public class TaskRow extends Item{
String cityname,postid, posttypeid, postedbyid, postdate, postimageid,postcontent,lang,lat,locationname,firstname,lastname,
picture, imagefull, userid, username;
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public String getPosttypeid() {
return posttypeid;
}
public void setPosttypeid(String posttypeid) {
this.posttypeid = posttypeid;
}
public String getPostedbyid() {
return postedbyid;
}
public void setPostedbyid(String postedbyid) {
this.postedbyid = postedbyid;
}
public String getPostdate() {
return postdate;
}
public void setPostdate(String postdate) {
this.postdate = postdate;
}
public String getPostimageid() {
return postimageid;
}
public void setPostimageid(String postimageid) {
this.postimageid = postimageid;
}
public String getPostcontent() {
return postcontent;
}
public void setPostcontent(String postcontent) {
this.postcontent = postcontent;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLocationname() {
return locationname;
}
public void setLocationname(String locationname) {
this.locationname = locationname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getImagefull() {
return imagefull;
}
public void setImagefull(String imagefull) {
this.imagefull = imagefull;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public TaskRow(String cityname, String postid, String posttypeid, String postedbyid, String postdate, String postimageid,
String postcontent, String lang, String lat, String locationname, String firstname, String lastname,
String picture, String imagefull, String userid, String username) {
this.cityname=cityname;
this.postid=postid;
this.posttypeid=posttypeid;
this.postedbyid=postedbyid;
this.postdate=postdate;
this.postimageid=postimageid;
this.postcontent=postcontent;
this.lang=lang;
this.lat=lat;
this.locationname=locationname;
this.firstname=firstname;
this.lastname=lastname;
this.picture=picture;
this.imagefull=imagefull;
this.userid=userid;
this.username=username;
}
}
无尽的回收者视图重复json数据,我增加了页码,但首先它会给我5条记录,然后在增加页码之前重复相同的记录,增加页码后,它也会重复从第一页码到在增加页码之前的最后一个。我已经尝试了https://github.com/danielme-com/Android-Endless-RecyclerView这个解决方案。
答案 0 :(得分:1)
只需使用以下解决方案修改您的代码即可。您应该在添加新项目之前清除阵列列表。希望这会有所帮助
protected void onPostExecute(String result) {
//pDialog.dismiss();
//rprogress.setVisibility(View.GONE);
if(pageno <5)
{
int size = colors.size();
colors.remove(size - 1);//removes footer
colors.clear(); // just clear your array before adding new items to it
colors.addAll(buildColors());
recyclerView.getAdapter().notifyItemRangeChanged(size - 1, colors.size() - size);
Toast.makeText(getApplicationContext(), Integer.toString(pageno), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "No More Post", Toast.LENGTH_LONG).show();
hasMore = false;
}
errored = false;
}