java - ListView : How to remain highlighted/selected after press back button?

标签 java android listview

当我们按下后退按钮时,如何让 ListView 保持高亮?可能吗?我经历过这个 link但没有回复。

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

我的一个 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

<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 的哪个 Activity 中放置这个:

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

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

onCreate 中处理您的 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。意味着您必须在进入下一个 Activity 后取消选择该 litview。您可以在列表项点击或您使用的任何内容中执行此操作..

关于java - ListView : How to remain highlighted/selected after press back button?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15893171/

相关文章:

java - 使用 "onBackPressed()"使自己开始的 Activity 返回到上一个屏幕

android - 带有键盘向上移动的编辑文本的 Bottom Sheet

java - 如何在应用程序订阅android中使用按比例分配模式

android - Gesturelistener onFling 行为不一致

java - Android - 带有自定义 CursorAdapter 的 Listview,运行异步任务会崩溃

java - 导入的 jar 文件创建空包

java - 删除 dependencies.dependency.version 错误

java - 向不可扩展类 java 添加新功能

android - 约束布局下的ListView在预览中消失

android - ViewPager 中的 fragment 有时会被垃圾收集吗? AsyncTask/ListView/ViewPager 问题