列表视图:如何在按下后退按钮后保持突出显示/选择?

当我们按下按钮时,如何保持列表视图保持突出显示? 可能吗? 我已经通过这个链接但没有回复。

我设法设置背景,图像和文本,以便在用户按下列表时进行更改。

我的一个xml:

     

notification_row.xml

           

你有listview的Activity把这个:

将其创建为全局变量: Bundle savedInstanceState;

 @Override protected void onRestart() { // TODO Auto-generated method stub onCreate(savedInstanceState); super.onRestart(); } 

onCreat e中处理ListView或从您将adapter设置为ListView

这是简单的示例如何做到这一点:

 package com.example.testapp; import com.example.main.util.testActivity; import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MainActivity extends ListActivity { private static final String[] COUNTRIES = new String[] { "Belgium", "France", "France_", "Italy", "Germany", "Spain" }; private MyArrayAdapter adapter; Bundle savedInstanceState; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = new MyArrayAdapter(MainActivity.this); getListView().setAdapter(adapter); } private class MyArrayAdapter extends BaseAdapter { private LayoutInflater mInflater; public MyArrayAdapter(Context con) { // TODO Auto-generated constructor stub mInflater = LayoutInflater.from(con); } @Override public int getCount() { // TODO Auto-generated method stub return COUNTRIES.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final ListContent holder; View v = convertView; if (v == null) { v = mInflater.inflate(R.layout.my_spinner_style, null); holder = new ListContent(); holder.name = (TextView) v.findViewById(R.id.textView1); v.setTag(holder); } else { holder = (ListContent) v.getTag(); } holder.name.setText("" + COUNTRIES[position]); holder.name.setOnClickListener(mOnTitleClickListener3); return v; } } static class ListContent { TextView name; } public OnClickListener mOnTitleClickListener3 = new OnClickListener() { public void onClick(View v) { final int position = getListView().getPositionForView( (View) v.getParent()); v.setBackgroundColor(Color.RED); startActivity(new Intent(MainActivity.this, testActivity.class)); // Log.d("you are click on Ratings", "you are click on Ratings"); // v.setBackgroundColor(Color.WHITE); } }; protected void onRestart() { // adapter.notifyDataSetChanged(); onCreate(savedInstanceState); }; } 

或者你也可以做同样的风格REVERSE。 意味着你必须在进入下一个活动后取消选择那个litview。 您可以在列表项单击或您使用的任何内容中执行此操作。

您可以每次在OnItemSelectedListener中记录位置,在用户导航回来之后,调用mListView.setSelection(pos)

您可以使用这些方法在listView上进行选择

  mListView.setSelector(Color.BLUE);//or any other color or drawable mListView.setSelection(position); //for selected position 

在这里,您可以将位置用作列表视图的选定位置。您必须在选择后存储此位置。因此,当您按下后退按钮并再次返回到该活动时,所选位置将保持选中状态。