Select position of CardView in RecyclerView and Change Fragment in RecyclerView [closed]

微笑、不失礼 提交于 2019-12-13 09:20:24

问题


* EASY PART *

At the moment I have set up a CardView inside a RecyclerView.

What i need to do is to change the color of the 2nd cardview to yellow, and the 3rd cardview to red (At the moment they are all green). Also i want the first cardview to remain green.

* HARD PART *

What i also need, is to be able to switch to another fragment, whenever i click on one of the CardViews in the RecyclerView. So basically to switch fragments in my RecyclerAdapter.

My RecyclerAdapter Java class is as follows:

Thanks in advance!`

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

Fragment fragment;

private DataTabelFragment dataTabelFragment;

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

    recyclerViewHolder.cardView.setCardBackgroundColor( Color.parseColor( "#FFCE54" ) );


    //recyclerViewHolder.cardView.setCardBackgroundColor( Color.parseColor( "#FFCE54" ) );

}

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

        //((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.recycler_view, new DataTabelFragment() ).commit();


    }

  }

}

回答1:


For the first part, you can just do the same thing you do with the titles. Add an array similar to this.

private static String[] colors = new String[]{"#FFCE54", "#57ad48", "#c93e3c"};

Then access it like this.

recyclerViewHolder.cardView.setCardBackgroundColor( Color.parseColor( colors[i] ) );

You could also set the onClickListener inside the onBindViewHolder like this.

recyclerViewHolder.setOnClickListener(new View.OnClickListener() {       
    @Override
    public void onClick(View view) {
        getActivity().getSupportFragmentManager().beginTransaction().replace( R.id.recycler_view, new DataTabelFragment() ).commit();
    }
});

Or add it as an onItemTouchListener to your RecyclerView.

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
          getActivity().getSupportFragmentManager().beginTransaction().replace( R.id.recycler_view, new DataTabelFragment() ).commit();
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });



回答2:


A short clarify :

  1. If you want to change only the first 3 cardviewcolors then you can simply check the position and do the job. But I suppose you want this to be a bit more customized, therefore you'll have to @Override getItemViewtypemethod of the adapter, and based on that you can show different views in the `Recyclerview. Sample

  2. Now, about the fragment you want to show from each cardview.. I see you are trying to do that from within the adapter. I suggest you do it from activity / fragment [RecyclerView Listener] (https://antonioleiva.com/recyclerview-listener/) properly implemented. In addition to this, you'll want to use add() method instead of replace for fragment transaction (this way you will have your list still shown below the added fragment, and you won't have to inflate or start the fragment again.



来源:https://stackoverflow.com/questions/52531834/select-position-of-cardview-in-recyclerview-and-change-fragment-in-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!