-1

I have managed to create a tabbed activity using recycle view and have made a list-view in each fragment, now I have the lists I need to make the cards in the fragment clickable and create an intent to start a new activity. I have tried on-click method and onListItemClick even though I knew that would not work.

Basically I need to create one more layer of lists for each fragment which will lead to a list of actions (webviews, Maps, and information).

Example Code is below

ublic class LOneFragment extends Fragment {

public LOneFragment() {
    // Required empty public constructor
}

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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

    RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
    rv.setHasFixedSize(true);
    MyAdapter adapter = new MyAdapter(new String[]{"League One News", "AFC Wimbledon", "Blackburn Rovers", "Blackpool", "Bradford City", "Bristol Rovers", "Bury", "Charlton Athletic", "Doncaster Rovers", "Fleetwood Town",
            "Gillingham", "Milton Keynes Dons", "Northampton Town", "Oldham Athletic", "Oxford United", "Peterborough United", "Plymouth Argyle", "Portsmouth", "Rochdale", "Rotherham United",
            "Scunthorpe United", "Shrewsbury Town", "Southend United", "Walsall", "Wigan Athletic"});
    rv.setAdapter(adapter);

    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    rv.setLayoutManager(llm);

    return rootView;
}

public void onListItemClick(ListView l, View v, int position, long id) {
    if (position == 3) {
        Intent intent = new Intent(getContext(), Bradford.class);
        startActivity(intent);
    }

My adaptor class

public class MyAdapter extends RecyclerView.Adapter {

private String[] mDataset;

public static class MyViewHolder extends RecyclerView.ViewHolder{
    public CardView mCardView;
    public TextView mTextView;
    public MyViewHolder(View v){
        super(v);

        mCardView = (CardView) v.findViewById(R.id.card_view);
        mTextView = (TextView) v.findViewById(R.id.tv_text);

    }

}

public MyAdapter(String[] myDataset){
    mDataset = myDataset;
}

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position){
    holder.mTextView.setText(mDataset[position]);
}

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

}

JonniBravo
  • 1,051
  • 1
  • 11
  • 26

1 Answers1

0

This works perfect for me.

1) Create method in viewholder class which will take position, get array item from position and use the way you like.

2) Access it in onBindViewHolder method with itemView instance which is the full row at given position,

public class MyAdapter extends RecyclerView.Adapter {

     private String[] mDataset;


public MyAdapter(String[] myDataset){
mDataset = myDataset;
}

 @Override
 public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position){
    holder.mTextView.setText(mDataset[position]);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.bindCliks(groupItem);
            }
        });

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



public static class MyViewHolder extends RecyclerView.ViewHolder{
     public CardView mCardView;
     public TextView mTextView;

     public MyViewHolder(View v){
          super(v);

          mCardView = (CardView) v.findViewById(R.id.card_view);
          mTextView = (TextView) v.findViewById(R.id.tv_text);
     }


    public void bindCliks(int position) {
        Intent intent = new Intent(context, 
            OtherActivity.class);
       //use number here to get your string item and use it to other activity.
        context.startActivity(intent);
    }
}
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31