【问题标题】:How to remain the listView index selected and highlighted?如何保持 listView 索引被选中并突出显示?
【发布时间】:2012-01-17 12:41:26
【问题描述】:

在我的应用程序中,我实现了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;                 
            }             
        }
    }

谢谢。

【问题讨论】:

标签: android listview android-layout android-listview expandablelistview


【解决方案1】:

您可以从用于填充 ListView 的数据持有者中保存选定的位置。然后,您可以使用 ListView 的setSelection(position) 属性来设置所选位置随时保持选中状态。

【讨论】:

  • 谢谢拉利特。但我想要的是所选列表索引应保持突出显示。
  • 您可能需要实现 OnScrollListener 并将您的项目设置在那里,但有一条注释指出“当屏幕上不可见时,Android 列表视图会回收列表中的“项目”。所以任何不可见的东西都是空的。以及有关 stackoverflow.com/questions/4156217/… 的信息
【解决方案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;
    }
}

现在就试试吧。

【讨论】:

  • 是的,这就是我想要的,但仍然存在一些问题。当我查看列表视图时,有床出现。当我选择另一个索引时,我可以选择另一个索引。现在有两个索引被选中。我不想这样。
  • 如果您将颜色设置为 onItem clic 那么为什么我的代码不可能?请参考并告诉我。
  • @iDroid:请评论您的答案。并查看更新的问题。我已经这样做了,但仍然无法获得正确的结果。有了你的回答,我得到了我现在用我的代码得到的结果。所以请帮助我。
  • @iDroidExplorer 你能复制粘贴我的程序并运行它。并检查它是否正常工作。并满足您的要求。然后编辑程序。数组列表只是你必须改变。
  • 是的,你是对的,但这样做我没有得到我想要的正确结果。那有什么问题呢?
【解决方案3】:

您可以在ListViewOnItemClickListener 中拥有一个在onItemClick 上更新的选定音素列表,然后它们会调用adapter.notifyDataSetChanged(),以便重新绘制列表。

在适配器的getView 方法中,如果音素在所选音素列表中,您可以更改项目背景。

问候。

【讨论】:

  • @iDroidExplorer 你是在模拟器上还是在手机里检查。我已经检查了上面的代码,没有发现错误...你能告诉我确切的问题是什么吗?
【解决方案4】:

经过几天的搜索和拉扯我的头发后,我发现activatedBackgroundIndicator 也可用于 ActionBarSherlock 造型系统。大多数需要在 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>

【讨论】:

    猜你喜欢
    • 2013-04-17
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多