I am writing a app which use a recycleView in fragement. However only the textView in that fragement show,there is nothing in the recycleView ,function like onBindViewHolder() in the recycleView Adapter wasn't been called .Here is my code of recycleViewAdapter and fragement.
Adapter
import java.util.List;
public class ShoppingAdapter extends RecyclerView.Adapter<ShoppingAdapter.MyViewHolder> {
private List<Shopping> localData;
public ShoppingAdapter(List<Shopping> localData) {
this.localData = localData;
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
ImageView imageView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
textView =itemView.findViewById(R.id.shopping_text);
imageView = itemView.findViewById(R.id.shopping_img);
}
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d("test", "onCreateViewHolder: ");
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_item,parent,false);
return null;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.textView.setText(localData.get(position).getTitle());
holder.imageView.setImageResource(localData.get(position).getImageResource());
}
#Override
public int getItemCount() {
return this.localData.size();
}
}
Fragment
public class shoppingFragment extends Fragment {
View view;
List<Shopping> shoppingList;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initShoppingList();
RecyclerView recyclerView =(RecyclerView) view.findViewById(R.id.shopping_recycle_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
ShoppingAdapter myAdapter=new ShoppingAdapter(shoppingList);
recyclerView.setAdapter(myAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_shopping, container, false);
return view;
}
public void initShoppingList(){
shoppingList = new ArrayList<Shopping>();
shoppingList.add(new Shopping(R.drawable.data,"data"));
shoppingList.add(new Shopping(R.drawable.position,"position"));
shoppingList.add(new Shopping(R.drawable.floor,"floor"));
}
}
Looks like you are missing 2 crucial things.
onCreateViewHolder should return a view. You return null.
return new MyViewHolder(view);
You forgot to set the LayoutManager to your adapter.
recyclerView.setLayoutManager(linearLayoutManager);
Instead of returning null at onCreateViewHolder, you should return new MyViewHolder(view);
And you forgot to assign the layoutmanager you created on your fragment into your recyclerview (recyclerView.setLayoutManager(linearLayoutManager);)
Also, you don't need to cast recyclerview to recyclerview. You can remove (RecyclerView) from RecyclerView recyclerView =(RecyclerView) view.findViewById(R.id.shopping_recycle_view);
Related
I've got a Fragment, which has a RecyclerView. This RecyclerView has a header adapter, which contains an EditText.
When the Fragment comes into view, I want the EditText to auto focus (i.e. show the keyboard automatically).
How would I do this?
The Fragment:
public class My Fragment extends Fragment {
private LinearLayoutManager layoutManager;
private FeedRecyclerView recyclerView;
private PostAdapter postAdapter;
private ConcatAdapter concatAdapter;
private HeaderAdapter headerAdapter;
private List<Post> postData = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_fragment, parent, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
recyclerView = view.findViewById(R.id.recycler_view);
layoutManager = new FeedLinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
// This is the adapter that contains the EditText
headerAdapter = new HeaderAdapter(getContext());
postAdapter = new PostAdapter(getContext(), postData, layoutManager);
concatAdapter = new ConcatAdapter();
recyclerView.setAdapter(concatAdapter);
concatAdapter.addAdapter(searchHeaderAdapter);
}
}
The adapter class, HeaderAdapter:
public class HeaderAdapter extends RecyclerView.Adapter<HeaderAdapter.ViewHolder> {
private Context context;
public HeaderAdapter(Context context) {
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.my_header, parent, false));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//
}
#Override
public long getItemId(int position) {
return 1;
}
#Override
public int getItemCount() {
return 1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public EditText myEditText;
public ViewHolder(View v) {
super(v);
myEditText = v.findViewById(R.id.my_edit_text);
}
}
}
As you can see, my RecyclerView's adapter, HeaderAdapter, has the EditText. When the Fragment gets shown, I want to auto focus this EditText.
How would I do this?
Thanks!
Add this line myEditText.requestFocus() into init in Header ViewHolder
init {
myEditText = v.findViewById(R.id.my_edit_text)
myEditText.requestFocus()
}
try this
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.myEditText.requestFocus();
((InputMethodManager)holder.itemView.context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(holder.myEditText, 0);
}
I am trying uamp sample, whenever orientation changes MediaBrowserFragment recyclerview state is lost(its scroll position is not maintained).
My problem is how to maintain scroll position in uamp sample.
I know I can save scroll position and restore it later but since recyclerView itself maintains scroll state I don't want that hacks.
I tried pushing this simple fragment instead of MediaBrowserFragment and its maintaining its scroll
public class TestFragment extends Fragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mRootView = inflater.inflate(R.layout.fragment_test, container, false);
RecyclerView recyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(new RecAdapter());
return mRootView;
}
private class RecAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_text, null);
return new ItemViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
((ItemViewHolder) holder).title.setText(""+ position);
}
#Override
public int getItemCount() {
return 200;
}
}
private class ItemViewHolder extends RecyclerView.ViewHolder {
TextView title;
ItemViewHolder(View itemLayoutView) {
super(itemLayoutView);
title = (TextView) itemLayoutView.findViewById(R.id.title);
}
}
}
Update
I raised the issue with uamp
https://github.com/googlesamples/android-UniversalMusicPlayer/issues/217
you can just add configChanges in your manifest file as below:
<activity android:name=".YourActivity"
android:configChanges="orientation|screenSize"/>
I want to grey out fragment activity on some condition, I am trying to set a flag in pojo class and checking every time i try to grey out an activity.
I do not know where exactly the logic should be placed. I tried to put it inside a onCreateView() method, but it gave me an null pointer exception.
Please find the code below for your reference.
#SuppressLint("ValidFragment")
public class QuizTab extends Fragment {
private static final String TAG = "QuizTab";
RecyclerView recyclerView;
View view;
LinearLayout linearLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.quiztab, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
if(flag){
grey out logic ---- not working
}
getQuizContent();
setupRecyclerView(recyclerView);
return view;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity()));
}
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder {
public final LinearLayout mLinearlayout;
public ViewHolder(View view) {
super(view);
mLinearlayout = (LinearLayout) view.findViewById(R.id.mcq);
}
}
public SimpleStringRecyclerViewAdapter(Context context) {
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quiztab_mcq, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
}
#Override
public int getItemCount() {
return 1;
}
}
}
LinearLayout in this case is parent of fragment i tryed to paint fragment but failure so i did something like that
if(flag){
LinearLayout layout = view.findViewById(R.id.parentOffragment);
layout.setBackgroundColor(Color.GRAY);
}
When I try Recyclerview With Fragments its getting error Fragments With RecyclerView not working. Its getting Error No layout manager attached. Skipping entry.
My FrgamentList.java
public class FragmentList extends Fragment {
String [] names={"Nitin","Manish","Ankit","Hardik","Mayur","Mayank"};
RecyclerView mRecyclerView;
Context context;
RecyclerAdapter mAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_list,container,false);
mRecyclerView= (RecyclerView)v.findViewById(R.id.recycler_nit);
LinearLayoutManager mLayout= new LinearLayoutManager(getActivity());
mLayout.setOrientation(LinearLayoutManager.VERTICAL);
mAdapter= new RecyclerAdapter(getActivity(),names);
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
RecyclerAdapeter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context context;
String[] names;
public RecyclerAdapter(Context context, String[] names) {
this.context = context;
this.names = names;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_list,parent,false);
vh=new ViewHolder(v);
return (ViewHolder) vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.txt.setText(names[position]);
}
#Override
public int getItemCount() {
return names.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txt;
public ViewHolder(View itemView) {
super(itemView);
txt= (TextView) itemView.findViewById(R.id.name_listtext);
}
}
}
You should set your created layout manager to RecyclerView
mRecyclerView= (RecyclerView)v.findViewById(R.id.recycler_nit);
LinearLayoutManager mLayout= new LinearLayoutManager(getActivity());
mLayout.setOrientation(LinearLayoutManager.VERTICAL);
//add this line
mRecyclerView.setLayoutManager(mLayout);
You need to set a LayoutManager to recyclerView like this:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
or
mRecyclerView.setLayoutManager(mLayout);
Here is my adapter class
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
List<CategoryItem> mItems;
public CategoryAdapter(Context context) {
super();
Resources res = context.getResources();
mItems = new ArrayList<>();
mItems.add(new CategoryItem(res.getString(R.string.category_book), R.drawable.books));
// ... few items
mItems.add(new CategoryItem(res.getString(R.string.category_clothing), R.drawable.clothes));
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_category_selection, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
CategoryItem category = mItems.get(i);
viewHolder.title.setText(category.getTitle());
viewHolder.thumbnail.setImageResource(category.getThumbnail());
}
#Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView thumbnail;
public CardView card;
public ViewHolder(View itemView){
super(itemView);
card = (CardView)itemView.findViewById(R.id.card_view);
title = (TextView)itemView.findViewById(R.id.category_title);
thumbnail = (ImageView)itemView.findViewById(R.id.category_thumbnail);
}
}
}
My fragment
public class CategorySelectionFragment extends Fragment {
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
public CategorySelectionFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_category_selection, container, false);
mRecyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);
setUpGrid();
return v;
}
private void setUpGrid() {
mRecyclerView.setHasFixedSize(true);
colNum = 3;
mLayoutManager = new GridLayoutManager(getActivity(), colNum);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CategoryAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
}
When fragment just created (onCreateView called) recyclerview works perfectly, but when app goes to background and then restore scroll start to freeze on the first few items and then continue to work smoothly.
When app restore from background onCreateView method is not called so I try to set up adapter for recyclerview in onStart(), like this:
#Override
public void onStart(){
super.onStart();
// Adapter setting here to avoid freezes, when start to scroll after switching to an app from background.
mRecyclerView.setAdapter(mAdapter);
}
This approach has solved the problem, but it seems like not realy clear way to solve that issue. Since for example, restoring the fragment leads to reset last scrolled position.
Thanks for your help!