RecyclerView ItemClick to Detail活动无效

时间:2017-07-16 18:42:54

标签: android json android-intent android-recyclerview android-adapter

我已在我的应用中实施了recyclerView。我从存储在assets文件夹中的本地json解析数据。现在我可以在recyclerview中看到解析的数据。我创建了另一个Activity,它将显示此recyclerview的详细活动。看到这个deatil我已经实现了onItemClick监听器方法。但不幸的是这种方法在我的应用程序中无效。对于recyclerview,我不知道如何解决这个问题。

我的ItemClickListener界面是

public interface ItemClickListener {
void onItemClick(int pos);
}

我的Adpater课程

public class MyColleaguesAdapter extends 
RecyclerView.Adapter<MyColleaguesAdapter.ColleagueHolder> {


private List<MyColleageModel> colleagueObject;
private Context context;

public MyColleaguesAdapter(List<MyColleageModel> colleagueObject, Context context) {

    this.colleagueObject = colleagueObject;
    this.context = context;
}

@Override
public ColleagueHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext())
            .inflate(R.layout.colleage_row_layout,parent,false);
    return new ColleagueHolder(view);
}

@Override
public void onBindViewHolder(ColleagueHolder holder, int position) {
    final MyColleageModel myColleague=colleagueObject.get(position);
    //holder.colleaguePicture.setImageResource(myColleague.getImageResourceId());
    holder.mobile.setText(myColleague.getMobile());
    holder.mail.setText(myColleague.getMail());
    holder.name.setText(myColleague.getName());
    holder.company.setText(myColleague.getCompany());
    holder.department.setText(myColleague.getDepartment());
    holder.title.setText(myColleague.getTitle());

    //do not know how to implement it
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (int)
        }
    });
}

@Override
public int getItemCount() {
    return colleagueObject.size();
}
////open activity
private void openDetailActivity(String...details)
{
    Intent i=new Intent(context,DetailMyColleague.class);
    i.putExtra("mobile",details[0]);
    i.putExtra("mail",details[1]);
    i.putExtra("name",details[2]);
    i.putExtra("company",details[3]);
    i.putExtra("department",details[4]);
    i.putExtra("title",details[5]);
    context.startActivity(i);
}


public class ColleagueHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public CardView cardView;
    public ImageView colleaguePicture;
    public TextView mobile;
    public TextView mail;
    public TextView name;
    public TextView company;
    public TextView department;
    public TextView title;
    ItemClickListener itemClickListener;

     public ColleagueHolder(View itemView) {
        super(itemView);
        //colleaguePicture=(ImageView)itemView.findViewById(R.drawable.profile_image);
        mobile=(TextView)itemView.findViewById(R.id.colleague_mobile);
        mail=(TextView) itemView.findViewById(R.id.colleague_mail);
        name=(TextView) itemView.findViewById(R.id.colleague_name);
        company=(TextView)itemView.findViewById(R.id.company_name);
        department=(TextView) itemView.findViewById(R.id.department_name);
        title=(TextView) itemView.findViewById(R.id.job_title);
        cardView=(CardView)itemView.findViewById(R.id.cvSingleUser);
    }


    @Override
    public void onClick(View v) {
        this.itemClickListener.onItemClick(this.getLayoutPosition());
    }
    public void setItemClickListener(ItemClickListener itemClickListener)
    {
        this.itemClickListener=itemClickListener;
     }
   }

 }

我的主要活动类是

public class MyColleaguesPage extends AppCompatActivity{

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<MyColleageModel> colleagueObject;
InputStream is;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mycolleagues_layout);
    recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MyColleaguesPage.this);
    recyclerView.setLayoutManager(layoutManager);
    new JSONTask().execute();

}



    public class JSONTask extends AsyncTask<String, String, List<MyColleageModel>> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected List<MyColleageModel> doInBackground(String... params) {

        colleagueObject = new ArrayList<MyColleageModel>();

        try {

            JSONObject obj = new JSONObject(loadJSONFromAsset());
            JSONArray m_jArry = obj.getJSONArray("contacts");
            MyColleageModel my_clg;

            for (int i = 0; i < m_jArry.length(); i++) {
                JSONObject jo_inside = m_jArry.getJSONObject(i);
                //my_clg=new MyColleageModel();
                String val1 = jo_inside.getString("mobile");
                String val2 = jo_inside.getString("mail");
                String val3 = jo_inside.getString("name");
                String val4 = jo_inside.optString("company");
                String val5 = jo_inside.optString("department");
                String val6 = jo_inside.optString("title");
                my_clg = new MyColleageModel(val1, val2, val3, val4, val5, val6);
                colleagueObject.add(my_clg);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    protected void onPostExecute(final List<MyColleageModel> result) {
        super.onPostExecute(result);

            adapter = new MyColleaguesAdapter(colleagueObject, getApplicationContext());
            recyclerView.setAdapter(adapter);


    }
 }

   private String loadJSONFromAsset() {
    String json = null;
    try {

        is = getAssets().open("colleagues.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");


    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

   }
}

我的详细活动类是

public class DetailMyColleague extends AppCompatActivity {


private TextView jsonName;
private TextView jsonRole;
private TextView jsonCompany;
private TextView jsonMobile;
private TextView jsonEmail;
private TextView jsonDepartment;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.colleague_detail);

    // Showing and Enabling clicks on the Home/Up button
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    // setting up text views and stuff
    setUpUIViews();

    Intent intent = getIntent();

    //RECEIVE DATA
    String mobil=intent.getExtras().getString("mobile");
    String mail=intent.getExtras().getString("mail");
    String name=intent.getExtras().getString("name");
    String company=intent.getExtras().getString("company");
    String dept=intent.getExtras().getString("department");
    String title=intent.getExtras().getString("title");
    //BIND DATA
    jsonMobile.setText(mobil);
    jsonEmail.setText(mail);
    jsonName.setText(name);
    jsonCompany.setText(company);
    jsonDepartment.setText(dept);
    jsonRole.setText(title);

}

private void setUpUIViews() {

    jsonName=(TextView)findViewById(R.id.json_name);
    jsonRole=(TextView)findViewById(R.id.json_role);
    jsonCompany=(TextView)findViewById(R.id.json_company);
    jsonMobile=(TextView)findViewById(R.id.json_mobile);
    jsonEmail=(TextView)findViewById(R.id.json_email);
    jsonDepartment=(TextView)findViewById(R.id.json_department);
}

}

2 个答案:

答案 0 :(得分:0)

您可以简单地将OnClickListener设置为持有者的父布局,即RelativeLayout或LinearLayout等。

holder.relative_layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDetailActivity(myColleague.getMobile(),myColleague.getMail(), myColleague.getName(),
                myColleague.getCompany(), myColleague.getDepartment(), myColleague.getTitle());

        }
    });

答案 1 :(得分:0)

首先,您没有在任何View上设置OnClickListener,因此,永远不会触发onClick()方法,因此不会发送回onItemClickListener的回调。

其次,您不需要为此实现接口,您只需在onBindViewHolder()方法中设置onClickListener就像这样:

holder.cardView.setTag(position);

holder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int pos = (int)view.getTag()
    openDetailActivity(myColleague.getMobile(),myColleague.getMail(), myColleague.getName(),
            myColleague.getCompany(), myColleague.getDepartment(), myColleague.getTitle());

    }
});

这里使用setTag()方法将当前位置发送到onClick()方法,以引用被点击的视图。

在ViewHolder类中编写此行以获取您的案例中的CardView引用。

cardView=(CardView) itemView.findViewById(R.id.cardview_id);