0

我有一个动态列表视图布局。我为列表视图创建了 5 行(硬编码)。现在,默认情况下,Android 会为每一行生成一个点击事件。但相反,我想为整个列表视图块设置一个 onClick 事件并禁用单个行点击。我怎么做?我想点击整个列表视图框并将用户重定向到不同的布局: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);

    }


}
4

2 回答 2

0

你有没有试过用RelativeLayout包装ListView然后在RelativeLayout上添加一个android:onClick?

于 2013-09-19T04:25:26.423 回答
0

尝试:

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);

           }});
于 2013-10-30T23:36:48.743 回答