View.OnClickListener无效

时间:2017-09-26 15:02:50

标签: android android-recyclerview onclicklistener android-viewholder

我的View.OnClickListener中有Recycler View,但每当我点击一个视图时,它都不会触发onClick事件

以下是代码:

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView name;
    ImageView image;
    Context context;
    ArrayList<Categories_Model_Class> arrayList;

    public ViewHolder(View view, Context context, ArrayList<Categories_Model_Class> arrayList) {
        super(view);
        name = view.findViewById(R.id.category_name);
        image = view.findViewById(R.id.category_image);
        this.context = context;
        this.arrayList = arrayList;
        view.setOnClickListener(this);
        Log.i("Created", "ViewHolder");
    }

    @Override
    public void onClick(View view) {
        //Pair<View, String> pair1 = Pair.create((View) this.image, this.image.getTransitionName());
        Intent intent = new Intent(context, FullWallpaperViewActivity.class);
        //ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity)context, pair1);
        context.startActivity(intent);
        Log.i("TOUCH", "TOUCH");
    }
}

任何建议?

  • CardView模型类的布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackground"
    android:orientation="vertical">
    
    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_margin="4dp"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackground">
    
    
    <ImageView
        android:id="@+id/category_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:background="@color/colorBackground" />
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_gradient" />
    
    <TextView
        android:id="@+id/category_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="sans-serif-smallcaps"
        android:text="Category!"
        android:textColor="@android:color/white"
        android:textSize="24dp" />
    
    </FrameLayout>
    </LinearLayout>
    

2 个答案:

答案 0 :(得分:2)

顶部布局(<LinearLayout>中的view视图(在代码中作为android:clickable访问)正在自行处理点击事件。

您需要从<FrameLayout>中移除<FrameLayout android:layout_width="match_parent" android:layout_height="150dp" android:layout_margin="4dp" android:focusable="true" android:foreground="?attr/selectableItemBackground"> ,如此:

< LinearLayout>

如果没有,它将消耗点击,不会到达父var options = new ParallelOptions { MaxDegreeOfParallelism = 4 }; Parallel.ForEach(items, options, t => { try { var result = DoWork(t.A, t.Range).Result; } catch (Exception ex) { _logger.LogError(ex, $"{ex.ToString()}"); } }); public async Task<string> DoWork(A table, SqlXml range) { _context.Database.SetCommandTimeout(150000); using (var cmd = Conn.CreateCommand()) { cmd.CommandTimeout = 1200; cmd.CommandText = "storedProc"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.SmallInt) { Value = table.Id }); cmd.Parameters.Add(new SqlParameter("@Range", SqlDbType.Xml) { Value = range }); var result = await cmd.ExecuteScalarAsync(); return result as string; } }

答案 1 :(得分:0)

view.setClickable(true);
view.setOnClickListener(this);