上下文
我通过ui::ListView
制作了自定义排行榜。排行榜的每个元素都是ui::Button
。当用户点击某个位置时,他会在排行榜中收到有关此位置的详细统计信息。我正在使用Cocos2d-x ver。 3.8.1。为清楚起见,这是我的代码:
auto lvLeaderboard = ListView::create();
for (int j = 0; j < linesCount; j++)
{
Button* btnSingleUser = Button::create("btn.png”);
btnSingleUser->setTag(j + offsetResult);
btnSingleUser->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
switch (type)
{
case ui::Widget::TouchEventType::ENDED:
{
Button* currentButton = (Button*)sender;
this->pressedUserStatistic(currentButton->getTag());
break;
}
default:
break;
}
});
lvLeaderboard->pushBackCustomItem(btnSingleUser);
…
}
lvLeaderboard->setItemsMargin(0);
lvLeaderboard->setGravity(ListView::Gravity::CENTER_HORIZONTAL);
lvLeaderboard->setSize(Size(winSize.width, _height));
lvLeaderboard->setPosition(Point(0, 0));
listContainer->addChild(lvLeaderboard);
一切正常,我可以滚动排行榜,查看我点击的每个用户的统计数据
的问题
但是有一个问题。当用户到达列表末尾时,我需要加载下一部分数据(我一次下载50行结果)。但我无法找到变量,方法或处理程序,当用户到达ListView的第一个或最后一个元素时,它可以工作。
我试图
ui:ListView继承ScrollView,因此我尝试在两者中找到一些方法。我试过的一切,都不要让我想要的结果
的问题
如何确定用户何时到达ListView
中的第一个和最后一个元素?或者,如果这不可能,如何确定ScrollView
的结束和开始?
答案 0 :(得分:1)
您可以安排更新方法或运行“永久重复”操作以不断调用方法,以检查列表视图的底部项是否具有最后一个标记(在您的案例中为tag = 10 == linesCount-1)。您需要全局引用lvLeaderboard才能访问以下函数。
static Vec2 calculateItemPositionWithAnchor(Widget* item, const Vec2& itemAnchorPoint)
{
Vec2 origin(item->getLeftBoundary(), item->getBottomBoundary());
Size size = item->getContentSize();
return origin + Vec2(size.width * itemAnchorPoint.x, size.height * itemAnchorPoint.y);
}
static Widget* findClosestItem(const Vec2& targetPosition, const Vector<Widget*>& items, const Vec2& itemAnchorPoint, ssize_t firstIndex, float distanceFromFirst, ssize_t lastIndex, float distanceFromLast)
{
CCASSERT(firstIndex >= 0 && lastIndex < items.size() && firstIndex <= lastIndex, "");
if (firstIndex == lastIndex)
return items.at(firstIndex);
if (lastIndex - firstIndex == 1)
{
if (distanceFromFirst <= distanceFromLast)
return items.at(firstIndex);
else
return items.at(lastIndex);
}
// Binary search
ssize_t midIndex = (firstIndex + lastIndex) / 2;
Vec2 itemPosition = calculateItemPositionWithAnchor(items.at(midIndex), itemAnchorPoint);
float distanceFromMid = (targetPosition - itemPosition).length();
if (distanceFromFirst <= distanceFromLast)
return findClosestItem(targetPosition, items, itemAnchorPoint, firstIndex, distanceFromFirst, midIndex, distanceFromMid);
else
return findClosestItem(targetPosition, items, itemAnchorPoint, midIndex, distanceFromMid, lastIndex, distanceFromLast);
}
static Widget* getBottommostItemInCurrentView(ListView* lvLeaderboard)
{
const Vec2& positionRatioInView = Vec2::ANCHOR_MIDDLE_BOTTOM;
const Vec2& itemAnchorPoint = Vec2::ANCHOR_MIDDLE;
// Calculate the target position
Size contentSize = lvLeaderboard->getContentSize();
Vec2 targetPosition = -lvLeaderboard->getInnerContainerPosition();
targetPosition.x += contentSize.width * positionRatioInView.x;
targetPosition.y += contentSize.height * positionRatioInView.y;
// Find the closest item through binary search
ssize_t firstIndex = 0;
Vec2 firstPosition = calculateItemPositionWithAnchor(lvLeaderboard->getItems().at(firstIndex), itemAnchorPoint);
float distanceFromFirst = (targetPosition - firstPosition).length();
ssize_t lastIndex = lvLeaderboard->getItems().size() - 1;
Vec2 lastPosition = calculateItemPositionWithAnchor(lvLeaderboard->getItems().at(lastIndex), itemAnchorPoint);
float distanceFromLast = (targetPosition - lastPosition).length();
return findClosestItem(targetPosition, lvLeaderboard->getItems(), itemAnchorPoint, firstIndex, distanceFromFirst, lastIndex, distanceFromLast);
}
void HelloWorld::checkIfAtEnd() {
Button* bottom = (Button*)getBottommostItemInCurrentView(lvLeaderboard);
CCLOG("Bottom Button Having = %d", bottom->getTag());
if (bottom->getTag()-10 == linesCount-1) {
CCLOG("Bottom item reached..");
}
}
以下代码段调用在指定的秒后连续执行,此处为0.5。
DelayTime* dl = DelayTime::create(0.5);
CallFunc* cb = CallFunc::create(CC_CALLBACK_0(HelloWorld::checkIfAtEnd, this));
Sequence* seq = Sequence::create(dl, cb, nullptr);
this->runAction(RepeatForever::create(seq));
答案 1 :(得分:0)
You can add eventListener to your scrollView and detect then check like this
public class MainActivity extends AppCompatActivity {
static ArrayList<String> note;
ListView listView;
static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;
static Set<String> set;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
setTitle("Notely");
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notes.add("");
SharedPreferences sharedPreferences = getSharedPreferences("mypackage", Context.MODE_PRIVATE);
if (set == null) {
set = new HashSet<String>();
} else {
set.clear();
}
set.addAll(notes);
arrayAdapter.notifyDataSetChanged();
sharedPreferences.edit().remove("notes").apply();
sharedPreferences.edit().putStringSet("notes", set).apply();
Intent i = new Intent(getApplicationContext(), EditNote.class);
i.putExtra("noteId", notes.size() - 1);
startActivity(i);
}
});
ListView listView = (ListView) findViewById(R.id.listView);
SharedPreferences sharedPreferences = this.getSharedPreferences("mypackage", Context.MODE_PRIVATE);
set = sharedPreferences.getStringSet("notes", null);
notes.clear();
if (set != null) {
notes.addAll(set);
} else {
notes.add("Example note");
set = new HashSet<String>();
set.addAll(notes);
sharedPreferences.edit().putStringSet("notes", set).apply();
}
arrayAdapter = new ArrayAdapter(this, R.liststyle, notes);
listView.setAdapter(arrayAdapter);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), EditNote.class);
i.putExtra("noteId", position);
startActivity(i);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure?")
.setMessage("Do you want to delete this note?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notes.remove(position);
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("mypackage", Context.MODE_PRIVATE);
if (set == null) {
set = new HashSet<String>();
} else {
set.clear();
}
set.addAll(notes);
sharedPreferences.edit().remove("notes").apply();
sharedPreferences.edit().putStringSet("notes", set).apply();
arrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.show();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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
return super.onOptionsItemSelected(item);
}
}