2013-04-09 30 views
5

當我們按下按鈕時如何保持listview保持高亮顯示?可能嗎?我已經通過這link但沒有答覆。Listview:如何在按下後退按鈕後保持突出顯示/選中狀態?

我設法設置背景,圖像和文字,以便在用戶按下列表時進行更改。

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

回答

1

在活動你有列表視圖把這個:

創建這個全局變量:Bundle savedInstanceState;

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

onCreatË處理您ListView或者你設置你adapterListView

下面是簡單的例子,如何做到這一點:

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

,或者您也可以做同樣的風格相反。意味着你必須在進入下一個活動後取消選擇litview。你可以在列表項點擊或任何你已經使用過的。

1

您可以只需登錄該位置OnItemSelectedListener每一個時間和用戶瀏覽後回,請致電mListView.setSelection(pos)

0

您可以使用一個ListView這些methos來選擇

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

在這裏您可以直接使用列表視圖的選定位置的位置。你有當你按返回鍵,然後再次回到這個位置存儲選擇。所以之後到那個活動,選定的位置將保持被選中。

相關問題