问题
In My app i have implement the ListView
.
Now i want it to set as like that: If i select on perticular index it should be remain as selected. ans appear as selected on ListView
.
If i select another index then now that new index should be remain as selected.
Edited
Means I want to set as the selected index should remain as highlighted as selected till I select another. but not like multiple selected.
So how to do it ?
Please help me for that.
I have implemented the ListView
as like below code:
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;
}
}
}
Thanks.
回答1:
You can just save the selected position from the data holder that you are using to populate your ListView. Then, you can use setSelection(position) attribute of ListView to set the selected position remain selected whenever you want.
回答2:
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;
}
}
Just Try it now.
回答3:
You can have a list of selected phonemes that is updated on onItemClick
in the OnItemClickListener
of the ListView
and them call to adapter.notifyDataSetChanged()
so the list is repainted again.
In getView
method of the adapter you can change the item background if the phoneme is in the list of selected phonemes.
Regards.
回答4:
after days of search and pulling my hair i just found out that activatedBackgroundIndicator
is also available in ActionBarSherlock styling system. Most of the devs which need to develop in ICS and support backward compatibility, use ActionBarSherlock,so using ActionBarSherlock is a good option for most cases.So instead of using android:background="?activatedBackgroundIndicator"
which will give errors in android versions prior to 11, just use: android:background="?activatedBackgroundIndicator"
here is the example xmle code:
<?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>
来源:https://stackoverflow.com/questions/8894780/how-to-remain-the-listview-index-selected-and-highlighted