Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为整个列表视图创建onclick,而不是android中的单个行_Android_Listview_Android Listview_Onclick - Fatal编程技术网

为整个列表视图创建onclick,而不是android中的单个行

为整个列表视图创建onclick,而不是android中的单个行,android,listview,android-listview,onclick,Android,Listview,Android Listview,Onclick,我有一个动态列表视图布局。我已经为listview创建了5行(硬编码)。现在,默认情况下,Android为每一行生成一个clickevent。但是,我想为整个listview块设置onClick事件,并禁用单独的行点击。我该怎么做?我想点击listview的整个框,并将用户重定向到另一个布局:layout2.XML 我的XML文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="htt

我有一个动态列表视图布局。我已经为listview创建了5行(硬编码)。现在,默认情况下,Android为每一行生成一个clickevent。但是,我想为整个listview块设置onClick事件,并禁用单独的行点击。我该怎么做?我想点击listview的整个框,并将用户重定向到另一个布局:layout2.XML

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/stream_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:fontFamily="sans-serif-light"
        android:text="@+id/stream_name"
        android:textSize="14sp" />


    <ImageView
    android:id="@+id/your_fit_image"
    android:layout_width="17sp"
    android:layout_height="17sp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/stream_name"
    android:layout_marginRight="5dp"
    android:contentDescription="@string/desc"
    android:src="@drawable/smiley" />

    <TextView
        android:id="@+id/dotsView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/stream_name"
        android:layout_alignBottom="@+id/stream_name"
        android:layout_toRightOf="@+id/stream_name"
        android:ellipsize="end"
        android:paddingRight="20dp"
        android:text="@string/dots"
        android:textColor="@color/dottedlines" />

</RelativeLayout>

我的JAVA文件:

package org.in.pappu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Details_Kollege extends Activity {

    static final String[] mystreams_list = new String[] { 
        "Electronics & Telecom",
        "Information Technology", 
        "Computer", 
        "Mechanical",
        "Instrumentation", };
    int[] fit_image = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};

    public static void updateListViewHeight(ListView myListView) {
         ListAdapter myListAdapter = myListView.getAdapter();
         if (myListAdapter == null) {            
                  return;
         }
        //get listview height
        int totalHeight = 0;
        int adapterCount = myListAdapter.getCount();
        for (int size = 0; size < adapterCount ; size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        //Change Height of ListView 
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
        myListView.setLayoutParams(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details_kollege); 


        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

        for(int i=0;i<5;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("stream_name", mystreams_list[i]);
            hm.put("your_fit_image", Integer.toString(fit_image[i]) );          
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "stream_name", "your_fit_image"};

        // Ids of views in listview_layout
        int[] to = { R.id.stream_name,R.id.your_fit_image};  

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.streams_list, from, to);

        // Getting a reference to listview of main.xml layout file
        ListView listView = ( ListView ) findViewById(R.id.streams_listview);

        // Setting the adapter to the listView
        listView.setAdapter(adapter); 
        updateListViewHeight(listView);

    }


}
package org.in.pappu;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
公共课堂详细信息\u Kollege扩展活动{
静态最终字符串[]mystreams\u list=新字符串[]{
“电子与电信”,
“信息技术”,
“计算机”,
“机械”,
“仪器”,};
int[]fit_image=new int[]{R.drawable.ic_启动器,R.drawable.ic_启动器,R.drawable.ic_启动器,R.drawable.ic_启动器,R.drawable.ic_启动器};
公共静态void updateListViewHeight(ListView myListView){
ListAdapter myListAdapter=myListView.getAdapter();
如果(myListAdapter==null){
返回;
}
//获取列表视图高度
int totalHeight=0;
int adapterCount=myListAdapter.getCount();
对于(int size=0;size对于(int i=0;i您是否尝试过使用RelativeLayout包装列表视图,然后在RelativeLayout上添加一个android:onClick?

尝试:

listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
           int position, long id) {

                Intent i = new Intent(Details_Kollege.this,
                    NextActivityName.class);
                startActivity(i);

           }});
listView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
意向i=新意向(细节,
NextActivityName.class);
星触觉(i);
}});