So i just made this simple fragment that contains a RecycleView. The recycle view has a custom made relative layout with two TextViews and a CheckBox. The problem is that a single list item takes up the whole AVD seceen's space and the listener I set for the list item is not working.
here is a screenshot of the AVD screen (I am using android studio)
And when scrolled
The XML layout code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<CheckBox
android:id="#+id/list_item_crime_solved_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="4dp"
/>
<TextView
android:id="#+id/list_item_crime_title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/list_item_crime_solved_check_box"
android:textStyle="bold"
android:padding="4dp"
tools:text="Crime Title"/>
<TextView
android:id="#+id/list_item_crime_date_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/list_item_crime_solved_check_box"
android:layout_below="#id/list_item_crime_title_text_view"
android:padding="4dp"
tools:text="Crime date"
/>
</RelativeLayout>
The Fragment java code:
package com.bignerdranch.android.criminalintent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class CrimeListFragment extends Fragment
{
private RecyclerView mCrimeRecyclerView;
private CrimeAdabter mCrimeAdabter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_crime_list, container, false);
mCrimeRecyclerView = (RecyclerView) v.findViewById(R.id.crime_recycler_view);
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return v;
}
private void updateUI()
{
CrimeLab crimeLab = CrimeLab.get(getActivity());
List<Crime> crimes = crimeLab.getCrimes();
mCrimeAdabter = new CrimeAdabter(crimes);
mCrimeRecyclerView.setAdapter(mCrimeAdabter);
}
}
The ViewHolder java code:
private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView mTitleTextView;
private TextView mDateTextView;
private CheckBox mCrimeSolvedCheckBox;
private Crime mCrime;
public CrimeHolder(View itemView)
{
super(itemView);
mTitleTextView = (TextView) itemView.findViewById(R.id.list_item_crime_title_text_view);
mDateTextView = (TextView) itemView.findViewById(R.id.list_item_crime_date_text_view);
mCrimeSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.list_item_crime_solved_check_box);
}
public void bindCrime(Crime crime)
{
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
mCrimeSolvedCheckBox.setChecked(mCrime.isSolved());
}
#Override
public void onClick(View v)
{
Intent i=new Intent(getActivity(),CrimeActivity.class);
startActivity(i);
}
}
And the Adapter java code:
private class CrimeAdabter extends RecyclerView.Adapter<CrimeHolder>
{
private List<Crime> mCrimes;
public CrimeAdabter(List<Crime> crimes)
{
mCrimes = crimes;
}
#Override
public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(getActivity());
View v = inflater.inflate(R.layout.list_item_crime, parent, false);
return new CrimeHolder(v);
}
#Override
public void onBindViewHolder(CrimeHolder holder, int position)
{
Crime crime = mCrimes.get(position);
holder.bindCrime(crime);
}
#Override
public int getItemCount()
{
return mCrimes.size();
}
}
It's because your root RelativeLayout's height is set to match_parent. Make it wrap_content or specific height.
Related
I currently have a grid view using a base adapter inside a fragment and I am trying to transfer to a different fragment when one of the items is clicked but none of the solutions I found on stack overflow has worked. I might miss something.
Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.licenta.joberfrontend.R;
import com.licenta.joberfrontend.rest.backend_entieties.Category;
import java.util.ArrayList;
import java.util.List;
public class CategoriesAdapter extends BaseAdapter {
public class ViewHolder {
TextView textName;
ImageView imageView;
}
private ArrayList<Category> categoryList;
public Context context;
public CategoriesAdapter(List<Category> apps, Context context) {
this.context = context;
this.categoryList = (ArrayList<Category>) apps;
}
#Override
public int getCount() {
return categoryList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View view, ViewGroup parent) // inflating the layout and initializing widgets
{
ViewHolder viewHolder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.category_list_content, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = view.findViewById(R.id.textName);
viewHolder.imageView = view.findViewById(R.id.iconView);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
// here we are setting up the names and images
viewHolder.textName.setText(categoryList.get(position).getName());
viewHolder.imageView.setImageResource(this.context.getResources().getIdentifier(categoryList.get(position).getCategoryIconId(), "mipmap", this.context.getPackageName()));
return view;
}
}
Fragment's OnCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LocalStorageSaver localStorageSaver = new LocalStorageSaver(Objects.requireNonNull(getContext()));
final ToastShower toastShower = new ToastShower();
//REST services creation
final RetrofitCreator retrofitCreator = new RetrofitCreator();
final Retrofit retrofit = retrofitCreator.getRetrofit();
final CategoryService categoryService = retrofit.create(CategoryService.class);
final Call<List<Category>> getCategoriesRequest = categoryService.getAllCategoriesAndTheirJobs(localStorageSaver.getValueFromStorage(Constants.TOKEN));
getCategoriesRequest.enqueue(
new Callback<List<Category>>() {
#Override
public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
toastShower.showToast("Categories succesfully retrieved from backend.", getContext());
final GridView gridView = Objects.requireNonNull(getView()).findViewById(R.id.gridViewNewContract);
gridView.setAdapter(new CategoriesAdapter(response.body(), getActivity()));
}
#Override
public void onFailure(Call<List<Category>> call, Throwable t) {
toastShower.showToast("There has been a problem with retrieving the categories data!", getContext());
}
}
);
}
Items inside the grid view
<?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">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iconView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_list"
android:tint="#color/colorAccent" />
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="sans-serif"
android:maxLength="12"
android:text="#string/appName"
android:textColor="#color/colorAccent"
android:textSize="13sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
If you need any more information I will gladly provided.
I can mention that I've tried placing listeners both in the adapter and in the fragment directly on the grid.
please try this
viewHolder.imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
package com.android_examples.recyclerview_android_examplescom;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Context context;
RecyclerView recyclerView;
LinearLayout relativeLayout;
Button button;
RecyclerView.Adapter recyclerViewAdapter;
RecyclerView.LayoutManager recylerViewLayoutManager;
String[] subjects =
{
"ANDROID",
"PHP",
"BLOGGER",
"WORDPRESS",
"JOOMLA",
"ASP.NET",
"JAVA",
"C++",
"MATHS",
"HINDI",
"ENGLISH"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
context = getApplicationContext();
relativeLayout = (LinearLayout) findViewById(R.id.relativelayout1);
button = (Button) findViewById(R.id.button);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
recylerViewLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(recylerViewLayoutManager);
recyclerViewAdapter = new RecyclerViewAdapter(context, subjects);
recyclerView.setAdapter(recyclerViewAdapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
recylerViewLayoutManager.findViewByPosition(10).setBackgroundColor(Color.BLUE);
}
});
}
}
activity_main:--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.android_examples.recyclerview_android_examplescom.MainActivity"
android:id="#+id/relativelayout1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change"
android:id="#+id/button"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview1"
android:scrollbars="vertical"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
RecyclerAdapter:--
package com.android_examples.recyclerview_android_examplescom;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
String[] SubjectValues;
Context context;
View view1;
ViewHolder viewHolder1;
TextView textView;
public RecyclerViewAdapter(Context context1,String[] SubjectValues1){
SubjectValues = SubjectValues1;
context = context1;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(View v){
super(v);
textView = (TextView)v.findViewById(R.id.subject_textview);
}
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
view1 = LayoutInflater.from(context).inflate(R.layout.recyclerview_items,parent,false);
viewHolder1 = new ViewHolder(view1);
return viewHolder1;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position){
package com.android_examples.recyclerview_android_examplescom;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
String[] SubjectValues;
Context context;
View view1;
ViewHolder viewHolder1;
TextView textView;
public RecyclerViewAdapter(Context context1,String[] SubjectValues1){
SubjectValues = SubjectValues1;
context = context1;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(View v){
super(v);
textView = (TextView)v.findViewById(R.id.subject_textview);
}
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
view1 = LayoutInflater.from(context).inflate(R.layout.recyclerview_items,parent,false);
viewHolder1 = new ViewHolder(view1);
return viewHolder1;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position){
holder.textView.setText(SubjectValues[position]);
}
#Override
public int getItemCount(){
return SubjectValues.length;
}
}
holder.textView.setText(SubjectValues[position]);
}
#Override
public int getItemCount(){
return SubjectValues.length;
}
}
If i want to change the background of perticular item in Recyclerview from button click it will effect to only visible items only not for invisible means (first item is visible to user it will effect, but for last item it will not change the color )
Please Help me...
Thanks In advance..
OnItemClickListener Will give you the position of the view and you can use this below code for changing background of selected positioned row
private View SelectedItem;
private void BackColor(View view) {
if (SelectedItem != null) {
SelectedItem.setBackgroundColor(Color.TRANSPARENT);
}
view.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
SelectedItem = view;
}
yourListview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick (AdapterView < ? > adapterView, View view,int i, long l){
BackColor(view);
}
}
First get the click action of convertView (Object of View) under this set view as
if(!convertView.isSelected()){
convertView.setSelected(true);
//here change the background color as selected.
}else{
convertView.setSelected(false);
//here change the background color as unselected.
}
Handel isSelected status in selection.
How to get the convertView Object reference
Here is my main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.smartrix.horizontal_listview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Here is my row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_margin="12dp"
android:layout_width="150dp"
android:layout_height="250dp">
<TextView
android:id="#+id/title"
android:textStyle="bold"
android:textSize="20dp"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.CardView>
Here is my mainActivity.java
package com.smartrix.horizontal_listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView.Adapter mAdapter;
private ArrayList<String> mDataSet;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDataSet = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mDataSet.add("New Title #"+i);
}
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MainAdapter(mDataSet);
mRecyclerView.setAdapter(mAdapter);
}
}
Here is my mainAdapter.java ::
package com.smartrix.horizontal_listview;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
private ArrayList<String> mDataSet;
public MainAdapter(ArrayList<String> mDataSet){
this.mDataSet = mDataSet;
}
#Override
public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {
holder.mTitle.setText(mDataSet.get(position));
}
#Override
public int getItemCount() {
return mDataSet.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTitle;
public ViewHolder(View itemView) {
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.title);
}
}
}
This Code Works Perfectly. Now I want to add this Horizontal ListView in Every ListItem of Vertical ListView. How to do this ??
You would simply add a new Horizontally oriented RecyclerView as an item in your list (and you would add it to your adapter).
It is fine to nest RelativeViews into each other.
In your case, add it to your row.xml layout, then inflate it in your mainAdapter.java class
i have an activity, which includes a fragment. This fragment includes a Recycler View.
I want it to display data of a game in a grid.
I need it to scroll down (every round of the game is one row) but also horizontal, because i need 5 - 10 Columns.
When id use the paramter android:scrollbars="vertical|horizontal" in the recycler view it only scrolls down and makes it column very small. This happens even when i set it only to horizontal.
How it shouldnt look..
Code:
The Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.sb.matt.doppelkopf.activities.GameActivity">
<fragment
android:id="#+id/fragment_game"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.sb.matt.doppelkopf.fragments.game_fragment"
tools:layout="#layout/fragment_game">
</fragment>
</RelativeLayout>
The Fragment
<RelativeLayout 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"
tools:context="com.sb.matt.doppelkopf.fragments.game_fragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_gameData"
android:scrollbars="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
The View for the Items in the Recycler View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="200dp">
<TextView
android:layout_width="400dp"
android:layout_height="200dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Test"
android:id="#+id/txt_inhalt"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
Fragment Code
package com.sb.matt.doppelkopf.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.sb.matt.doppelkopf.R;
import com.sb.matt.doppelkopf.activities.GameActivity;
import com.sb.matt.doppelkopf.adapters.GameData_Adapter;
import com.sb.matt.doppelkopf.data.GameData;
/**
* A simple {#link Fragment} subclass.
*/
public class game_fragment extends Fragment
{
private GameData gameData;
private RecyclerView rGridView;
GameData_Adapter adapter;
public game_fragment()
{
// 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
final View v = inflater.inflate(R.layout.fragment_game, container, false);
gameData = ((GameActivity)getActivity()).getGameData();
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
String[][] gameData = ((GameActivity) getActivity()).getGameData().getViewData();
rGridView = (RecyclerView) getActivity().findViewById(R.id.recyclerView_gameData);
rGridView.setLayoutManager(new GridLayoutManager(getActivity(), gameData[0].length));
adapter = new GameData_Adapter(gameData);
rGridView.setAdapter(adapter);
}
}
Adapter Code, its uses a 2 dimensional Array to fill the Grid.
package com.sb.matt.doppelkopf.adapters;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.sb.matt.doppelkopf.R;
import com.sb.matt.doppelkopf.data.SingleGameDataItem;
import java.util.ArrayList;
/**
* Created by matts on 16.01.2016.
*/
public class GameData_Adapter extends RecyclerView.Adapter<GameData_Adapter.ViewHolder>
{
private String[][] gameData;
private ArrayList<SingleGameDataItem> list;
public static class ViewHolder extends RecyclerView.ViewHolder
{
View MyView;
TextView textView;
public ViewHolder(View view)
{
super(view);
MyView = view;
textView = (TextView) view.findViewById(R.id.txt_inhalt);
}
}
public GameData_Adapter (String[][] gameData)
{
this.gameData = gameData;
list = new ArrayList<SingleGameDataItem>();
for(int i = 0; i < gameData.length; i++)
{
for(int j = 0; j < gameData[0].length; j++)
{
SingleGameDataItem item = new SingleGameDataItem(gameData[i][j]);
list.add(item);
}
}
}
#Override
public GameData_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.singlegamedataitem, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.textView.setText(list.get(position).getInhalt());
}
#Override
public int getItemCount()
{
return list.size();
}
}
What i am doing wrong? :/
You used wrong constructor new GridLayoutManager(getActivity(), gameData[0].length). Javadoc says that it "Creates a vertical GridLayoutManager".
Try new GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false)
instead.
Here 1 counts as number of rows.
This is the Java file of the fragment I wish to insert recycler view. Please dont worry about the package name which I deleted for some reasons.
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class Dashboard extends Fragment {
private RecyclerView recyclerView;
private RecyclerAdapter adapter;
/**
* Returns a new instance of this fragment for the given section number.
*/
public static Dashboard newInstance() {
Dashboard fragment = new Dashboard();
return fragment;
}
public Dashboard () {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_dashboard, container,
false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.drawerlist);
adapter=new RecyclerAdapter(getActivity().getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
public static List<Information> getData(){
List<Information> data=new ArrayList<>();
String[] titles={"Rooms Occupied","RoomsVacant","Check-In","Check-Out","Extensions","Confirmations","Cancellations"};
for(int i=0;i<titles.length;i++)
{
Information current =new Information();
current.title=titles[i];
data.add(current);
}
return data;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
This is my Adapter class
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
/**
* Created by gowtham on 6/13/2015.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<Information> data= Collections.emptyList();
public RecyclerAdapter(Context context, List<Information> data){
inflater=LayoutInflater.from(context);
this.data=data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_row, parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current=data.get(position);
holder.title.setText(current.title);
}
#Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.viewText);
}
}
}
This is Layout file of the fragment in which I want to use the recycler view
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewImage"
android:background="#drawable/ic_dashboard"
android:layout_gravity="center_vertical"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/viewText"
android:text="Dummy Text"
android:textStyle="bold"
android:layout_gravity="left"
android:gravity="center" />
</LinearLayout>
Since I'm new to stackoverflow I don't have enough reputations to add image. So I will attach the screenshot in the comments below. Please take a look at it to know what is happening here exactly. Thanks!
Why you call getActivity().getData(), just call getData().
And in your Adapter class:
#Override
public int getItemCount() {
return data.size();
}
It can't just return zero, you should return the data size.