Dialog,ListView和setOnItemClickListener的问题

时间:2013-08-27 13:53:13

标签: android listview android-listview dialog actionlistener

我使用ListView和自定义适配器。在此适配器中,我想使用HorizontalScrollView来连续显示完整信息。我得到了很长的文字所以我必须滚动。 很遗憾,我的setOnItemClickListener无法识别点击它。如果删除horizo​​ntalScrollView,它可以工作。在过去的几个小时里,我在这里阅读了很多关于这个主题的内容,但没有什 即使我的ListView也可以水平滚动,也可以接收事件吗?

    public void show_alert() {

    final Dialog dia = new Dialog(this);

    dia.setContentView(R.layout.alert);

    dia.setTitle(String.format(res.getString(R.string.msg_11)));
    dia.setCancelable(true);

    list_alert = (ListView) dia.findViewById(R.id.alert_list);

    list_alert.setAdapter(new MainActivityArrayAdapter(this, GetProtokollFileNames()));                                            

    this.list_alert.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {

            Toast.makeText(MainActivity.this,
                    "Item in position " + pos + " clicked", Toast.LENGTH_LONG).show();              
            dia.dismiss();
        }
    });               
    dia.show();
}

类MainActivityArrayAdapter(...)使用rowlayout.xml。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;

import android.content.Context;
import android.content.pm.LabeledIntent;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivityArrayAdapter extends ArrayAdapter<String> {
  private final Context context;

  private String[] Values;

  public MainActivityArrayAdapter(Context context, String[] values) {
    super(context, R.layout.rowlayout, values);
    this.context = context;    
    Values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      View rowView = inflater.inflate(R.layout.rowlayout, parent, false);


    return rowView;
  }
} 

rowlayout.xml

  

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:scrollbarAlwaysDrawVerticalTrack="false"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"                        
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

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

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_menu_name" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/lbl_datum"
                    android:clickable="false"
                    android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                    android:text="22.03.2013" />

                <TextView
                    android:id="@+id/lbl_time"
                    android:clickable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="10:50:45" />
            </LinearLayout>              

            <Space
                android:layout_width="10dp"
                android:layout_height="match_parent" />

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

                <TextView
                    android:id="@+id/lbl_strasse"
                    android:clickable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Straße" />

                <TextView
                    android:id="@+id/lbl_plz"
                    android:clickable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="plz" />

                <TextView
                    android:id="@+id/lbl_ort"
                    android:clickable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ort" />
            </LinearLayout>

             <Space
                android:layout_width="10dp"
                android:layout_height="match_parent" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:clickable="false"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/lbl_auftraggeber"
                    android:clickable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Auftraggeber" />

                <TextView
                    android:id="@+id/textView5"
                    android:clickable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </LinearLayout>

        <TextView
            android:id="@+id/lbl_bemerkung"
            android:clickable="false"
            android:layout_width="354dp"
            android:layout_height="wrap_content"
            android:paddingLeft="50dp"
            android:text="Bemerkung" />

    </LinearLayout>
</HorizontalScrollView>
     

alert.xml

 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/alert_list"
        android:clickable="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

    </ListView>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您可以实现自定义视图(horizo​​ntalScrollView)并覆盖某些方法:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean is_event = super.onInterceptTouchEvent(ev);
    if (is_event)
        getParent().requestDisallowInterceptTouchEvent(true);
    return ret;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    boolean is_event = super.onTouchEvent(ev);
    if (is_event)
        getParent().requestDisallowInterceptTouchEvent(true);
    return ret;
}  

Sample here

答案 1 :(得分:0)

  

此代码是您的问题的解决方案,您只需要为listView编写list_alert.setOnItemClickListener(...)和setClickable(true)

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Your Title");
dialog.setCancelable(true);
dialog.show();

list_alert = (ListView) dialog.findViewById(R.id.alert_list);
list_alert.setClickable(true);

list_alert.setAdapter(new MainActivityArrayAdapter(this, GetProtokollFileNames()));                                            

list_alert.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
            long arg3) {

        Toast.makeText(MainActivity.this,
                "Item in position " + pos + " clicked", Toast.LENGTH_LONG).show();              
        dialog.dismiss();
    }
});