Listview OnItemClickListener,客户适配器无法正常工作

时间:2015-10-07 13:40:12

标签: android listview android-listview android-adapter

多次看过这个问题,我使用客户适配器用我的客户布局xml填充我的Listview。我理解点击可能永远不会进入列表视图,我的布局阻止了它,但在尝试了可能的解决方案后似乎没有尝试:

尝试: 机器人:descendantFocusability = “blocksDescendants” 用于我的自定义xml的布局

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" 

布局

中的每个元素

任何其他解决方法

  

修改

添加listview附加到适配器的代码:

if(!allcontactsstring.isEmpty()){
  adapter = new MyContactListAdapter(getContext(),R.layout.contact_list_view,imageArry);
       listView.setAdapter(adapter);
   }

AdapterView.OnItemClickListener mylistViewClicked = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG);
        }
    };
    listView.setOnItemClickListener(mylistViewClicked);

我的自定义适配器:

public class MyContactListAdapter extends ArrayAdapter<MySQLiteContact> {

Context context;
int layoutResourceId;
ArrayList<MySQLiteContact> data=new ArrayList<MySQLiteContact>();


public MyContactListAdapter(Context context, int resource, ArrayList<MySQLiteContact> objects) {
    super(context, resource, objects);
    //this.layoutResourceId = layoutResourceId;
    this.layoutResourceId = resource;
    this.context = context;
    this.data = objects;



}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ImageHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        //ImageView tmp = (ImageView)row.findViewById(R.id.icon);
        //tmp.clearFocus();
        //tmp.isClickable();

        //TextView tmp1=(TextView)row.findViewById(R.id.Itemname);
        //tmp1.clearFocus();


        holder = new ImageHolder();
        holder.txtTitle = (TextView)row.findViewById(R.id.Itemname);
        holder.imgIcon = (ImageView)row.findViewById(R.id.icon);
        row.setTag(holder);

    }
    else
    {
        holder = (ImageHolder)row.getTag();
    }

    MySQLiteContact picture = data.get(position);
    holder.txtTitle.setText(picture.getsFirstName()+" "+picture.getsLastName());
    byte[] outImage=picture.getBimage();
    ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
    Bitmap theImage = BitmapFactory.decodeStream(imageStream);
    theImage=Bitmap.createScaledBitmap(theImage , 100, 100, true);
    theImage= ContactDetailsMain.getRoundedRectBitmap(theImage,100);
    holder.imgIcon.setImageBitmap(theImage);


    return row;



}

static class ImageHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}

}

我的带有文本视图和imageview的xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"   
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<ImageView                                          
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:src="@drawable/ic_contact_picture_holo_light"
/>
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="@color/black"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:paddingTop="5dp"
/>
</LinearLayout>
  

EDIT   一个肮脏的解决方法

作为一个粗糙和肮脏的解决方法,我在适配器内部添加了一个点击监听器,以便每个布局都被充气,这似乎目前正在起作用。

if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println(position);
            }
        });

拥有如此多的听众并不干净,只有一个听起来不错,但是现在它是的,是的。

1 个答案:

答案 0 :(得分:0)

您的点击是否有效,它对您的代码无效的事实是您没有看到Toast对吗?您忘记在Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG)上调用show {),该Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG).show()

享受:p

修改

List click 1 List click 2