2015-04-06 39 views
0

我有一個自定義列表視圖具有以下佈局(list_single.xml):Android的ListView控件 - 使用整排

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" > 
    <TableRow android:layout_width="fill_parent"> 
     <ImageView 
      android:id="@+id/img" 
      android:layout_width="64dp" 
      android:layout_height="64dp"/> 
     <TextView 
      android:id="@+id/txt" 
      android:textSize="24dp" android:textStyle="bold" 
      android:paddingTop="20dp" android:paddingLeft="10dp" 
      android:layout_width="fill_parent" 
      android:layout_height="64dp" /> 
    </TableRow> 
</TableLayout> 

然後,我有一個CustomList 擴展類被分配這個佈局:

public class CustomList extends ArrayAdapter<String> { 
    private final Activity context; 
    private final String[] web; 
    private final Integer[] imageId; 
    public CustomList(Activity context, 
       String[] web, Integer[] imageId) { 
      super(context, R.layout.list_single, web); 
      this.context = context; 
      this.web = web; 
     this.imageId = imageId; 
    } 
    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView= inflater.inflate(R.layout.list_single, null, true); 
     TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 
     txtTitle.setText(web[position]); 
     imageView.setImageResource(imageId[position]); 
     return rowView; 
    } 
} 

然後OnCreate事件處理程序中我添加一個偵聽器:

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

但是,行中唯一可點擊的區域是圖標,但我希望整行可點擊。我在這裏查了一些其他類似的問題,但我找不到TableLayout的解決方案。

謝謝你這麼多

+0

對於它你應該使用'ListView'而不是'TableLayout'。 – 2015-04-06 06:13:32

+0

是否有必要使用tableLayout? – Moudiz 2015-04-06 06:22:07

+0

不知道我是否應該TableLayout,因爲我只是遵循明顯使用這種佈局的教程。 – 2015-04-06 06:25:35

回答

0

以下建議我用相對佈局替換表,現在一切都按預期工作。十分感謝大家。非常感謝。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:id="@+id/img" 
     android:layout_width="64dp" 
     android:layout_height="64dp" android:paddingLeft="20dp" /> 
    <TextView 
     android:id="@+id/txt" 
     android:textSize="24dp" 
     android:textStyle="bold" 
     android:paddingTop="20dp" 
     android:paddingLeft="84dp" 
     android:layout_width="fill_parent" 
     android:layout_height="64dp" /> 
</RelativeLayout> 

對不起,不能夠投票(它需要我,因爲我的n00b沒有最低15分),非常感謝你的幫助。非常感謝。

0

你應該設置

android:focusableInTouchMode="true" 
android:focusable="true" 

在你的表格佈局。一般來說,它不是一個好主意,使用表格佈局n列布局的列表視圖。對於您的情況,您可以通過任何其他佈局輕鬆完成此操作。 linearlayout,relativelayout等

+0

這沒有幫助。甚至更多的listview變得不響應。順便說一句,你們可以向我展示一個使用相對佈局來自定義列表視圖的例子。我只需要在文本左側放置一個圖像,並讓整行可點擊。這就是我需要的。沒有什麼花哨 :) – 2015-04-06 06:51:01