gpt4 book ai didi

android - recyclerview 将数据添加到顶部而不会弄乱滚动?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:37 24 4
gpt4 key购买 nike

我正在尝试在回收站 View 的滚动顶部添加一些日期。这是工作,除了滚动,当添加一些新数据时它会把所有东西都弄乱。

我看到了这个主题:

Link 1

Link 2

但他们都没有解决我的问题。

也许有人可以帮助我...

我使用 volley 获取一些数据,如下所示:

try {
//Getting json
json = array.getJSONObject(i);

//Adding data to the object
PostObj.setImageUrl(json.getString(Config.TAG_PPIC));
...


} catch (JSONException e) {
e.printStackTrace();
}
//Adding object to the list
listLf.add(0, PostObj); //0, postobj
}

//Notifying the adapter that data has been added or changed
//adapter.notifyDataSetChanged(); the scroll will jump to position 0
adapter.notifyItemRangeChanged(0, adapter.getItemCount()); // still not working
}

我尝试添加 adapter.notifyItemRangeChanged(0, adapter.getItemCount()); 但还是一样。

我也试过:

// Save state
private Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();

// Restore state
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);

而且它也不起作用。也许我把它放在了错误的地方或什么的......

我需要一些建议,我做错了什么以及如何获取新数据、通知适配器而不弄乱滚动位置?


编辑--------------------------------全类:

public class Comments extends BaseActivity {


private List<LfGetSet> listLf;

//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;

//Volley Request Queue
private RequestQueue requestQueue;
private String requestCount = "0";
private String lastrequestCount = "0";

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

ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_comments, content, true);

//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerViewLf);
recyclerView.setHasFixedSize(true);
//layoutManager = new LinearLayoutManager(this);

final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);

recyclerView.setLayoutManager(layoutManager);

//Initializing our list
listLf = new ArrayList<>();
requestQueue = Volley.newRequestQueue(this);

//Calling method to get data to fetch data
getData();

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isLastItemDisplaying(recyclerView)) {
getData();
}
}
});

//initializing our adapter
adapter = new LfAdapter(listLf, this);

//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);


}

private JsonArrayRequest getDataFromServer(String requestCount) {
...
return jsonArrayRequest;
}

//This method will get data from the web api
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
lastrequestCount = requestCount;
//Incrementing the request counter
requestCount++;
}

//This method will parse json data
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
//Creating the object
LfGetSet PostObj = new LfGetSet();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);

//Adding data to the object
PostObj.setImageUrl(json.getString(Config.TAG_PPIC));
...


} catch (JSONException e) {
e.printStackTrace();
}
//Adding object to the list
listLf.add(0, PostObj); //0, postobj
}

//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}

//This method would check that the recyclerview scroll has reached the bottom or not
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION
&& lastVisibleItemPosition == 1
&& lastrequestCount != requestCount
&& requestCount != "0")
return true;
}
return false;
}

}

最佳答案

您可以尝试这种方法,按照正常方式向列表中添加一个新项目,然后更新数组中的数据并将新项目放在顶部。

   // Add a new item the usual way so it goes at the bottom

int x = 0;

// Create a new Data list. New item goes 1st

// Create a Loop

myList.remove(x); // Remove item from the array at position

MyList myList = new MyList();

myList.add(x, theData); // Add data to array at position


// Now that new data has been inserted to the Array at position X update the List item at that same Position. I usually do this in the Background.

Handler handler = new Handler();

final int finalX = x;
final Runnable r = new Runnable() {
public void run() {
adapter.notifyItemChanged(finalX); // Notify Adapter that the Items data has changed and Update
}
};

handler.post(r);

x++;

从数据库中读取更新数据然后使用新数据动态更新列表项的代码。但在您的情况下,您需要添加 1 个新项目并使用数组中位置 0 和列表中的新项目数据更新列表。旧项目的位置移动 1。

public void updateStocks() {

System.out.println("Updating");
int rec = db.getRecordsCount() - 1;

List<Records> record = db.getAllRecords();

int x = 0;

for (Records cn : record) {

stocksList.remove(x);

Livestocks stocks = new Livestocks();

stocks.setID(cn.getID());
stocks.setName(cn.getName());
stocks.setTicker(cn.getTicker());
stocks.setPrice(cn.getPrice());
stocks.setOpen(cn.getOpen());
stocks.setChange(cn.getChange());
stocks.setPchange(cn.getPchange());
stocks.setDhigh(cn.getDhigh());
stocks.setDlow(cn.getDlow());
stocks.setYhigh(cn.getYhigh());
stocks.setYlow(cn.getYlow());
stocks.setVol(cn.getVol());
stocks.setShares(cn.getShares());
stocks.setIown(cn.getIown());
stocks.setMcap(cn.getMcap());
stocks.setStatus(cn.getStatus());
stocks.setExchange(cn.getExchange());

stocksList.add(x, stocks);

Handler handler = new Handler();

final int finalX = x;
final Runnable r = new Runnable() {
public void run() {
adapter.notifyItemChanged(finalX);
}
};

handler.post(r);

x++;

if (rec == x) {
//do nothing
}
}

}

关于android - recyclerview 将数据添加到顶部而不会弄乱滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42916098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com