取消选择在ListView项目的onClick在android系统 [英] Unselect the ListView Item onClick in android

查看:101
本文介绍了取消选择在ListView项目的onClick在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是怎么回事,我有一个列表视图上,我穿上selection.As以及默认我把选定为

What is happening i have a listview on which i putting background color change on selection.As well as by default i am putting the first listview item selected as

public class OneWayFlightResult extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;


public OneWayFlightResult(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@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) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row, null);
    TextView flightTime = (TextView)vi.findViewById(R.id.flightTime); // title
    TextView flightCompanyName = (TextView)vi.findViewById(R.id.flightCompanyName); // title
    TextView flightNumber = (TextView)vi.findViewById(R.id.flightNumber); // title
    ImageView flightLogo = (ImageView)vi.findViewById(R.id.flightLogo);

    HashMap<String, String> flight = new HashMap<String, String>();
    flight = data.get(position);

    flightTime.setText(flight.get(TestActivity.FlightTime));
    flightCompanyName.setText(TestActivity.FlightCompanyName);
    flightNumber.setText(TestActivity.FlightNumber);

    if(position == 0){

        vi.setBackgroundResource(R.drawable.selection_effect);
        vi.setSelected(true);

    }
    return vi;
}

这是我使用的XML文件本 selection_effect.xml

This is XML file i am using in this selection_effect.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true">
    <shape>
        <solid android:color="#ffffff" />
    </shape>
</item>
    <item>
    <shape>
        <solid android:color="#00a7eb" />
    </shape>
</item>
</selector>

因此​​,在默认情况下此第一个列表视图selected.Now当用户选择另一个ListView项第一个选择保持,另一个也得到了相同的effect.So怎么可能改变对ListView项的点击效果默认情况下,第一项动态.Means大作选择当用户选择其他项目另一种获取所选从效果默认一送去掉

So by default this the first list view is selected.Now when the user select the another listview item the first one remains selected and the other one also got the same effect.So how could change the effect on click of the listview item dynamically .Means by default first item comes up selected when the user selects other item other one get selected the effect from the default one get removed

推荐答案

我得到了你的问题的解决方案。做如下。

I got solution for your problem. Do as following.

1)打开其中的ListView 您已经创建主布局文件。结果
  添加的android:choiceMode =singleChoice。这看起来像下面。

1) open your main layout file where ListView you have created.
Add android:choiceMode="singleChoice". This will look like below.

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:choiceMode="singleChoice" >
</ListView>

2)打开您的 list_item.xml 布局文件。其中,你的根视图中添加的android:背景=机器人:ATTR / activatedBackgroundIndicator。在我的样本项目,它的外观像下面。

2) Open your list_item.xml layout file. In which, to your root view, add android:background="?android:attr/activatedBackgroundIndicator". In my sample project, its look like below.

<?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="match_parent"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:orientation="vertical" >

    //your views

</LinearLayout>

3)打开你的活动文件。设置适配器将的ListView后添加 list.setSelector(R.drawable.selection_effect); 。这看起来像下面。

3) Open your activity file. After setting adapter to your ListView, add list.setSelector(R.drawable.selection_effect);. This will look like below.

ListView ls = (ListView) findViewById(R.id.listView1);
ListAdapter adapter = new ListAdapter(this, data);
ls.setAdapter(adapter);
ls.setSelector(R.drawable.selection_effect);

在这里, selection_effect 是您在绘制目录下创建文件的绘制。
我测试了我的code。这是工作的罚款。

Here, selection_effect is drawable file which you have created in drawable directory. I tested my code. Which is working fine.

4)要默认选择第一种观点,免除您的code在 BaseAdapter ,并把下面的code完成第3步后。

4) To select first view by default, remove your code in BaseAdapter and put following code after completing 3rd step.

ls.setItemChecked(0,true);

您需要把上面code像下面了。

You need to put it after above code like below.

ListAdapter adapter = new ListAdapter(data);
ls.setAdapter(adapter);
ls.setSelector(R.drawable.selection_effect);
ls.setItemChecked(0, true);

说明

ls.setSelector(R.drawable.selection_effect);

ls.setSelector(R.drawable.selection_effect);

这将根据选择器中选择行项目您已经绘制目录中定义。

This will select row item based on selector you have defined in drawable directory.

ls.setItemChecked(0,TRUE);

ls.setItemChecked(0, true);

这将在第一次运行默认选择第一项。后,您可以选择通过点击其他项目。

This will select first item by default at first time run. After you can select other items by clicking on them.

这篇关于取消选择在ListView项目的onClick在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