2017-07-13 23 views
0

我在這樣的image 的recylerview包含cardview和列表視圖列表視圖中創建一個recylerview放置在列表視圖Recylerview的項目只包含文本的數組,我有我的適配器設置onclicklistner但它不工作時我點擊,我檢查了我的製作日誌,但沒有顯示出來。的onclick聽者的

回收站查看代碼:

public class LessonVideoAdapter extends RecyclerView.Adapter<LessonVideoAdapter.LessonDataHolder> { 

    private List<VideoList> slipList; 


    public LessonVideoAdapter() { 
     slipList = new ArrayList<>(); 
     slipList.add(new VideoList("Chapter 1", R.drawable.demo_one)); 
     slipList.add(new VideoList("Chapter 2", R.drawable.demo_two)); 
     slipList.add(new VideoList("Chapter 3", R.drawable.demo_one)); 
     slipList.add(new VideoList("Chapter 4", R.drawable.demo_two)); 
     slipList.add(new VideoList("Chapter 5", R.drawable.demo_one)); 
     slipList.add(new VideoList("Chapter 6", R.drawable.demo_two)); 
     slipList.add(new VideoList("Chapter 7", R.drawable.demo_one)); 
     slipList.add(new VideoList("Chapter 8", R.drawable.demo_two)); 
     slipList.add(new VideoList("Chapter 9", R.drawable.demo_one)); 
     slipList.add(new VideoList("Chapter 10", R.drawable.demo_two)); 

    } 

    @Override 
    public LessonDataHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     Context context = parent.getContext(); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.abc_card_view_video, parent, false); 

     return new LessonDataHolder(view); 

    } 

    @Override 
    public void onBindViewHolder(LessonDataHolder holder, int position) { 

     VideoList s = slipList.get(position); 
     holder.bindSlip(s); 

    } 

    @Override 
    public int getItemCount() { 
     return slipList.size(); 
    } 


    public class LessonDataHolder extends RecyclerView.ViewHolder { 

     public BlurLayout samplelayout; 
     private TextView nameOfSlip; 
     private ImageView slipImage; 
     private Context context; 


     public LessonDataHolder(View itemView) { 
      super(itemView); 


//   context = itemView.getContext(); 
      nameOfSlip = (TextView) itemView.findViewById(R.id.demoText); 
      slipImage = (ImageView) itemView.findViewById(R.id.imageViewDemoVideo); 

      itemView.setClickable(true); 
      itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        { 
         Log.d("ashu", " clicked "); 
        } 
       } 
      }); 
     } 

     public void bindSlip(VideoList videoList) { 
      nameOfSlip.setText(videoList.stringSlipName); 
      slipImage.setImageResource(videoList.stringImage); 
     } 
    } 


    public class VideoList { 
     public String stringSlipName; 
     public int stringImage; 

     public VideoList(String stringSlipName, int stringImage) { 
      this.stringImage = stringImage; 
      this.stringSlipName = stringSlipName; 
     } 

    } 
} 

列表視圖代碼:

public class ListAdapter extends BaseAdapter { 


    private Context context; 
    private String[] names={"Lesson 1: Intro","Lesson 2: Addition","Lesson 3: Subtraction","Lesson 4: Multiplication","Lesson 5: Geometry","Lesson 6: Shapes","Lesson 7: Quad","Lesson 8: Fun","Lesson 9: Problems-1","Lesson 10: Problems-2"}; 
    LayoutInflater layoutInflater; 
    LessonVideoAdapter recyclerAdapter; 

    public ListAdapter(Context context) { 
     this.context = context; 
     layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     recyclerAdapter=new LessonVideoAdapter(); 
    } 

    @Override 
    public int getCount() { 
     return names.length; 
    } 

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

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

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     View view1=layoutInflater.inflate(R.layout.abc_lesson_name,null,false); 

     TextView tittle=(TextView)view1.findViewById(R.id.lesson_name); 
     tittle.setText(names[i]); 

     RecyclerView recyclerView=(RecyclerView)view1.findViewById(R.id.recyclerViewChapters); 
     recyclerView.setAdapter(recyclerAdapter); 

     LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false); 
     recyclerView.setLayoutManager(linearLayoutManager); 
     return view1; 
    } 

活動:

public class SubjectActivity extends AppCompatActivity { 

    private ListView listview_lesson; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_subject); 
     listview_lesson = (ListView) findViewById(R.id.listview_lesson); 
     listview_lesson.setAdapter(new ListAdapter(this)); 


    } 


} 

樂SSON名的xml:

