如何判断出哪些已被点击一个机器人列表视图元素中已被、视图、机器人、如何判断

2023-09-07 02:24:56 作者:18.迷糊少女

我使用的ListView建立一个Android应用程序。我有列表视图项,如下所示。

^列表项指标0

^列表项指标1

的XLM如下。

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:layout_width =FILL_PARENT    机器人:layout_height =WRAP_CONTENT    机器人:方向=横向    机器人:填充=4dip>    < ImageView的        机器人:ID =@ + ID /图标        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =FILL_PARENT        机器人:SRC =@绘制/图标/>    <的LinearLayout        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =FILL_PARENT        机器人:layout_weight =0.93        机器人:方向=垂直        机器人:填充=10dp        机器人:paddingLeft =10dp        机器人:paddingRight =10dp>        <! -  JFN底以前是2,前6  - >        <! - 名称标签 - >        <的TextView            机器人:ID =@ + ID /名称            机器人:layout_width =FILL_PARENT            机器人:layout_height =WRAP_CONTENT            机器人:paddingBottom会=0dip            机器人:paddingTop =0dip            机器人:文字颜色=#43bd00            机器人:TEXTSIZE =16SP            机器人:文字样式=大胆/>        <! - 电子邮件标签 - >        <的TextView            机器人:ID =@ +帐号/电邮            机器人:layout_width =FILL_PARENT            机器人:layout_height =WRAP_CONTENT            机器人:paddingBottom会=0dip            机器人:文字颜色=#acacac/>        <! - 手机号标签 - >        <的TextView            机器人:ID =@ + ID /移动            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:重力=左            机器人:文字=移动            机器人:文字颜色=#5d5d5d            机器人:文字样式=大胆/>    < / LinearLayout中>    <的LinearLayout        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =WRAP_CONTENT        机器人:重力=右        机器人:方向=垂直>        < ImageView的            机器人:ID =@ + ID /聊天            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =FILL_PARENT            机器人:layout_marginRight =2DIP            机器人:重力=右            机器人:SRC =@绘制/聊天/>        < ImageView的            机器人:ID =@ + ID /日历            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =FILL_PARENT            机器人:layout_marginRight =2DIP            机器人:重力=右            机器人:SRC =@绘制/日历/>    < / LinearLayout中>< / LinearLayout中> 

我试图来检测四种不同的单击事件:

当狗的图片被点击当中间部分被点击当聊天按钮被点击当日历按钮被点击

在此外,每个这些点击的必须根据其在ListView项目已经被点击是区分。

我知道我可以区分每个列表项:我可以使用ListView.setOnItemClickListener为如下图所示,它提供了列表中的项目的ID。但是,这并没有告诉我这四个部分被点击。

 的ListView LV = getListView();//列表视图对项目点击监听器lv.setOnItemClickListener(新OnItemClickListener(){    @覆盖    公共无效onItemClick(适配器视图<>母公司,观景,            INT位置,长的id){        //从选定的ListItem得到的值        Log.d(JFN,列表项点击ID为:+身份证);    }}); 
图书管理机器人图

我也可以结合不同的onClick函数调用中的每个XML四个部分,但这并不让我知道该列表中的项目被点击了。

 安卓的onClick =imgClicked 

我不认为我可以结合这两种解决方案,因为它会引入竞争状态。有没有办法做到既检测被点击该列表项之有道,并点击该特定列表项的一部分?

另外信息

