Android listview setOnItemClickListener和textview setOnLongClickListener在一起?

时间:2014-11-09 17:32:19

标签: java android android-layout listview android-listview

重写了整个问题。这是我想要做的一个简化的例子。我有一个自定义适配器列表视图。适配器生成的每一行都有多个textview。在两个文本视图中,longclick将生成一个对话框。而在另一种观点中,没有做任何事情。在所有3个视图中,如果我点击它们。列表视图的onitemclick应该被触发。我希望这个简化的例子可以帮助别人更好地理解我想要实现的目标,并提出一个解决方案。

代码的问题(如果你自己尝试),是因为实现了onlongclicklistener的两个textview没有触发onitemclick。任何人都有任何线索如何获得onitemclick来解雇这两个文本视图?感谢。

我有一个自定义arrayadapter的列表视图:

public class TestListView  extends ListView{
    final Context mContext;
    List<String> list = new ArrayList<String>();
    public TestListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        list.add("Blue");
        list.add("Green");
        list.add("Red");
        list.add("Black");

        MyArrayAdapter adapter = new MyArrayAdapter(context, 0, list);
        this.setAdapter(adapter);

        this.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                Log.i("ApplicationName", "onItemClick " + position);
                Intent intent = new Intent(mContext, MainActivity.class);
                mContext.startActivity(intent);
            }
        });
    }
    private class MyArrayAdapter extends ArrayAdapter<String> {
        Context context;
        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public MyArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
    }

    @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.test_view_row, parent, false);
      TextView tv1 = (TextView) rowView.findViewById(R.id.tv1);
      TextView tv2 = (TextView) rowView.findViewById(R.id.tv2);
      TextView tv3 = (TextView) rowView.findViewById(R.id.tv3);

      tv1.setText(this.getItem(position));
      tv2.setText("tv2 text");
      tv3.setText("tv3 text");

      tv1.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new AlertDialog.Builder(context)
                .setMessage("You have onLongClicked a textview with color")
                .setPositiveButton(
                        "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    DialogInterface dialog,
                                    int which) {
                                dialog.cancel();
                            }
                        }).show();
                return false;
            }
        });
        tv3.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new AlertDialog.Builder(context)
                .setMessage("You have onLongClicked a generic textview")
                .setPositiveButton(
                        "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    DialogInterface dialog,
                                    int which) {
                                dialog.cancel();
                            }
                        }).show();
                return false;
            }
        });
      return rowView;
    }

  }
}

使用充气xmlView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal" >

<TextView
    android:id="@+id/tv1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:singleLine="true"
    android:gravity="center_vertical" />
<TextView
    android:id="@+id/tv2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:singleLine="true"
    android:gravity="center_vertical" />
<TextView
    android:id="@+id/tv3"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:singleLine="true"
    android:gravity="center_vertical" />
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

首先让你的listview可以点击Listview.Setclickable(true); - (参考textview抱歉)并对textview做同样的事情..并且还添加了可聚焦的&amp;&amp; focusbaleInTouchMode使textview为true 同样对于listview这样做.. Listview. setDescendantFocusability (View.FOCUS_AFTER_DESCENDANTS);

并在你的适配器中放置你的textview onlongitemclicklistener onlongclicklistener 。 请注意我只是打字,我不会再检查愚蠢的错误所以这样做..我们已经很晚了..谢谢 让我知道它是否有帮助

感谢您的修正

复制并粘贴

public class ListViewAdapter extends ArrayAdapter<Object> {

List<Object> AndroidPlist; 
int AndroidPres;
Context AndroidPcontext;

public ListViewAdapter(Context context, int resource, List<Object> objects) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
    this.AndroidPcontext = context;
    this.AndroidPlist = objects;
    this.AndroidPres = resource;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    if(AndroidPlist == null){
        return 0;
    }else{
        return AndroidPlist.size();
    }
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return AndroidPlist.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View child = convertView;
    final RecordHolder holder;
    if (child == null) { // checking for null
        LayoutInflater inflater = ((Activity) AndroidPcontext).getLayoutInflater(); // inflating the view
        child = inflater.inflate(AndroidPres, parent, false);
        holder = new RecordHolder();
        holder.name = (TextView) child.findViewById(R.id.tv);   // referencing your textview        
        child.setTag(holder);
    } else {
        holder = (RecordHolder) child.getTag(); //
    }
    holder.name.setText("set your text") // 
    holder.name.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View arg0) {
            // Hurray AndroidP, Elltz has solved your problem.. lol.


            return false;
        }
    });

    return child;
}

static class RecordHolder {
    TextView name;
}
}

setonclick你的列表视图,现在应该工作..如果它没有 - (我怀疑)..只需将你放在listview中的相同方法onclick in textview onlclick..lol ..你不是有问题吗?你呢?

答案 1 :(得分:0)

此代码可能对您有所帮助

            tv1.setOnLongClickListener(new View.OnLongClickListener() { 

                    @Override 
                    public boolean onLongClick(View v) {
                      // Put a Toast here and try 
                      //  Test long click works 
                    } 
                }); 

答案 2 :(得分:0)

在xmlView中定义以下行时,您无法处理setOnClicklistener:

 android:singleLine="true"

更改为:

android:lines="1"

这很奇怪,但它的工作