<RelativeLayout 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="230dp" 
    android:paddingTop="10dp"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:elevation="3dp" 
     app:cardBackgroundColor="#b7a2d6" 
     app:cardCornerRadius="10dp"> 

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

      <TextView 
       android:id="@+id/lesson_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="10dp" 
       android:text="LessonName" 
       android:textColor="#fff" 
       android:textSize="20sp" 
       android:textStyle="bold" /> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/recyclerViewChapters" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/lesson_name" /> 
     </RelativeLayout> 
    </android.support.v7.widget.CardView> 
</RelativeLayout> 

abc_cardview_demo:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <com.daimajia.androidviewhover.BlurLayout 
     android:id="@+id/blur_layout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <android.support.v7.widget.CardView 
      android:id="@+id/cardViewDemo1" 
      android:layout_width="220dp" 
      android:layout_height="180dp" 
      android:layout_margin="5dp" 
      android:elevation="5dp" 
      app:cardBackgroundColor="#e4a455" 
      app:cardCornerRadius="10dp"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:padding="10dp" 
       android:weightSum="1"> 

       <FrameLayout 
        android:id="@+id/frameDemoOne" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight=".2"> 

        <ImageView 
         android:id="@+id/imageViewDemoVideo" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:paddingBottom="3dp" 
         android:paddingLeft="5dp" 
         android:paddingRight="5dp" 
         android:paddingTop="3dp" 
         android:scaleType="fitXY" 
         android:src="@drawable/demo_one" /> 

        <ImageView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:src="@drawable/cardview_border" /> 

       </FrameLayout> 


       <android.support.v7.widget.CardView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_marginTop="5dp" 
        android:layout_weight=".8" 
        app:cardBackgroundColor="#e9e6e3" 
        app:cardCornerRadius="10dp"> 

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

         <TextView 
          android:id="@+id/demoText" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerInParent="true" 
          android:text="DEMOVIDEOONE" 
          android:textColor="#c9750f" 
          android:textSize="15sp" 
          android:textStyle="bold" /> 

        </RelativeLayout> 
       </android.support.v7.widget.CardView> 
      </LinearLayout> 
     </android.support.v7.widget.CardView> 
    </com.daimajia.androidviewhover.BlurLayout> 
</RelativeLayout> 
+0

任何你正在尋找在日誌中的貓? –

+0

我們可以看到'abc_card_view_video'和'abc_lesson_name'的代碼嗎? –

+0

@MayurRaval我粘貼了我的代碼plz檢查出來 – Abhishek

回答

1

根據您的病情

public class RecyclerHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 
    public ImageView imageView; 
    public RecyclerHolder(View itemView) { 
     super(itemView); 
     imageView=(ImageView)itemView.findViewById(R.id.imageView2); 
     imageView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View view) { 
     if (view.getId() == imageView.getId()) { 
      Toast.makeText(view.getContext(),"Pressed " + String.valueOf(getAdapterPosition()),Toast.LENGTH_SHORT).show(); 
     // Intent intent=new Intent(context,ItemInfo.class); 
      // context.startActivity(intent); 
     } 
    } 
} 
0

嘗試這樣的..這將工作我猜。

RecyclerView recyclerView = (RecyclerView) 
convertView.findViewById(R.id.recyclerViewChapters); 


recyclerView.addOnItemTouchListener(new 
RecyclerTouchListener1(MainActivity.this, recyclerView, new ClickListener1() 
{ 
     @Override 
     public void onClick(View view, int position) { 

      ---Your Function--- 

      } 

public static class RecyclerTouchListener1 implements 
RecyclerView.OnItemTouchListener { 

    private GestureDetector gestureDetector; 
    private ClickListener1 clickListener; 

    public RecyclerTouchListener1(Context context, final RecyclerView 
    recyclerView, final ClickListener1 clickListener) { 
     this.clickListener = clickListener; 
     gestureDetector = new GestureDetector(context, new 
    GestureDetector.SimpleOnGestureListener() { 
      @Override 
      public boolean onSingleTapUp(MotionEvent e) { 
       return true; 
      } 

      @Override 
      public void onLongPress(MotionEvent e) { 
       View child = recyclerView.findChildViewUnder(e.getX(), 
       e.getY()); 
       if (child != null && clickListener != null) { 
        clickListener.onLongClick(child, 
       recyclerView.getChildPosition(child)); 
       } 
      } 
     }); 
    } 
+0

其中是ClickListener1類嗎? – Abhishek

+0

它不是一個類..第二個代碼是你在找什麼。 –