[英]Programmatically select item ListView in Android
我有兩個片段。 第一個內部帶有按鈕,另一個內部帶有ListView
(ListFragment)。
我希望第一個片段(由於其按鈕)允許用戶瀏覽第二個片段中的ListView。
因此,我希望ListView由帶有按鈕的第一個片段控制。
我在片段之間進行通信沒有問題(從第一個片段到第二個片段發送訂單),但是我不知道如何告訴我的ListView選擇(以編程方式)特定的列表項。
我應該使用哪種ListView,如何告訴ListView選擇/突出顯示/突出顯示其中一項?
當用戶按下第一個片段的按鈕時,我處於觸摸模式。
我應該使用setFocusableInTouchMode(true)
還是setChoiceMode(ListView.CHOICE_MODE_SINGLE)
或其他東西?
這是為每個試圖:
-以編程方式在ListView中選擇一個項目
- 使該項目突出顯示
我正在使用Android ICS,不知道它是否適用於所有級別的Api。
首先創建一個列表視圖(如果已經在listActivity / listFragment中,則創建一個列表視圖)
然后使用以下Mylistview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
將列表視圖的選擇模式設置為單選 : Mylistview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
然后使用以下Mylistview.setItemChecked(position, true);
以編程方式選擇您的商品 : Mylistview.setItemChecked(position, true);
(位置是整數,表示要選擇的項目的等級)
現在,您的商品已被實際選中,但您可能幾乎看不到任何東西,因為沒有視覺反饋。 現在,您有兩個選擇: 您可以使用預先構建的列表視圖,也可以使用自定義列表視圖。
1)如果您想要一個預先構建的列表視圖,請嘗試執行simple_list_item_activated_1
, simple_list_item_checked
, simple_list_item_single_choice
等。
您可以像這樣設置列表視圖,例如: setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_activated_1, data))
在選擇了哪個預構建的列表視圖之后,您現在將看到,選中該復選框時,您將選中一個復選框或更改了背景色,等等。
2)如果使用自定義列表視圖,則將定義將在每個項目中使用的自定義布局。 在此XML布局中,將為行中每個零件視圖分配一個選擇器,選擇時需要更改選擇器。
假設當您選擇該選項時,您希望您的行更改文本的顏色和背景的顏色。 您的XML布局可以這樣寫:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/menu_item_background_selector"
android:orientation="horizontal" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@drawable/menu_item_text_selector" />
現在,在可繪制文件夾中,創建menu_item_background_selector.xml和menu_item_text_selector.xml。
menu_item_text_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true"
android:color="#FFF">
</item>
<item android:state_pressed="true"
android:color="#FFF">
</item>
<item android:state_pressed="false"
android:color="#000">
</item>
</selector>
選中時文本將為白色。
然后為您的背景做類似的事情:(請記住,您不必強迫使用顏色,但也可以使用可繪制對象)
menu_item_background_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true"
android:color="#0094CE">
</item>
<item android:state_pressed="true"
android:color="#0094CE">
</item>
<item android:state_pressed="false"
android:color="#ACD52B">
</item>
</selector>
在這里,背景被選中時為藍色,未選中時為綠色。
我缺少的主要元素是android:state_activated
。 確實有太多狀態:激活,按下,集中,選中,選中...
我不確定android:state_activated
和android:state_pressed
是否是最好,最干凈的android:state_pressed
,但它似乎對我android:state_pressed
。
但是我不需要自己創建類來獲取Custom CheckableRelativeLayout (骯臟而又恐怖),也不需要使用CheckableTextViews。 我不知道為什么其他人使用這種方法,這可能取決於Api級別。
這對我有用:
1)設置列表的選擇行為。
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
2)設置指定位置的檢查狀態。
mListView.setItemChecked(1,true); //Don't make the same mistake I did by calling this function before setting the listview adapter.
3)在樣式資源(res / values)內添加一個新樣式,如下所示:
<style name="activated" parent="android:Theme.Holo">
<item name="android:background">@android:color/holo_green_light</item>
</style>
隨意使用您喜歡的任何顏色。
4)在ListView中使用先前定義的樣式 :
<ListView
android:id="@+id/listview"
style="@style/activated"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"/>
或在您用作行的布局中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/activated"
>
<!--widgets for your row here-->
</LinearLayout>
希望對任何人有幫助!
嘗試mListView.setSelection(position);
傑西米的答案對我有用,除了一小部分。 我想與他人分享。 調用list.setItemChecked( 0, true );
在FragmentActivity的onCreate()中無效。 在適配器list.getCheckedItemPosition( )
getView()
中返回-1。
我必須從protected void onPostCreate( Bundle savedInstanceState )
調用此方法。
您可以使用ListView#setSelection(int)
package com.example.samsung;
import com.example.samsung.*;
import com.example.samsung.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
public class Firstscreen extends Activity implements OnItemSelectedListener {
Button btn;
Spinner sp;
public String[] product = { "ML-1676P/XIP","SLM2021W/XIP","SL-M2826ND/XIP","SL-M2826ND/XIP","SL-M2826ND/XIP","SL-M3320ND/XIP","SL-M3820ND/XIP","SL-M4020ND/XIP"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstscreen);
btn = (Button) findViewById(R.id.bn);
sp= (Spinner) findViewById(R.id.sp);
}
public void button (View v){
{
Intent i = new Intent(Firstscreen.this,ML1676P.class);
startActivity(i);
}
Spinner s1 = (Spinner) findViewById(R.id.sp);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, product); // find other layout parameters
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private Object product() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.firstscreen, menu);
return true;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Firstscreen"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF35B5E5"
android:layout_centerInParent="true"
android:text="CHOOSE THE PRODUCT FROM THE LIST" />
<Spinner
android:id="@+id/sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawSelectorOnTop="true" />
<Button
android:id="@+id/bn"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:text=" GO "
android:onClick="button"/>
</LinearLayout>
在列表視圖中選擇一個項目,單擊按鈕時應轉到特定的選定項目頁面。代碼如上
我喜歡這樣:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
try {
int pos = 0;
listview.performItemClick(null, pos, listview.getItemIdAtPosition(pos) );
} catch (Exception e) {
e.printStackTrace();
}
}
}
只需將以下行添加到自定義列表視圖的布局即可:
android:background="?android:attr/activatedBackgroundIndicator"
有關完整的工作示例,請參見:
https://elimelec@bitbucket.org/elimelec/custombaseadapter.git
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.