片段更改和在RecyclerView中选择CardView的位置

时间:2018-09-26 19:40:08

标签: android android-fragments android-recyclerview

*简单的部分*

目前,我已经在RecyclerView中设置了CardView。

我需要做的是将第二张卡片视图的颜色更改为黄色,而第三张卡片视图的颜色更改为红色(此刻它们均为绿色)。我也希望第一张卡片视图保持绿色。

*硬部件*

每当我单击RecyclerView中的CardView之一时,我还需要能够切换到另一个片段。所以基本上是在我的RecyclerAdapter中切换片段。

我的RecyclerAdapter Java类如下:

谢谢!

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

Fragment fragment;

private static String[] title = new String[]{"D42DB2", "B42DC6", "CURRENTLY NOT AVAILABLE"};

private static String[] beskrivelse = new String[]{"Temperatur & Humdity Sensorer", "Light Sensorer", ""};


@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    System.out.println( "DU ER KOMMET SÅ LANGT HABEBEEEEEEEEE" );

    View view = LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.fragment_card_view_tabel, viewGroup, false );

    viewGroup.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View view) {

        }
    } );

    return new RecyclerViewHolder( view );
}

@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int i) {

    recyclerViewHolder.mBeskrivelse.setText( beskrivelse[i] );
    recyclerViewHolder.mTitle.setText( title[i] );

}

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

class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView mBeskrivelse;
    private TextView mTitle;
    private CardView cardView;

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

        mBeskrivelse = (TextView) itemView.findViewById( R.id.item_beskrivelse );
        mTitle = (TextView) itemView.findViewById( R.id.item_title );
        cardView = (CardView) itemView.findViewById( R.id.card_view );
        itemView.setOnClickListener( this );

    }

    @Override
    public void onClick(View v) {

        Toast.makeText( itemView.getContext(), "HEJ DU HAR TRYKKET PÅ KNAP", Toast.LENGTH_LONG ).show();

    }

  }

}

0 个答案:

没有答案