列表视图:如何保持高亮/后preSS后退按钮选择? [英] Listview : How to remain highlighted/selected after press back button?
问题描述
如何保持列表视图,当我们pressed后退按钮仍然突出?可能吗?我已经通过这个<一去href="http://stackoverflow.com/questions/14952134/listview-selected-item-color-changed-when-come-back-from-other-activity-in-andro">link但没有回复。
How to keep listview remain highlighted when we pressed back button? Is it possible? I've went through this link but no replies.
我设法设置背景,图像和文字,以改变当用户preSS列表。
I managed to set background, image, and text in order to change when user press the list.
我的一个XML的:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@color/light_grey" />
<item android:state_selected="true"
android:drawable="@color/light_grey"/>
<item android:state_activated="true"
android:drawable="@color/light_grey"/>
<item android:state_focused="false"
android:drawable="@color/white" />
notification_row.xml
notification_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/activated_bg" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:padding="4dp"
android:src="@drawable/checkbox" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.73"
android:orientation="vertical" >
<TextView
android:id="@+id/id_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/title_row"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:padding="2dp"
android:text="Title"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/content_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/sendtime_row"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:maxLength="100"
android:paddingBottom="2dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="Date"
android:textColor="#808080"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
推荐答案
在活动你的ListView把这样的:
in which Activity you have listview put this:
创建这个全局变量:捆绑savedInstanceState;
@Override
protected void onRestart() {
// TODO Auto-generated method stub
onCreate(savedInstanceState);
super.onRestart();
}
在 onCreat
电子处理你的的ListView
或你设置你适配器
到的ListView
。
in onCreat
e handle your ListView
or from where you set you adapter
to 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后进入下一个活动。你可以在列表项的点击或任何你已经使用做到这一点。
or you can also do same style REVERSE. mean you have to deselect that litview after go to next activity. you can do this in list item click or whatever you have used..
这篇关于列表视图:如何保持高亮/后preSS后退按钮选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!