以下code显示了我创建使用适配器的ListView。

  //检索字符串转换成JSON,并提取联系人串jsonData = jsonToStringFromAssetFolder(maemployees.json,这一点);JSONObject的jsonObj =新的JSONObject(jsonData).getJSONObject(TAG_RESULTS);INT numContacts = jsonObj.getInt(TAG_NUM);Log.d(JFN表示,触点数量:+ numContacts);接触= jsonObj.getJSONArray(TAG_CONTACTS);Log.d(JFN,联系人present数:+ contacts.length());//循环触点的for(int i = 0; I< contacts.length();我++){    //提取这一个    JSONObject的C = contacts.getJSONObject(I)    //提取字符串    第一串=(c.has(TAG_FIRST))? c.getString(TAG_FIRST):;    字符串最后=(c.has(TAG_LAST))? c.getString(TAG_LAST):;    字符串城市=(c.has(TAG_CITY))? c.getString(TAG_CITY):;    字符串状态=(c.has(TAG_STATE))? c.getString(TAG_STATE):;    字符串国家=(c.has(TAG_COUNTRY))? c.getString(TAG_COUNTRY):;    字符串costName =(c.has(TAG_COST_NAME))? c.getString(TAG_COST_NAME):;    //临时哈希映射为单个联系人    HashMap的<字符串,字符串>接触=新的HashMap<字符串,字符串>();    contact.put(名,第一++即止);    contact.put(电子邮件,市+,+状态+,+国家);    contact.put(手机,costName);    //添加单个联系人名单    contactList.add(接触);}Log.d(JFN,完成循环联系人);//更新解析的JSON数据到ListView控件ListAdapter适配器=新SimpleAdapter(        MainActivity.this,contactList,        R.layout.list_item,新的String [] {姓名,电子邮件,                手机},新的INT [] {R.id.name,                R.id.email,R.id.mobile});setListAdapter(适配器); 

解决方案

