从包含卡片的Recyclerview打开新活动

时间:2017-01-20 21:46:50

标签: java android android-recyclerview android-cardview

看似简单的任务 -

使用回收者视图显示项目列表,每个项目都应该是卡片视图。点击任何项目应该打开新屏幕,显示该项目的详细信息。

我已经看到了关于同一主题发布的各种问题,我很感激我对我的尝试做了一点帮助,而不是将我重定向到那里,因为我很新,无法理解究竟是怎么回事只是通过查看这些答案来实现。我已经尝试过密切关注教程,但显然未能复制结果。这些文件是

  

MainActivity.java

. package com.example.hardikvats.recylerviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private String[] phones={"MotoX","Nokia Asha","Lumia 710","Iphone 7s"};
    private String[] OperatingSytem={"Android","Symbian","Windows","Ios"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RecyclerView recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        MyAdapter adapter=new MyAdapter(this,phones);
        recyclerView.setAdapter(adapter);

    }


    }
  

MyAdapter.java

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Hardik Vats on 20-Jan-17.
 */

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
    Context c;
    String[] phones;
    String[] OperatingSystem;
    //CONSTRUCTOR
    public MyAdapter(Context c, String[] PhoneModel){
        this.c=c;
        this.phones=PhoneModel;
        //this.OperatingSystem=OperatingSystem;
    }
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //INFLATING FROM XML TO JAVA VIEW
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);

        MyHolder holder=new MyHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
holder.PhoneModel.setText(phones[position]);
holder.setItemClickListner(new ItemClickListner() {
    @Override
    public void onItemClick(View v, int position) {
        Intent i=new Intent(c,DetailActivity.class);
        i.putExtra("OS name",OperatingSystem[position]);
    }
});
    }

    @Override
    public int getItemCount() {
        return phones.length;
    }

}

MyHolder.java

   import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

/**
 * Created by Hardik Vats on 20-Jan-17.
 */

public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView PhoneModel;
     ItemClickListner icl;
    public MyHolder(View itemView) {
        super(itemView);
        PhoneModel=(TextView) itemView.findViewById(R.id.MobilePhone);
itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
this.icl.onItemClick(v,getLayoutPosition());
    }
public void setItemClickListner(ItemClickListner itemClickListner){
        this.icl=itemClickListner;
    }
}
  

DetailActivity.java

     import android.content.Intent;

    import android.os.Bundle;

    import android.support.v7.app.AppCompatActivity;

    import android.widget.TextView;
    public class DetailActivity extends AppCompatActivity {
        TextView OperatingSystem;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detail2);
            Intent i=getIntent();
            final String OperatingSystem =i.getExtras().getString("Operating System");


        }
    }
  

ItemClickListner.java(接口)

`import android.view.View;

    /**
     * Created by Hardik Vats on 20-Jan-17.
     */

    public interface ItemClickListner {
        void onItemClick(View v, int position);
    }`

目标是点击手机阵列中的每个列表项,并在新文本字段中的新活动中获取列出操作系统的活动

3 个答案:

答案 0 :(得分:2)

在i.putExtra之后(&#34; OS NAME .... 添加此行

c.startActivity(c,DetailActivity.class);

答案 1 :(得分:1)

正如您所做的那样,为了使更改变小,请尝试以下方法:

holder.PhoneModel.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i=new Intent(c,DetailActivity.class);
        i.putExtra("OS name",OperatingSystem[holder.getAdapterPosition()]);
    }
});

持有人将始终保留您的职位。

答案 2 :(得分:0)

//set on item click listener like this 

mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, "Card at " + position + " is clicked", Toast.LENGTH_SHORT).show();

// here you can start your detail Activity
 Intent i=new Intent(c,DetailActivity.class);
        i.putExtra("OS name",OperatingSystem[position]);
c.startActivity(i);


            }
        }));

// here is the on item click listener class 

public class RecyclerItemClickListener  implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;
    GestureDetector mGestureDetector;


    public interface OnItemClickListener{
        public void onItemClick(View view, int position);
    }


    public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        View childView = rv.findChildViewUnder(e.getX(),e.getY());
        if(childView != null && mListener != null && mGestureDetector.onTouchEvent(e)){
            mListener.onItemClick(childView, rv.getChildAdapterPosition(childView));
            return true;
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }

    @Override`enter code here`
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }