简体   繁体   中英

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. 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

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.....

don't call finish(); on your first activity

listView.setSelection(position); send selected row to top of the ListView. listView.setSelectionFromTop(position, 20); this method also send selected row top of the ListView. But second argument (in this method 20) distance of the ListView`s top edge. (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(). When user clicks on the back button in second activity just call onBackPressed(); to resume first activity.

So do not use finish() method in first activity and use onBackPressed() in secound activity.

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(); 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();

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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