我有一个动态列表视图布局。我为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>
MY 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);
}
}
答案 0 :(得分:0)
您是否尝试使用RelativeLayout包装ListView,然后在RelativeLayout上添加一个android:onClick?
答案 1 :(得分: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);
}});