Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OnClickListener不使用Android视图中的嵌套元素_Android_Xml - Fatal编程技术网

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

OnClickListener不使用Android视图中的嵌套元素,android,xml,Android,Xml,因此,我有一个带有一些嵌套布局的视图,我希望能够单击@+id/likeStar imageview。我知道我的OnClickListener是正确的,因为它一直工作到我开始添加嵌套布局。要使ImageView再次可单击,我需要更改什么 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layo

因此,我有一个带有一些嵌套布局的视图,我希望能够单击@+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;
    }
}
}
公共级电影卡扩展卡{
私人语境;
私人弦乐电影,duur,乐谱,regiseur,descr,流派,imgurl;
私密的图像视图;
私家影城;
公共电影卡(上下文、字符串电影标题、字符串duur、字符串乐谱、字符串regiseur、字符串流派、字符串描述、字符串imgurl){
appContext=context;
this.movieTitle=movieTitle;
this.duur=duur;
这个分数=分数;
this.regiseur=regiseur;
这个。流派=流派;
this.descr=descr;
this.imgurl=imgurl;
}
@凌驾
公共视图getCardContent(上下文){
视图=LayoutFlater.from(上下文)。充气(R.layout.moviecardlayout,null);
moviecover=((ImageView)view.findviewbyd(R.id.ImageView));
GetXMLTask task=新建GetXMLTask();
execute(新字符串[]{imgurl});
like=((ImageView)view.findviewbyd(R.id.likeStar));
like.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
Toast.makeText(appContext,“LOL”,Toast.LENGTH\u LONG);
返回false;
}
});
like.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(appContext,“LOL”,Toast.LENGTH\u LONG);
like.setImageResource(R.drawable.ic\u action\u important\u like);
SharedReferences mPrefs=appContext.getSharedReferences(appContext.getApplicationInfo().name,Context.MODE\u PRIVATE);
SharedReferences.Editor ed=mPrefs.edit();
Gson Gson=新的Gson();
ed.putString(“myObjectKey”,gson.toJson(this));
ed.commit();
}
});
//定名
TextView titleView=((TextView)view.findViewById(R.id.title));
titleView.setText(电影字幕);
TextView持续时间=((TextView)view.findViewById(R.id.cardPlayDuration));
持续时间.setText(duur);
TextView imdbscore=((TextView)view.findViewById(R.id.cardRating));
imdbscore.setText(分数+“/10”);
TextView控制器=((TextView)view.findViewById(R.id.cardDirector));
setText(regiseur)董事;
TextView gen=((TextView)view.findViewById(R.id.cardCategory));
gen.setText(体裁);
TextView描述=((TextView)view.findViewById(R.id.description));
description.setText(descr);
返回视图;
}
@凌驾
公共布尔转换(视图转换CARDVIEW){
return false;//若要更改已实现方法的主体,请使用文件|设置|文件模板。
}
私有类GetXMLTask扩展了AsyncTask{
@凌驾
受保护位图doInBackground(字符串…URL){
位图映射=空;
for(字符串url:url){
map=下载图像(url);
}
返回图;
}
//设置doInBackground返回的位图
@凌驾
受保护的void onPostExecute(位图结果){
moviecover.setImageBitmap(结果);
}
//从InputStream创建位图并返回它
私有位图下载图像(字符串url){
位图=空;
InputStream=null;
BitmapFactory.Options bmOptions=新的BitmapFactory.Options();
bmOptions.inSampleSize=1;
试一试{
流=getHttpConnection(url);
位图=位图工厂。
解码流(流,空,bmOptions);
stream.close();
}捕获(IOE1异常){
e1.printStackTrace();
}
返回位图;
}
//建立HttpURLConnection并返回InputStream
私有InputStream getHttpConnection(字符串urlString)
抛出IOException{
InputStream=null;
URL=新URL(URL字符串);
URLConnection=url.openConnection();
试一试{
HttpURLConnection httpConnection=(HttpURLConnection)连接;
setRequestMethod(“GET”);
httpConnection.connect();
if(httpConnection.getResponseCode()==HttpURLConnection.HTTP\u确定){
stream=httpConnection.getInputStream();
}
}捕获(例外情况除外){
例如printStackTrace();
}
回流;
}
}
}

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

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


我想问题不在你们的布局上。我假设此布局作为listview中的项,并使用dataadapter,其中在getView方法中,您在膨胀视图上使用onClick,它从子级窃取MotionEvent。处理单击的两种方法-使用onTouchEvent或更简单的方法是使用listview提供的onItemClick事件。

请在
getCardContent(上下文)
中尝试。
view.invalidate();

返回视图

这是一个疯狂的外观布局。确定你不能在一个RelativeLayout中完成你想做的事情吗?我认为不可能只在一个RelativeLayout中完成。你能分享你的活动/片段活动代码吗?感谢包含活动代码。如果它确实是一个片段活动或活动,请更改以下行“like=((ImageView)view.findViewById(R.id.likeStar));”到'like=((ImageView)findviewbyd(R.id.likeStar));'对不起,我的错!侦听器的代码不在活动中,而是在一个普通类中,因此当我删除“视图”时,它表示无法解析方法findViewById
like = ((ImageView) findViewById(R.id.likeStar));