奥莱特我有一个类似的问题,但我用的 ViewPager ListFragment 并自定义的ListView 布局。搜索结果在上述情况下,你只使用 ImageViews ,但只是如果你使用的控件类似情况下,的ImageButton 复选框按钮等等,那么你将面临的问题探讨here和here.这仅仅是因为这样的控件可以窃取焦点从的ListView 和完整的列表不能被选定的项目/点击。所以我要说,只是用的 ImageViews 是一个聪明的举动。结果我假设你正在使用的适配器设置列表的内容。该适配器在里面你可以指定 onClickListener() S表示这样每个项目:结果

 公共类MyListAdapter扩展ArrayAdapter<串GT; {    .......    @覆盖    公共查看getView(INT位置,查看convertView,父母的ViewGroup){         查看rowView = inflator.inflate(R.layout.channel_list_item,空,真);         ImageView的聊天=(ImageView的)rowView.findViewById(R.id.chat);         chat.setOnClickListener(新OnClickListener(){            @覆盖            公共无效的onClick(视图v){                 //做一点事            }         }         ImageView的日历=(ImageView的)rowView.findViewById(R.id.calendar);         calendar.setOnClickListener(新OnClickListener(){            @覆盖            公共无效的onClick(视图v){                 //做一点事            }         }         .......    }} 

但请记住,在使用的控件像的ImageButton 复选框按钮您需要分配财产的 机器人:可聚焦=FALSE 在XML。并在 的ImageButton 您需要这样做的 getView()方法中:

 最后的ImageButton imgBtn =(的ImageButton)rowView.findViewById(R.id.imgBtn);    imgBtn.setFocusable(假);    imgBtn.setOnClickListener(新OnClickListener(){        @覆盖        公共无效的onClick(视图v){            //做你在这里的任务        }    }); 

或者你也可以使用纳撒尼尔·瓦格纳的方法。

希望我回答你的问题。

I'm building an Android application using ListView. I have list view items as show below.

^ List Item Index 0

^ List Item Index 1

The xlm is as follows.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="4dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/icon" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.93"
        android:orientation="vertical"
        android:padding="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

        <!-- JFN bottom used to be 2, top 6 -->
        <!-- Name Label -->

        <TextView
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="0dip"
            android:paddingTop="0dip"
            android:textColor="#43bd00"
            android:textSize="16sp"
            android:textStyle="bold" />
        <!-- Email label -->

        <TextView
            android:id="@+id/email"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="0dip"
            android:textColor="#acacac" />

        <!-- Mobile number label -->

        <TextView
            android:id="@+id/mobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="Mobile: "
            android:textColor="#5d5d5d"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/chat"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="2dip"
            android:gravity="right"
            android:src="@drawable/chat" />

        <ImageView
            android:id="@+id/calendar"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="2dip"
            android:gravity="right"
            android:src="@drawable/calendar" />
    </LinearLayout>

</LinearLayout>

I'm trying to detect four different click events:

When the picture of the dog is clicked When the middle section is clicked When the chat button is clicked When the calendar button is clicked

In addition, each of these clicks must be distinguishable based on which item in the ListView has been clicked.

I know how I can tell each ListItem apart: I can use the ListView.setOnItemClickListener as is shown below, which provides an id of the item in the list. But this does not tell me which of the four portions was clicked on.

ListView lv = getListView();

// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // getting values from selected ListItem
        Log.d("JFN","List item clicked with id: "+id);
    }
});

I can also bind a different onClick function call to each of the four sections in the xml, but this doesn't let me know which of the list items was clicked.

android:onClick="imgClicked"

I don't think I can combine these two solutions, as it would introduce a race condition. Is there a proper way to accomplish detecting both which list item was clicked, and which part of that particular list item was clicked?

Further Info

The following code shows my creation of the ListView using the adapter.

// Retrieve string, convert to JSON, and extract contacts
String jsonData = jsonToStringFromAssetFolder("maemployees.json",this);
JSONObject jsonObj = new JSONObject(jsonData).getJSONObject(TAG_RESULTS);
int numContacts = jsonObj.getInt(TAG_NUM);
Log.d("JFN","Number of contacts stated: "+numContacts);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
Log.d("JFN","Number of contacts present: "+contacts.length());
// Loop over contacts
for( int i=0; i<contacts.length(); i++) {
    // Extract this one
    JSONObject c = contacts.getJSONObject(i);

    // Extract strings
    String first = (c.has(TAG_FIRST)) ? c.getString(TAG_FIRST) : "";
    String last = (c.has(TAG_LAST)) ? c.getString(TAG_LAST) : "";
    String city = (c.has(TAG_CITY)) ? c.getString(TAG_CITY) : "";
    String state = (c.has(TAG_STATE)) ? c.getString(TAG_STATE) : "";
    String country = (c.has(TAG_COUNTRY)) ? c.getString(TAG_COUNTRY) : "";
    String costName = (c.has(TAG_COST_NAME)) ? c.getString(TAG_COST_NAME) : "";

    // Temporary hash map for single contact
    HashMap<String, String> contact = new HashMap<String, String>();
    contact.put("name", first+" "+last);
    contact.put("email", city+", "+state+", "+country);
    contact.put("mobile", costName);

    // Adding single contact to list
    contactList.add(contact);
}
Log.d("JFN","Done looping contacts");
//Updating parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(
        MainActivity.this, contactList,
        R.layout.list_item, new String[] { "name", "email",
                "mobile" }, new int[] { R.id.name,
                R.id.email, R.id.mobile });
setListAdapter(adapter);

解决方案

Allright I was having a similar kind of problem but i was using a ViewPager with a ListFragment and a custom ListView Layout. In the above case you are using just ImageViews, but just in case if you use controls like ImageButton, CheckBox ,Button etc then you would face problems discussed here and here. This is simply because such controls can steal focus from the ListView and the complete list item cant be selected/clicked. so I would say that just using ImageViews is a smart move. I am assuming that you are using an Adapter to set the content of your list. Inside that adapter you can assign onClickListener()s for each item like this:

public class MyListAdapter extends ArrayAdapter<String>{
    .......
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         View rowView=inflator.inflate(R.layout.channel_list_item, null, true);
         ImageView chat=(ImageView) rowView.findViewById(R.id.chat);
         chat.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 //do something
            }
         }

         ImageView calendar=(ImageView) rowView.findViewById(R.id.calendar);
         calendar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 //do something
            }
         }
         .......
    }
}

but remember that while using controls like ImageButton, CheckBox ,Button you need to assign property android:focusable="false" in the XML. and for the ImageButton you need to do this inside the getView() method:

    final ImageButton imgBtn=(ImageButton) rowView.findViewById(R.id.imgBtn);
    imgBtn.setFocusable(false);
    imgBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //do your task here
        }
    });

or you can also use Nathaniel Waggoner's approach.

Hope I answered your question.