OnClickListener在Android视图中不使用嵌套元素

时间:2014-04-07 21:15:33

标签: android xml

所以我有一些嵌套布局的视图,我希望能够点击@ + id / likeStar imageview。我知道我的OnClickListener是正确的,因为它一直工作,直到我开始添加嵌套布局。我需要更改什么才能再次点击ImageView?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical"
    android:paddingBottom="8dp"
    android:paddingTop="8dp" >

    <!-- Title -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical"
        android:paddingLeft="8dp"
        android:paddingRight="8dp" >

        <TextView
            android:id="@+id/title"
            style="@style/CardTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="false"
            android:focusable="false"
            android:text="Title" />

        <ImageView
            android:id="@+id/likeStar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:background="@drawable/ic_action_important"
            android:clickable="true"
            android:focusableInTouchMode="false" />
    </LinearLayout>

    <!-- Stroke -->

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="4dp"
        android:background="@color/stroke"
        android:clickable="false"
        android:focusable="false" />

    <!-- Card text -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        android:gravity="center_vertical"
        android:padding="4dp" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:background="@drawable/poster" />
        <!-- Fixed views (aligned to the left) -->


        <!-- Dynamic views (aligned to the right) -->

        <TextView
            android:id="@+id/cardPlayDuration"
            style="@style/CardText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="8dp"
            android:layout_toRightOf="@id/imageView"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="126min" />

        <TextView
            android:id="@+id/cardCategory"
            style="@style/CardText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/cardPlayDuration"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/imageView"
            android:ellipsize="end"
            android:lines="3"
            android:maxLines="1"
            android:text="Action  Adventure  Sci-Fi" />

        <TextView
            android:id="@+id/cardRating"
            style="@style/CardText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/cardCategory"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/imageView"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="7,9/10" />

        <TextView
            android:id="@+id/cardDirector"
            style="@style/CardText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/cardRating"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/imageView"
            android:ellipsize="end"
            android:maxLines="2"
            android:text="Jon Favreau" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:clickable="false"
        android:focusable="false"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:padding="4dp"
            android:text="When wealthy industrialist Tony Stark is forced to build an armored suit after a life-threatening incident, he ultimately decides to use its technology to fight against evil."
            android:textColor="#000000" />
    </LinearLayout>

</LinearLayout>

活动代码

public class MovieCard extends Card {
private Context appContext;
private String movieTitle, duur, score, regiseur, descr, genre, imgurl;
private ImageView like;
private ImageView moviecover;


public MovieCard(Context context, String movieTitle, String duur, String score, String regiseur, String genre, String descr, String imgurl) {
    appContext = context;
    this.movieTitle = movieTitle;
    this.duur = duur;
    this.score = score;
    this.regiseur = regiseur;
    this.genre = genre;
    this.descr = descr;
    this.imgurl = imgurl;
}

@Override
public View getCardContent(Context context) {
    View view = LayoutInflater.from(context).inflate(R.layout.moviecardlayout, null);

    moviecover = ((ImageView) view.findViewById(R.id.imageView));

    GetXMLTask task = new GetXMLTask();
    task.execute(new String[] { imgurl });

    like = ((ImageView) view.findViewById(R.id.likeStar));

    like.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(appContext, "LOL", Toast.LENGTH_LONG);
            return false;
        }
    });
    like.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(appContext, "LOL", Toast.LENGTH_LONG);
            like.setImageResource(R.drawable.ic_action_important_liked);
            SharedPreferences mPrefs= appContext.getSharedPreferences(appContext.getApplicationInfo().name, Context.MODE_PRIVATE);
            SharedPreferences.Editor ed=mPrefs.edit();
            Gson gson = new Gson();
            ed.putString("myObjectKey", gson.toJson(this));
            ed.commit();
        }
    });
    // Set title
    TextView titleView = ((TextView) view.findViewById(R.id.title));
    titleView.setText(movieTitle);

    TextView duration = ((TextView) view.findViewById(R.id.cardPlayDuration));
    duration.setText(duur);

    TextView imdbscore = ((TextView) view.findViewById(R.id.cardRating));
    imdbscore.setText(score + "/10");

    TextView director = ((TextView) view.findViewById(R.id.cardDirector));
    director.setText(regiseur);

    TextView gen = ((TextView) view.findViewById(R.id.cardCategory));
    gen.setText(genre);

    TextView description = ((TextView) view.findViewById(R.id.description));
    description.setText(descr);

    return view;
}

@Override
public boolean convert(View convertCardView) {
    return false;  //To change body of implemented methods use File | Settings | File Templates.
}

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {
        moviecover.setImageBitmap(result);
    }

    // Creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}
}

3 个答案:

答案 0 :(得分:0)

如果它确实是片段活动或活动,则更改以下行

like = ((ImageView) view.findViewById(R.id.likeStar));

like = ((ImageView) findViewById(R.id.likeStar));

答案 1 :(得分:0)

我认为问题不在您的布局中。我假设这个布局作为listview中的项目并且使用了dataadapter,在getView方法中你使用onClick on inflated view来窃取来自children的MotionEvent。处理单击的两种方法 - 使用onTouchEvent或更简单的方法是使用listview提供的onItemClick事件。

答案 2 :(得分:0)

请你试试getCardContent(Context context)view.invalidate(); return view;