简体   繁体   English

Listview滚动位置

[英]Listview scroll position

i have Listview with images and text and i can to show image and text with list.setOnItemClickListener method, when i click listview item then open new activity and i can to show Details. 我有带有图像和文本的Listview,并且我可以使用list.setOnItemClickListener方法显示图像和文本,当我单击listview项目然后打开新活动时,我可以显示Details。 on second activity i have One button (Go back) and i want when i click this button listview would be position which i clicked first time for example if i clicked listview 5 items and then when i click Back button i want to this 5 item would be first position 在第二个活动中,我有一个按钮(返回),我想当我单击此按钮时,列表视图将是我第一次单击的位置,例如,如果我单击了列表视图5个项目,然后单击“返回”按钮时,我想要这5个项目成为第一名

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                journal = (TextView) view.findViewById(R.id.smalljournal);

                tittle = (TextView) view.findViewById(R.id.smalltitle);
                description = (TextView) view
                        .findViewById(R.id.smallDescription);
                smalllink = (TextView) view.findViewById(R.id.smalllink);
                DateTime = (TextView) view.findViewById(R.id.smallDateTime);
                smallstatID = (TextView) view
                        .findViewById(R.id.smallstatID);
                String Stringjournal = journal.getText().toString();
                String Stringtittle = tittle.getText().toString();
                String Stringdescription = description.getText().toString();
                String Stringlink = smalllink.getText().toString();
                String StringdateTime = DateTime.getText().toString();
                String StringstatID = smallstatID.getText().toString();
                HideKeyBoadr();
                Intent in = new Intent(MainActivity.this, Result.class);
                in.putExtra("KEY_journal", Stringjournal);
                in.putExtra("KEY_title", Stringtittle);
                in.putExtra("KEY_description", Stringdescription);
                in.putExtra("KEY_link", Stringlink);
                in.putExtra("KEY_pubDate", StringdateTime);
                in.putExtra("KEY_statID", StringstatID);

                String url = itemList.get(position).get(
                        MainActivity.KEY_image);
                if (url.endsWith("-c.jpg"))
                    url = url.replace("-c.jpg", ".jpg");

                in.putExtra("Bitmap", url);

                startActivity(in);
                overridePendingTransition(R.anim.trans_left_in,
                        R.anim.trans_left_out);
                finish();

            }
        });
    }

second activity 第二项活动

BackButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent in = new Intent(Result.this, MainActivity.class);
            startActivity(in);

            overridePendingTransition(R.anim.trans_right_in,
                    R.anim.trans_right_out);
            finish();

        }
    });

program working perfect but when i click back button listview is showing again..... 程序运行完美,但是当我单击“后退”按钮时,listview再次显示.....

don't call finish(); 不要调用finish(); on your first activity 在您的第一次活动中

listView.setSelection(position); listView.setSelection(位置); send selected row to top of the ListView. 将选定的行发送到ListView的顶部。 listView.setSelectionFromTop(position, 20); listView.setSelectionFromTop(position,20); this method also send selected row top of the ListView. 此方法还发送ListView的选定行顶部。 But second argument (in this method 20) distance of the ListView`s top edge. 但是第二个参数(在此方法20中)是ListView的上边缘的距离。 (include padding) If you neccessary to finish the first Activity you need to save the position of item and when back button clicked send saved position to your Activity. (包括填充)如果需要完成第一个活动,则需要保存项目的位置,然后单击“后退”按钮,将保存的位置发送到您的活动。 (Bundle, static class etc...) (捆绑包,静态类等...)

IDEA 理念

Simply you need to do is to create second activity without calling finish(). 您只需要做的是创建第二个活动,而无需调用finish()。 When user clicks on the back button in second activity just call onBackPressed(); 当用户在第二个活动中单击后退按钮时,只需调用onBackPressed();即可。 to resume first activity. 恢复第一次活动。

So do not use finish() method in first activity and use onBackPressed() in secound activity. 因此,请勿在第一个活动中使用finish()方法,而在第二活动中使用onBackPressed()。

What you should do: 您应该做什么:

In first activity: 在第一个活动中:

 @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
           //your logic gose here
           // start new activity without finishing current activity
}

In new activity: 在新活动中:

just call onBackPressed(); 只需调用onBackPressed(); in second activity.It will finish the second activity and automatically resume the first activity. 在第二个活动中。它将完成第二个活动并自动恢复第一个活动。

BackButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           //call onBackPressed(); to resume privious activity.
            onBackPressed();

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM