列表视图

时间:2017-07-13 11:12:04

标签: android listview android-recyclerview onclicklistener

我在列表视图中创建了一个recylerview,如image ,recylerview包含cardview,listview包含简单的文本数组,我在我的适配器中设置了onclicklistner,但是当我点击时,我检查不起我制作的日志,但它没有出现。

Recycler查看代码:

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));


    }


}

课程名称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>

2 个答案:

答案 0 :(得分: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);
        }
    }
}

答案 1 :(得分: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));
                }
            }
        });
    }