How can I use recyclerview for two different fragments with different layout
I cant seem to get the proper way to use the same recyclerview adapter with different layout .
since I'm trying to use different layout for different fragment I tried to make another adapter with the same methods .. and it actually worked but there is lag ( late response ) and I'm sure there is a way but I don't even know how to make it …
here is my recyclerAdapter class
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<featuresItemList> mfeaturesArray;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CircleImageView mImageAvater;
TextView mTextViewAvater;
public ViewHolder(View itemView){
super(itemView);
context = itemView.getContext();
mImageAvater = itemView.findViewById(R.id.avatar_picture);
mTextViewAvater = itemView.findViewById(R.id.avatar_text);
itemView.setClickable(true);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Intent intent;
switch (getPosition()) {
case 0:
intent = new Intent(context, Activity1.class);
break;
case 1:
intent = new Intent(context, Activity2.class);
break;
case 2:
intent = new Intent(context, Activity3.class);
break;
case 3:
intent = new Intent(context, Activity4.class);
break;
case 4:
intent = new Intent(context, Activity5.class);
break;
default:
intent = new Intent(context,something.class);
break;
}
context.startActivity(intent);
}
}
public recyclerAdapter(Context context, ArrayList<featuresItemList> featuresArray){
this.context = context;
inflater = LayoutInflater.from(context);
mfeaturesArray = featuresArray ;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.features_list_layout , parent , false );
ViewHolder holder = new ViewHolder (view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
featuresItemList currentItem = mfeaturesArray.get(position);
holder.mImageAvater.setImageResource(currentItem.getImageResource());
holder.mTextViewAvater.setText(currentItem.getImageText());
}
@Override
public int getItemCount() {
return mfeaturesArray.size();
} }
and here is the fragment am using right now ..
public class FragmentThree extends Fragment {
private RecyclerView.Adapter mAdapter;
//
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<featuresItemList> featuresitems ;
public FragmentThree() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
featuresitems = new ArrayList<>();
featuresitems.add(new featuresItemList(R.drawable.ic , "one"));
featuresitems.add(new featuresItemList(R.drawable.ic_ , "two"));
featuresitems.add(new featuresItemList(R.drawable.ic , "three "));
featuresitems.add(new featuresItemList(R.drawable.ic , "four"));
featuresitems.add(new featuresItemList(R.drawable.ic_, "five"));
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.myrecyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager( requireContext() );
// ToDo made a change here
mAdapter = new recyclerAdapter(getContext(), featuresitems);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view ; } }
basically the code above helps me build a list and each item I click on takes me to new activity as planned ..
and I do want to do the same with the second fragment I made, using the same code . each item with the new layout should also open new activity...
so how can that be done ?
Note : excuse my poor coding skill , just working my graduation project .
java android android-recyclerview
add a comment |
I cant seem to get the proper way to use the same recyclerview adapter with different layout .
since I'm trying to use different layout for different fragment I tried to make another adapter with the same methods .. and it actually worked but there is lag ( late response ) and I'm sure there is a way but I don't even know how to make it …
here is my recyclerAdapter class
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<featuresItemList> mfeaturesArray;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CircleImageView mImageAvater;
TextView mTextViewAvater;
public ViewHolder(View itemView){
super(itemView);
context = itemView.getContext();
mImageAvater = itemView.findViewById(R.id.avatar_picture);
mTextViewAvater = itemView.findViewById(R.id.avatar_text);
itemView.setClickable(true);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Intent intent;
switch (getPosition()) {
case 0:
intent = new Intent(context, Activity1.class);
break;
case 1:
intent = new Intent(context, Activity2.class);
break;
case 2:
intent = new Intent(context, Activity3.class);
break;
case 3:
intent = new Intent(context, Activity4.class);
break;
case 4:
intent = new Intent(context, Activity5.class);
break;
default:
intent = new Intent(context,something.class);
break;
}
context.startActivity(intent);
}
}
public recyclerAdapter(Context context, ArrayList<featuresItemList> featuresArray){
this.context = context;
inflater = LayoutInflater.from(context);
mfeaturesArray = featuresArray ;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.features_list_layout , parent , false );
ViewHolder holder = new ViewHolder (view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
featuresItemList currentItem = mfeaturesArray.get(position);
holder.mImageAvater.setImageResource(currentItem.getImageResource());
holder.mTextViewAvater.setText(currentItem.getImageText());
}
@Override
public int getItemCount() {
return mfeaturesArray.size();
} }
and here is the fragment am using right now ..
public class FragmentThree extends Fragment {
private RecyclerView.Adapter mAdapter;
//
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<featuresItemList> featuresitems ;
public FragmentThree() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
featuresitems = new ArrayList<>();
featuresitems.add(new featuresItemList(R.drawable.ic , "one"));
featuresitems.add(new featuresItemList(R.drawable.ic_ , "two"));
featuresitems.add(new featuresItemList(R.drawable.ic , "three "));
featuresitems.add(new featuresItemList(R.drawable.ic , "four"));
featuresitems.add(new featuresItemList(R.drawable.ic_, "five"));
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.myrecyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager( requireContext() );
// ToDo made a change here
mAdapter = new recyclerAdapter(getContext(), featuresitems);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view ; } }
basically the code above helps me build a list and each item I click on takes me to new activity as planned ..
and I do want to do the same with the second fragment I made, using the same code . each item with the new layout should also open new activity...
so how can that be done ?
Note : excuse my poor coding skill , just working my graduation project .
java android android-recyclerview
add a comment |
I cant seem to get the proper way to use the same recyclerview adapter with different layout .
since I'm trying to use different layout for different fragment I tried to make another adapter with the same methods .. and it actually worked but there is lag ( late response ) and I'm sure there is a way but I don't even know how to make it …
here is my recyclerAdapter class
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<featuresItemList> mfeaturesArray;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CircleImageView mImageAvater;
TextView mTextViewAvater;
public ViewHolder(View itemView){
super(itemView);
context = itemView.getContext();
mImageAvater = itemView.findViewById(R.id.avatar_picture);
mTextViewAvater = itemView.findViewById(R.id.avatar_text);
itemView.setClickable(true);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Intent intent;
switch (getPosition()) {
case 0:
intent = new Intent(context, Activity1.class);
break;
case 1:
intent = new Intent(context, Activity2.class);
break;
case 2:
intent = new Intent(context, Activity3.class);
break;
case 3:
intent = new Intent(context, Activity4.class);
break;
case 4:
intent = new Intent(context, Activity5.class);
break;
default:
intent = new Intent(context,something.class);
break;
}
context.startActivity(intent);
}
}
public recyclerAdapter(Context context, ArrayList<featuresItemList> featuresArray){
this.context = context;
inflater = LayoutInflater.from(context);
mfeaturesArray = featuresArray ;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.features_list_layout , parent , false );
ViewHolder holder = new ViewHolder (view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
featuresItemList currentItem = mfeaturesArray.get(position);
holder.mImageAvater.setImageResource(currentItem.getImageResource());
holder.mTextViewAvater.setText(currentItem.getImageText());
}
@Override
public int getItemCount() {
return mfeaturesArray.size();
} }
and here is the fragment am using right now ..
public class FragmentThree extends Fragment {
private RecyclerView.Adapter mAdapter;
//
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<featuresItemList> featuresitems ;
public FragmentThree() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
featuresitems = new ArrayList<>();
featuresitems.add(new featuresItemList(R.drawable.ic , "one"));
featuresitems.add(new featuresItemList(R.drawable.ic_ , "two"));
featuresitems.add(new featuresItemList(R.drawable.ic , "three "));
featuresitems.add(new featuresItemList(R.drawable.ic , "four"));
featuresitems.add(new featuresItemList(R.drawable.ic_, "five"));
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.myrecyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager( requireContext() );
// ToDo made a change here
mAdapter = new recyclerAdapter(getContext(), featuresitems);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view ; } }
basically the code above helps me build a list and each item I click on takes me to new activity as planned ..
and I do want to do the same with the second fragment I made, using the same code . each item with the new layout should also open new activity...
so how can that be done ?
Note : excuse my poor coding skill , just working my graduation project .
java android android-recyclerview
I cant seem to get the proper way to use the same recyclerview adapter with different layout .
since I'm trying to use different layout for different fragment I tried to make another adapter with the same methods .. and it actually worked but there is lag ( late response ) and I'm sure there is a way but I don't even know how to make it …
here is my recyclerAdapter class
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<featuresItemList> mfeaturesArray;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CircleImageView mImageAvater;
TextView mTextViewAvater;
public ViewHolder(View itemView){
super(itemView);
context = itemView.getContext();
mImageAvater = itemView.findViewById(R.id.avatar_picture);
mTextViewAvater = itemView.findViewById(R.id.avatar_text);
itemView.setClickable(true);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Intent intent;
switch (getPosition()) {
case 0:
intent = new Intent(context, Activity1.class);
break;
case 1:
intent = new Intent(context, Activity2.class);
break;
case 2:
intent = new Intent(context, Activity3.class);
break;
case 3:
intent = new Intent(context, Activity4.class);
break;
case 4:
intent = new Intent(context, Activity5.class);
break;
default:
intent = new Intent(context,something.class);
break;
}
context.startActivity(intent);
}
}
public recyclerAdapter(Context context, ArrayList<featuresItemList> featuresArray){
this.context = context;
inflater = LayoutInflater.from(context);
mfeaturesArray = featuresArray ;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.features_list_layout , parent , false );
ViewHolder holder = new ViewHolder (view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
featuresItemList currentItem = mfeaturesArray.get(position);
holder.mImageAvater.setImageResource(currentItem.getImageResource());
holder.mTextViewAvater.setText(currentItem.getImageText());
}
@Override
public int getItemCount() {
return mfeaturesArray.size();
} }
and here is the fragment am using right now ..
public class FragmentThree extends Fragment {
private RecyclerView.Adapter mAdapter;
//
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<featuresItemList> featuresitems ;
public FragmentThree() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
featuresitems = new ArrayList<>();
featuresitems.add(new featuresItemList(R.drawable.ic , "one"));
featuresitems.add(new featuresItemList(R.drawable.ic_ , "two"));
featuresitems.add(new featuresItemList(R.drawable.ic , "three "));
featuresitems.add(new featuresItemList(R.drawable.ic , "four"));
featuresitems.add(new featuresItemList(R.drawable.ic_, "five"));
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.myrecyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager( requireContext() );
// ToDo made a change here
mAdapter = new recyclerAdapter(getContext(), featuresitems);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return view ; } }
basically the code above helps me build a list and each item I click on takes me to new activity as planned ..
and I do want to do the same with the second fragment I made, using the same code . each item with the new layout should also open new activity...
so how can that be done ?
Note : excuse my poor coding skill , just working my graduation project .
java android android-recyclerview
java android android-recyclerview
edited Jan 3 at 16:37
Ahmad Dalo'o
asked Jan 3 at 16:01
Ahmad Dalo'oAhmad Dalo'o
916
916
add a comment |
add a comment |
0
active
oldest
votes
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025824%2fhow-can-i-use-recyclerview-for-two-different-fragments-with-different-layout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025824%2fhow-can-i-use-recyclerview-for-two-different-fragments-with-different-layout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown