Android - 按名称选择卡

时间:2017-02-22 14:50:40

标签: android android-layout android-cardview

我创建了一个CardViews列表,其中我使用TextView为每个列表添加了一个名称。我的问题是我找不到根据TextView中的名称选择和点击特定卡片的方法。这是我到目前为止所做的:

HomeAcitivity

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

 //cards
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    serviceList = new ArrayList<>();
    adapter = new ServicesAdapter(this, serviceList);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);
    prepareServices();
}

private void prepareServices() {
    Service  a = new Service("Service 1");
    serviceList.add(a);

    a = new Service("Service 2");
    serviceList.add(a);

    a = new Service("Service 3");
    serviceList.add(a);

    a = new Service("Service 4");
    serviceList.add(a);

    a = new Service("Service 5");
    serviceList.add(a);

    a = new Service(ADD_NEW_ITEM_TYPE);
    serviceList.add(a);

    adapter.notifyDataSetChanged();
}

   /**
 * RecyclerView item decoration - give equal margin around grid item
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }
}

private int dpToPx(int dp) {
    Resources r = getResources();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}

ServiceAdapter

 private Context mContext;
private List<Service> serviceList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        if(title.getText().toString().equals(ADD_NEW_ITEM_TYPE)) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent redirect= new Intent(v.getContext(), ListServices.class);
                    v.getContext().startActivity(redirect);
                }
            });//end onclick listener
        }


    }


}


public ServicesAdapter(Context mContext, List<Service> serviceList) {
    this.mContext = mContext;
    this.serviceList = serviceList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.service_card, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    Service serv = serviceList.get(position);
    holder.title.setText(serv.getName());

}


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

1 个答案:

答案 0 :(得分:1)

由于您需要在每行中使用ADD_NEW_ITEM_TYPE检查标题值,因此您必须在onBindViewHolder()中移动检查代码,以便在viewHolder中创建onBind方法并从{{{}调用它1}}

编辑:我完全重写了ServicesAdapter类:

onBindViewHolder()