在我的应用中,我实现了ListView
。
现在我希望它设置为:如果我选择特定索引,它应该保持为选中状态。 ans在ListView
上显示为已选中。
如果我选择另一个索引,那么现在新索引应保持为选中状态。
被修改
表示我想设置为所选索引应该保持为选中突出显示,直到我选择另一个。但不喜欢选择多个。
那怎么办呢?
请帮助我。
我已经实现了ListView
,如下面的代码:
phonemesListView = (ListView) findViewById(R.id.phonemsListView);
private String[] Phonemes_List = new String[]{"P","B","T","D","K","G","N","M","ING","TH v","TH vl","F","V","S","Z","SH","CH","J","L","R rf","R b"};
phonemesListView.setAdapter(new ArrayAdapter<String>(this,R.layout.phonemes_list_row, R.id.phonemes,Phonemes_List));
@Override
public void onItemClick(AdapterView<?> parent, View view,final int Position,long id) {
phonemsText.setText(Phonemes_List[Position]);
Toast.makeText(getApplicationContext(), "Phonems: "+Phonemes_List[Position], Toast.LENGTH_SHORT).show();
// view.setBackgroundColor(Color.RED);
// phonemesListView.setBackgroundColor(Color.BLUE);
jumposition = Position;
int temp = 0;
if(jumpCount == -1){
view.setBackgroundColor(Color.BLUE);
jumpCount = jumposition;
JumpView = view;
temp = 1;
}
if(temp == 0) {
if(jumpCount == jumposition) {
view.setBackgroundColor(Color.BLUE);
JumpView = view;
}
else{
JumpView.setBackgroundColor(Color.TRANSPARENT);
view.setBackgroundColor(Color.BLUE);
jumpCount = jumposition;
JumpView = view;
}
}
}
感谢。
答案 0 :(得分:2)
您只需从用于填充ListView的数据持有者中保存所选位置即可。然后,您可以使用ListView的setSelection(position)
属性来设置所选位置保持选中状态。
答案 1 :(得分:1)
First Create the List view by using the Base adapter : As Follows Create two layout files and One java file :
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="@+id/listviewText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#FFFFFF"></ListView>
</LinearLayout>
textview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15dip"
android:focusable="false"
></TextView>
</LinearLayout>
Activity code is
package com.pac.marico;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewColor extends Activity {
/** Called when the activity is first created. */
ArrayList<String> arrayList;
Listviewlistneer listviewlistneer;
ListView listView;
int jumposition;
int jumpCount = -1;
View JumpView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)findViewById(R.id.listviewText);
arrayList = new ArrayList<String>();
arrayList.add("ABC");
arrayList.add("XYZ");
arrayList.add("PQR");
arrayList.add("ABC");
arrayList.add("XYZ");
arrayList.add("PQR");
arrayList.add("ABC");
arrayList.add("XYZ");
arrayList.add("PQR");
arrayList.add("ABC");
arrayList.add("XYZ");
arrayList.add("PQR");
arrayList.add("ABC");
arrayList.add("XYZ");
arrayList.add("PQR");
arrayList.add("ABC");
arrayList.add("XYZ");
arrayList.add("PQR");
arrayList.add("ABC");
arrayList.add("XYZ");
arrayList.add("PQR");
arrayList.add("ABC");
arrayList.add("XYZ");
arrayList.add("PQR");
ListviewAdapter listviewAdapter = new ListviewAdapter();
listView.setAdapter(listviewAdapter);
listviewlistneer = new Listviewlistneer();
}
class ListviewAdapter extends BaseAdapter
{
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View rowView = view;
Viewholder viewholder = null;
if(rowView == null)
{
LayoutInflater layoutInflater = LayoutInflater.from(ListViewColor.this);
rowView = layoutInflater.inflate(R.layout.textview, null);
viewholder = new Viewholder();
viewholder.textView = (TextView)rowView.findViewById(R.id.textview);
rowView.setTag(viewholder);
}
else
{
viewholder = (Viewholder)rowView.getTag();
}
viewholder.textView.setTag(position);
viewholder.textView.setText(arrayList.get(position));
if(jumpCount == position)
{
JumpView = rowView;
rowView.setBackgroundColor(Color.RED);
}
else
{
rowView.setBackgroundColor(Color.TRANSPARENT);
}
listView.setOnItemClickListener(listviewlistneer);
return rowView;
}
}
class Listviewlistneer implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
jumposition = position;
int temp = 0;
if(jumpCount == -1)
{
view.setBackgroundColor(Color.RED);
jumpCount = jumposition;
JumpView = view;
temp = 1;
}
if(temp == 0)
{
if(jumpCount == jumposition)
{
view.setBackgroundColor(Color.RED);
JumpView = view;
}
else
{
JumpView.setBackgroundColor(Color.TRANSPARENT);
view.setBackgroundColor(Color.RED);
jumpCount = jumposition;
JumpView = view;
}
}
}
}
class Viewholder
{
TextView textView;
}
}
现在就试试吧。
答案 2 :(得分:0)
您可以在onItemClick
的{{1}}中OnItemClickListener
更新选定的音素列表,然后拨打ListView
,以便再次重新制作列表。
在适配器的adapter.notifyDataSetChanged()
方法中,如果音素位于所选音素列表中,则可以更改项目背景。
问候。
答案 3 :(得分:0)
activatedBackgroundIndicator
。大多数需要在ICS中开发并支持向后兼容性的开发人员使用ActionBarSherlock,因此使用ActionBarSherlock对于大多数情况来说都是一个不错的选择。所以使用android:background="?activatedBackgroundIndicator"
代替在11之前的Android版本中给出错误,只需使用:android:background="?activatedBackgroundIndicator"
这是示例xmle代码:
<?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"
//note the activatedBackgroundIndicator
android:background="?activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingTop="2dip" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textSize="15sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="5dip"
android:textSize="20dip" />
</LinearLayout>