سؤال

انا املك LinearLayout الذي يحتوي على بعض وجهات النظر الأخرى وبين هؤلاء أ ListView. يتم تحميل هذا العرض من واحد آخر بالنقر فوق زر.

يحدد هذا الزر بطريقة ما العنصر الذي يجب أن يكون العنصر في ListView هو الأول المرئي في القائمة. يتم استرداد العناصر التي تملأ القائمة عبر HTTP من خادم خارجي.

المشكلة هي أنه يمكنني الحصول على العنصر التاسع ليكون الأول في القائمة. يرجى ملاحظة ، لا أريد نقله ، تشكل الموضع الحالي إلى موقع جديد ، أريد أن يتم التمرير القائمة.

لقد حاولت مع setSelected() و scrollTo(x,y) و scrollBy(x,y) ولكن دون حظ.

لقد جربت أيضًا هذا اللقطات من الكود ، كما هو قبيح كما هو ، لكنني أردت فقط أن أجرب F أنه كان يعمل:

ListView categoryList = (ListView)findViewById(R.id.category_list);
        categoryList.post(new Runnable() {
            @Override
            public void run() {
                Log.d(this.getClass().getName(), "CategoryActivity.scrollToIndex: " + CategoryActivity.scrollToIndex);
                if(CategoryActivity.scrollToIndex>0){
                    ListView categoryList = (ListView)findViewById(R.id.category_list);
                    categoryList.setScrollContainer(true);
                    categoryList.scrollTo(4, CategoryActivity.scrollToIndex * 50);
                    categoryList.requestLayout();

                }
            }
        });

وهذا أعطاني بعض النجاح ، لكن القائمة كانت تتصرف في ذلك الوقت مجنون بطريقة ما لم أتمكن من وصفها ....

اي فكرة؟

هل كانت مفيدة؟

المحلول

حاول إضافتها إلى قائمة انتظار الرسائل

categoryList.post(new Runnable() {
    public void run() {
        categoryList.scrollTo(4, CategoryActivity.scrollToIndex * 50);
    }
});

لقد عملت بالنسبة لي في Scrollview (تحقق من هذه الإجابة).

نصائح أخرى

لقد قمت بعمل وظائف قد تكون مفيدة للآخرين في SlistView Scrolling ، فهي تعمل من أجلي في كل إصدار من Android ومحاكي وجهاز ، هنا ItemHeight هو الارتفاع الثابت للعرض في ListView.

int itemheight=60;
public void scrollToY(int position)
{
    int item=(int)Math.floor(position/itemheight);
    int scroll=(int) ((item*itemheight)-position);
    this.setSelectionFromTop(item, scroll);// Important
}
public void scrollByY(int position)
{
    position+=getListScrollY();
    int item=(int)Math.floor(position/itemheight);
    int scroll=(int) ((item*itemheight)-position);
    this.setSelectionFromTop(item, scroll);// Important
}
public int getListScrollY()
{
    try{
    //int tempscroll=this.getFirstVisiblePosition()*itemheight;// Important
    View v=this.getChildAt(0);
    int tempscroll=(this.getFirstVisiblePosition()*itemheight)-v.getTop();// Important
    return tempscroll;
    }catch(Exception e){}
    return 0;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top