I'm in the process of learning android app development however I have come to an impasse. My RecyclerView is picking up how many items are in my String array however it is not showing the actual data in text1, any help would be much appreciated.
This is my adapter
public class EventCalenderAdapter extends RecyclerView.Adapter<EventCalenderAdapter.ViewHolder> {
static String[] fakeData = new String[] {
"One","Two","Three","Four","Five","Ah..Ah..Ah"
};
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.titleView.setText(fakeData[i]);
}
#Override
public int getItemCount() {
return fakeData.length;
}
This is the corresponding XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:event_view="http://schemas.android.com/apk/res-auto"
android:layout_height="100dp"
android:layout_width="match_parent"
android:id="#+id/event_view"
android:layout_marginTop="#dimen/task_card_half_spacing"
android:layout_marginBottom="#dimen/task_card_half_spacing"
android:layout_marginLeft="#dimen/gutter"
android:layout_marginRight="#dimen/gutter"
android:layout_gravity="center"
android:elevation="#dimen/task_card_elevation"
android:foreground="?android:attr/selectableItemBackground"
event_view:cardCornerRadius="0dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium.Inverse"
android:id="#+id/text1"
android:maxLines="1"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignTop="#+id/image"
android:layout_toRightOf="#+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#android:style/TextAppearance.Inverse"
android:id="#+id/text2"
android:maxLines="3"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignStart="#+id/text1"
android:layout_below="#+id/text1"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is my fragment
public class EventCalenderFragment extends Fragment {
RecyclerView recyclerView;
EventCalenderAdapter adapter;
public EventCalenderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new EventCalenderAdapter();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_event_calender, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
Hm... everything looks ok, can you try setting this to it:
android:textColor="#000000"
Related
The view inside recycler view is not aligned properly. The items inside recyclerview are having different height on adding new item.
I tried of using display metrics but could not achieve it. My item list can be changed by adding or deleting items but the view padding, width and height must h=not get disturbed.
categoryRecyclerView = findViewById(R.id.categoriesRecyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1,GridLayoutManager.HORIZONTAL, false);
categoryRecyclerView.setLayoutManager(gridLayoutManager);
categoryRecyclerView.addItemDecoration(new ItemDecorator(getResources().getDimensionPixelSize(R.dimen.item_spacing), 3));
CategoryItemAdapter categoryItemAdapter = new CategoryItemAdapter(this, categoryList);
categoryRecyclerView.setAdapter(categoryItemAdapter);
Code for Adapter
public class CategoryItemAdapter extends RecyclerView.Adapter<CategoryItemAdapter.MyViewHolder> {
private Context context;
private List<Category> categoryList;
private onItemClickListener mItemClickListener;
public CategoryItemAdapter(Context context, List<Category> categoryList) {
this.context = context;
this.categoryList = categoryList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryItemAdapter.MyViewHolder holder, int position) {
Category category = (Category) categoryList.get(position);
holder.textView.setText(category.getCategoryName());
}
#Override
public int getItemCount() {
return categoryList.size();
}
public void setOnItemClickListener(onItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public interface onItemClickListener {
void onItemClickListener(View view, int position, Category category);
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cardview;
TextView textView;
ImageView selectItem;
RelativeLayout selectItemLayout;
LinearLayout linearLayout;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
cardview = itemView.findViewById(R.id.cardview);
textView = itemView.findViewById(R.id.categoryName);
linearLayout = itemView.findViewById(R.id.llbackground);
selectItemLayout = itemView.findViewById(R.id.selectItemLayout);
selectItem = itemView.findViewById(R.id.selectItem);
selectItemLayout.setVisibility(authenticationHelper.isAdmin() ? View.VISIBLE : View.GONE);
itemView.setOnClickListener(this);
Util.setRandomGradientColor(linearLayout, GradientDrawable.Orientation.LEFT_RIGHT);
}
#Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClickListener(view, getAdapterPosition(), categoryList.get(getAdapterPosition()));
}
}
}
}
Code for view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardview"
android:layout_width="150dp"
android:layout_height="100dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp">
<LinearLayout
android:id="#+id/llbackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/category_gradient"
android:gravity="center">
<TextView
android:id="#+id/categoryName"
android:text="Men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#color/colorWhite"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/selectItemLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
android:padding="5dp">
<ImageView
android:id="#+id/selectItem"
android:layout_width="20dp"
android:layout_height="20dp"
android:clickable="false"
android:tag="#string/unchecked"
android:enabled="false"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/circle"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
Recycler view in activity
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/categoriesRecyclerView"
android:layout_below="#+id/categoriesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
Thanks for help!!
I'm new to Android Studio and coding in general and I am trying to display a custom ListView with an image of a colour and the name of the colour. I have used a custom Adapter to inflate the view with two arrays containing the drawable image colours and string of the names.
The problem I am having - the custom adapter is only displaying the layers from position 1 and onwards. For example, where the first layer should be img_01 and "red" - it is displaying img_02 and "Orange".
I've tried debugging the code and it seems that though the adapter is setting the values for position 0, but I cannot find the reason why it is not displaying (as the others are displaying fine).
The images & names are just placeholders, so an alternative solution that doesn't include both the drawables and names wouldn't be viable,
Thank you in advance
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context context;
int[] images;
String[] colour;
LayoutInflater mInflater;
public CustomAdapter(Context c, int[] i, String[] col) {
this.context = c; //sets the application context that has been passed to the adapter
this.images = i; //sets the images array that has been passed to the adapter
this.colour = col; //sets the colour array that has been passed to the adapter
mInflater = (LayoutInflater.from(context)); //sets the layout inflater from context
}
#Override
public int getCount() { //total number of elements in the array
return images.length;
}
#Override
public Object getItem(int position) { //gets the item using the position
return null;
}
#Override
public long getItemId(int position) { //gets the item position
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) { //this is used to update the view
convertView = mInflater.inflate(R.layout.custom_layout, null);
ImageView image = convertView.findViewById(R.id.imageView);
TextView text = convertView.findViewById(R.id.textView);
text.setText(colour[position]);
image.setImageResource(images[position]);
return convertView;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView mListView;
int[] images = {R.drawable.img01,
R.drawable.img02,
R.drawable.img03,
R.drawable.img04};
String[] colour = {"Red",
"Orange",
"Yellow",
"Green"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), images, colour);
//passes information of images and application context to the item adapter
mListView.setAdapter(customAdapter);
//sets the adapter to the listview
}
}
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="7dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="9dp"
app:srcCompat="#drawable/img01" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="36dp"
android:text="colourName" />
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try to set the height of the ListView to wrap_content instead of hardcoding it
<ListView
...
android:layout_height="wrap_content"/>
You can do it with RecyclerView.
Create a custom class
public class ExampleItem {
private int mImageResource; //your color image
private String mText1; //your text
public ExampleItem(int imageResource, String text1) {
mImageResource = imageResource;
mText1 = text1;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
}
Create a XML layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="2dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:text="Line 1"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Create a Recycler View Adapter
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
}
#Override
public int getItemCount() {
return mExampleList.size();
}
}
your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.application.recyclerviewproject.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.img01, "Red"));
exampleList.add(new ExampleItem(R.drawable.img02, "Orange"));
exampleList.add(new ExampleItem(R.drawable.img03, "Yellow"));
exampleList.add(new ExampleItem(R.drawable.img04, "Green"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
So, for a personal project I'm making a stream app for Android which are separated by three fragments in the main screen where each of those fragment will be a list of a type of show in our archive, like movies, TV series and animations by using a RecyclerView inside of them.
That being said, after some errors that I managed to fix, the app is not showing what was expected, by skipping the RecyclerView because it has no "adapter".
Here is the code so far:
The main activity:
public class MenuPrincipal extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_principal);
AbasMenuPrincipalAdapter adapter = new AbasMenuPrincipalAdapter( getSupportFragmentManager() );
adapter.adicionar( new FilmesFragment() , "Filmes");
adapter.adicionar( new SeriesFragment() , "Series");
adapter.adicionar( new AnimacoesFragment() , "Animações");
ViewPager viewPager = (ViewPager) findViewById(R.id.abas_view_pager);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.abas);
tabLayout.setupWithViewPager(viewPager);
}
}
One of the fragments used on the main screen:
public class AnimacoesFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
ArrayList<Item> exemploItemList = new ArrayList<Item>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.animacoes_fragment, container, false);
TextView tv = view.findViewById(R.id.text1);
tv.setText("Você está na terceira aba");
exemploItemList.add(new Item(R.drawable.ic_android, "Line1", "line2"));
exemploItemList.add(new Item(R.drawable.ic_desktop_mac, "Line2", "line3"));
exemploItemList.add(new Item(R.drawable.ic_laptop_windows, "Line4", "line5"));
mRecyclerView = (RecyclerView) view.findViewById(R.id.AnimacaoRecyclerView);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
ItemAdapter adapter = new ItemAdapter(exemploItemList);
mAdapter = adapter;
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
And its layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/AnimacaoRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:scrollbars="vertical" />
</LinearLayout>
This is what I'm trying to show on that RecyclerView (later will be the real thing, now it's just a test):
public class Item {
private int mImageResource;
private String text1;
private String text2;
public Item(int mImageResource, String text1, String text2) {
this.mImageResource = mImageResource;
this.text1 = text1;
this.text2 = text2;
}
public int getmImageResource() {
return mImageResource;
}
public void setmImageResource(int mImageResource) {
this.mImageResource = mImageResource;
}
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
}
It's layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/imagem"
android:padding="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="0dp"
android:text="#string/line1"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/textView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="28dp"
android:text="#string/line2"
android:textSize="15sp"
android:textStyle="bold"
android:id="#+id/textView2"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Finally, the adapter used to convert the list:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private ArrayList<Item> mItemList;
public static class ItemViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
}
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
ItemViewHolder ivh = new ItemViewHolder(v);
return ivh;
}
public ItemAdapter(ArrayList<Item> itemList){
mItemList = itemList;
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position) {
Item currentItem = mItemList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
#Override
public int getItemCount() {
return mItemList.size();
}
}
First of all, check your fragment layout file. The linear layout parent orientation set to 'horizontal' and width of it's child views ('textview' & 'recyclerview') is match parent. This won't work.
Try to initialize your recyclerview adapter in 'onCreate' and use 'notifyDataSetChanged()' after setting up the adapter with recyclerview.
I want to create dynamic views with x number of rows for each CardView that gets created. To help illustrate my idea this image shows what I essentially want to do.
For each CardView I want to be able to put any amount of TableRows into the CardView. So one CardView could have 4 rows whereas the next CardView could have 2 rows and so on.
EDIT: Implementation
Essentially this view is replicated several times, I would like to minimise having to write seperate layouts for each CardView with x amount of rows like in the Card Storage implemenation which is more reusable than what I currently have. As I hit the limit here, I have posted a pastebin of the current layout with rows that I have
https://pastebin.com/9RFDGhXv
Layout: Card Storage
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="#style/CardStyle">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/MainConstraints" >
<TableRow
android:id="#+id/row_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:id="#+id/card_title"
style="#style/Title"/>
</TableRow>
<TableRow
android:id="#+id/row_two"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="#+id/row_one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.constraint.ConstraintLayout>
<ImageView
android:id="#+id/drawable"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
style="#style/RowDrawableElement"
/>
<TextView
android:id="#+id/percentage_descriptor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/drawable"
android:text="#string/percentage_descriptor"
style="#style/RowTextElement" />
<TextView
android:id="#+id/percentage_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
style="#style/RowTextElement" />
</android.support.constraint.ConstraintLayout>
</TableRow>
<TableRow
android:id="#+id/row_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/row_two"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
style="?android:attr/progressBarStyleHorizontal"
/>
</TableRow>
<TableRow
android:id="#+id/row_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/row_three"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.constraint.ConstraintLayout>
<TextView
android:id="#+id/text_used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
<TextView
android:id="#+id/text_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
</TableRow>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Adapter
public class StorageAdapter extends RecyclerView.Adapter<StorageAdapter.MyViewHolder> {
private Context mContext;
private List<StorageObjects> mList;
public StorageAdapter(Context context, List<StorageObjects> list) {
this.mContext = context;
this.mList = list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_storage, parent, false);
return new MyViewHolder(view);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
StorageObjects storageObjects = mList.get(position);
long used = storageObjects.getUsed();
long total = storageObjects.getTotal();
holder.mProgress.setMax(0);
holder.mProgress.setProgress(0);
holder.mTitle.setText(storageObjects.getTitle());
MiscUtils.testDrawableIdentifier(mContext.getApplicationContext(), holder.mImageViews, storageObjects.getDrawables());
holder.mPercentage.setText(storageObjects.getPercentage() + mContext.getString(R.string.percentage));
holder.mProgress.setMax((int) (total/100000));
holder.mProgress.setProgress((int) (used/100000));
holder.mUsed.setText(MiscUtils.humanReadableByteSize(used));
holder.mTotal.setText(MiscUtils.humanReadableByteSize(total));
}
#Override
public int getItemCount() {
return mList.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.card_title) TextView mTitle;
#BindViews({R.id.drawable}) List<ImageView> mImageViews;
#BindView(R.id.percentage_text) TextView mPercentage;
#BindView(R.id.progress_bar) ProgressBar mProgress;
#BindView(R.id.text_used) TextView mUsed;
#BindView(R.id.text_total) TextView mTotal;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
Class Storage
public class Storage extends Fragment {
private Unbinder mUnbinder;
private List<StorageObjects> storageList;
private StorageAdapter adapter;
private RecyclerView.LayoutManager mLayoutManager;
#BindArray(R.array.storage_ic_images)
String mDrawables[];
#BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
public Storage() {
// Required empty public constructor
}
public static Storage newInstance() {
return new Storage();
}
#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 view = inflater.inflate(R.layout.fragment_storage, container, false);
mUnbinder = ButterKnife.bind(this, view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onResume() {
super.onResume();
initRecyclerView();
}
#Override
public void onDestroy() {
super.onDestroy();
mUnbinder.unbind();
}
private void initRecyclerView() {
storageList = new ArrayList<>();
adapter = new StorageAdapter(getActivity(), storageList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLayoutManager = ViewUtils.getLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(2, ViewUtils.dpToPx(
getActivity(), 10), true));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
prepareView();
}
#SuppressLint("SetTextI18n")
private void prepareView(){
StorageObjects storage
= new StorageObjects(
getActivity().getString(R.string.int_storage_title),
StorageUtils.internalUsed(),
StorageUtils.internalTotal(),
StorageUtils.internalPercentage(),
mDrawables[0]);
storageList.add(storage);
storage = new StorageObjects (
getActivity().getString(R.string.ext_storage_title),
StorageUtils.externalUsed(getActivity()),
StorageUtils.externalTotal(getActivity()),
StorageUtils.externalPercentage(getActivity()),
mDrawables[1]
);
storageList.add(storage);
adapter.notifyDataSetChanged();
}
}
If number of rows per card view is less then you can inflate edit text and add it to linear layout.
Note : It's a bad practice to add scroll view inside a scroll view
I'm creating a list of "channels" on an app that users can select. I want them in a grid view, (kind of like clickable tiles). There are supposed to be two separate Grids on this Fragment - default channels (channelListSupplied) and ones that the user will have to request subscriptions to (channelListEarned).
I used a custom adapter that I got from another answer here on SO, but I can't get it to work because it's in a Fragment instead of in the Activity, and I'm sure there's some reference I'm not passing correctly.
Below is a list of the relevant pieces of Java and XML...
FragmentChannels.java: (fragment to MainActivity.java)
public class FragmentChannels extends Fragment implements FragmentManager.OnBackStackChangedListener {
ViewGroup container;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
final JSONObject channelList = data.getJSONObject("channels");
final JSONArray channelListSupplied = channelList.getJSONArray("supplied");
final JSONArray channelListEarned = channelList.getJSONArray("earned");
GridView channelViewSupplied = (GridView) view.findViewById(R.id.channel_grid_supplied);
GridView channelViewEarned = (GridView) view.findViewById(R.id.channel_grid_earned);
if (Session.getSessionVar("canHasSpecial").equals("1")) {
FragmentChannels.this.createGridView(channelViewEarned, channelListEarned, FragmentChannels.this.container);
}
FragmentChannels.this.createGridView(channelViewSupplied, channelListSupplied, FragmentChannels.this.container);
...
return view;
}
public void createGridView(final GridView gridView, JSONArray list, final ViewGroup container) throws JSONException {
final String[] gridList = new String[list.length()];
final Activity activity = getActivity();
for (int i = 0; i < list.length(); i++) {
JSONObject channelData = (JSONObject) list.get(i);
gridList[i] = channelData.getString("source_name");
}
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
gridView.setAdapter(new GridViewAdapter(activity, gridList, container));
}
});
}
public boolean createChannelMenu(Menu menu) {
MenuInflater inflater = this.getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_channels, menu);
super.onCreateOptionsMenu(menu, inflater);
return true;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
final Activity activity = getActivity();
}
#Override
public void onBackStackChanged() {
}
}
GridViewAdapter.java:
public class GridViewAdapter extends BaseAdapter {
private Context context;
private final String[] textViewValues;
private ViewGroup container;
public GridViewAdapter(Context context, String[] textViewValues, ViewGroup container) {
this.context = context;
this.textViewValues = textViewValues;
this.container = container;
}
#Override
public int getCount() {
return this.textViewValues.length;
}
#Override
public Object getItem(int position) {
return textViewValues[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("CDFF", position+": "+this.textViewValues[position]);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = inflater.inflate(R.layout.grid_item_layout, this.container);
TextView titleView = (TextView) gridView.findViewById(R.id.grid_item_title);
titleView.setText(textViewValues[position]);
}
else {
gridView = convertView;
}
return gridView;
}
}
fragment_channels.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="FragmentChannels"
android:id="#+id/fragment_channel_container"
tools:context="xx.xxx.xxxx.FragmentChannels"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="My Channels"
android:background="#color/peach"
android:textSize="20sp"
android:textColor="#color/midnightBlue"
android:gravity="center"
android:textAppearance="#style/TextAppearance.FontPath"
/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/channel_grid_earned"
android:layout_margin="5dp"
android:columnWidth="180dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/channel_grid_supplied"
android:layout_margin="5dp"
android:columnWidth="180dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</FrameLayout>
grid_item_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#color/white"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="150dp"
android:layout_height="100dp"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/grid_item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:gravity="center"
android:maxLines="2"
android:ellipsize="marquee"
android:textSize="12sp" />
</RelativeLayout>
As of now, nothing displays on the Fragment except the "My Channels" TextView.
I greatly appreciate any help!
Fixed.
I changed the line:
gridView = inflater.inflate(R.layout.grid_item_layout, this.container);
To:
gridView = inflater.inflate(R.layout.grid_item_layout, null);
in the GridViewAdapter.java class.