3

我正在使用包含一些文本视图和水平列表视图的自定义列表视图。

自定义列表视图的所有视图都正确填充,并且 OnClick 侦听器正常工作。

现在我正在使用第二个基本适配器来填充包含图像的水平列表视图。

填充水平列表视图后,我尝试单击水平列表视图中的相对布局,它没有触发。

这是我的代码:

public class ThreadListNewAdapter extends ArrayAdapter<Thread> {

private Context context;
private ImageLoader imageLoader;
private HorizontialListView horizontialListView;

public ThreadListNewAdapter(Context context, int textViewResourceId,
        List<CompressedThread> threadList) {

    super(context, textViewResourceId, threadList);
    this.context = context;
    imageLoader = new ImageLoader(context);

}

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

    ImageView userImage;
    View myView = convertView;

    LayoutInflater inflater = (LayoutInflater) context // activity.getLayoutInflater();
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    myView = inflater.inflate(R.layout.popular_new, null);

    myView.setTag(threadId);

    final View view = myView; 

    if (mediaList.size() > 0) {
        imageLayout.setVisibility(View.VISIBLE);
        horizontialListView = (HorizontialListView) myView
                .findViewById(R.id.media_horizontal_view);
        horizontialListView.setAdapter(new ImageAdapter(mediaList)); //PLACE WHHERE I AM CALLING SECOND ADAPTER
    }

    view.setTag(position);
    view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            onThreadClick(fThread);
        }
    });

    return myView;
}

class ImageAdapter extends BaseAdapter {

    List<Media> mediaList = new ArrayList<Media>();

    public ImageAdapter(List<Media> allMediaList) {
        mediaList = allMediaList;
    }

    @Override
    public int getCount() {
        return mediaList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos = position;
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.inbox_horizontal_row,
                    null);
            holder.imageView = (ImageView) convertView
                    .findViewById(R.id.media_image_view);
            holder.commentLayout = (RelativeLayout) convertView
                    .findViewById(R.id.comment_layout);

            holder.commentLayout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.v("Clicked", "Clicked" + "" + pos);
                }

            }); // CLICK EVENT IS NOT FIRING HERE

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mediaId = position;
        return convertView;
    }
}

class ViewHolder {
    ImageView imageView;
    TextView commentText, likeText;
    RelativeLayout commentLayout;
    int mediaId;
}

}

水平 XML:

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

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/media_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/like_comment_layout"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:background="#80000000"
        android:clickable="true"
        android:focusable="true" >

        <RelativeLayout
            android:id="@+id/comment_layout"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5dp"
                android:src="@drawable/comment" />
           </RelativeLayout>
          </RelativeLayout>
        </FrameLayout>
    </LinearLayout>
4

4 回答 4

1

使您的相对布局可点击和可聚焦的属性为 true,并且其所有子项的属性为 false。

于 2012-02-20T05:55:12.377 回答
1

最后我改变了在列表视图中添加水平图像的方法。

在我尝试创建新的适配器并对其进行充气之前,现在我创建了 DYNAMIC TABLE LAYOUT 并向其中添加了图像。

我改变的地方:

if (mediaList.size() > 0) {
   for (int i = 0; i < mediaList.size(); i++) {
       mediaInside = mediaList.get(i);
       LinearLayout row = (LinearLayout) inflater
                    .inflate(R.layout.inbox_horizontal_row,
                            dynamicMediaTable, false);
       final ImageView mediaImage = (ImageView) row
                    .findViewById(R.id.media_image_view);
       final RelativeLayout likeComment = (RelativeLayout) row
                    .findViewById(R.id.like_comment_layout);
       mediaImage.setImageDrawable(icon);

       dynamicMediaTable.addView(row);
       likeComment.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.v("Media Id Clicked", mediaImage.getTag()
                }
        });
    }

}

我的主要 XML :

<RelativeLayout
    android:id="@+id/image_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="65dp"
    android:layout_marginTop="10dp" >

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/dynamic_table_row"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" />
        </TableLayout>
    </HorizontalScrollView>

    <ImageView
        android:id="@+id/inbox_reply_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="5dp"
        android:src="@drawable/replybutton" />
</RelativeLayout>

我的动态布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
    <ImageView
        android:id="@+id/media_image_view"
        android:layout_width="180dp"
        android:layout_height="240dp" />
</LinearLayout>
于 2012-02-22T13:25:23.993 回答
0

我认为这种情况是父布局(水平列表视图)采用了应该由子视图(相对布局)进行的点击事件。不知道您的 Horizo​​ntalListView 是否从 AbsListView 扩展,但您可以尝试此代码listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

于 2012-02-20T06:04:41.977 回答
-1

你能发布你的 inbox_horizo​​ntal_row XML 的内容吗?您可以尝试在您的 RelativeLayout XMLandroid:descendantFocusability="blocksDescendants"android:descendantFocusability="beforeDescendants"

于 2012-02-20T06:18:49.560 回答