ListView - 如果单击ImageView,则启动活动

时间:2015-10-27 13:21:43

标签: android android-layout listview android-listview

我的ListView通过此获取filles

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

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

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:id="@+id/projectImage"
            android:maxHeight="70dp"
            android:maxWidth="70dp"
            android:adjustViewBounds="true"
            android:minHeight="50dp"
            android:minWidth="50dp"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:id="@+id/projectTitle"
                    android:textStyle="bold"
                    android:focusable="false"
                    android:clickable="false"
                    android:textSize="15dp"
                    android:text="projecttitle" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/projectTopic"
                    android:focusable="false"
                    android:clickable="false"
                    android:textSize="14sp"
                    android:text="projectTopic"
                    android:layout_marginTop="3dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/projectdeadline"
                    android:textSize="14sp"
                    android:text="projectdeadline"
                    android:layout_marginTop="3dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="projectTaskCount"
                    android:id="@+id/projectTaskCount"
                    android:layout_marginTop="3dp" />

            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

它包含一个ImageView和三个TextView。 如果我点击ImageView我要启动Activity示例1,请打开下一个Activity WiFi。

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

    Intent in = new Intent(this, WiFi.class);

    in.putExtra("projectFileNamesMap", projectFileNamesMap.get(position));
    in.putExtra("position", position);
    in.putExtra("sessionId", sessionId);
    startActivity(in);

}

如何解决我的问题?

概括为: 如果我点击ImageView打开Example1活动。 如果点击其他地方,则打开WiFi活动。

希望每个人都能理解这一点。

亲切的问候!

3 个答案:

答案 0 :(得分:2)

在getView方法中使用image.setOnclickListener和view.setOnItemCLickListener来执行点击图片视图和整个视图。

答案 1 :(得分:1)

首先设置你的图像:

android:clickable="true" //setOnClickListener makes a view clickable if it doesn't have that as a default but use it anyway.

然后:

imgView.setOnClickListener(new View.OnClickListener() {
   //@Override
   public void onClick(View v) {
      Log.v(TAG, " click");         
   }        
});

请告诉我这是否有效

答案 2 :(得分:1)

在适配器的getView方法中为listView的每个ImageView元素创建新实例时为ImageView编写OnClickListener

在将Linear添加到Linearlayout之前,请确保设置适当的LayoutParams。

public View getView(final int position, View convertView, ViewGroup parent) {
        TextView txtOne, txtTwo, txtThree;
        ImageView iv;
        LinearLayout layout;

        if(convertView == null){
            layout = new LinearLayout(context);
            txtOne = new TextView(context);
            txtTwo = new TextView(context);
            txtThree = new TextView(context);
            iv = new ImageView(context);
        } else{
            layout = (LinearLayout) convertView;
            txtOne = (TextView) layout.getChildAt(0);
            txtTwo = (TextView) layout.getChildAt(0);
            txtThree = (TextView) layout.getChildAt(0);
            iv = (ImageView) layout.getChildAt(0);
        }
        layout.addView(txtOne);
        layout.addView(txtTwo);
        layout.addView(txtThree);
        layout.addView(iv);

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Do your handling here
            }
        });

}