I'm trying to add a divider on top of my element in a RecyclerView.
Following google i've succeded to add a DividerItemDecoration, the code is as below:
DividerItemDecoration:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
#Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
The result:
What i'm trying to accomplish:
I've tried to play with the offset and the index but didn't get expected result.
How can I add the same item divider on top of my elements?
Also in case of necessary i've leave my layout of the row, here is the code:
Row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
Explanation : In this answer i have added a divider above every item in your RecyclerView and after the RecyclerView itself. This way you wont have "overdraw", where 2 lines are painted on top of each other.I have used View tag for this purpose which draws a simple line from the xml.
Row file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<View android:layout_height="2dp"
android:layout_width="match_parent"
android:layout_marginBottom="5dp"
android:background="#color/colorPrimary"/>
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
Main file :
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/repository_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/tmp3"
android:layout_marginTop="30dp"/>
<View
android:id="#+id/bottomLine"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:background="#color/colorPrimary"/>
</LinearLayout>
Copy and paste DividerItemDecoration and change the following two lines. I only tested for vertical orientation.
//Change - getItemOffsets
//old
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight())
//new
outRect.set(0, mDivider.getIntrinsicHeight(), 0, 0)
//Change - drawVertical
//old
final int bottom = mBounds.bottom + Math.round(child.getTranslationY())
//new
final int bottom = mBounds.bottom + Math.round(child.getTranslationY()) - child.getHeight()
Don't add divider via Item Decoration. Instead put Two vies above and below of your row code like below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="Vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_height="1px"
android:layout_width="match_parent"
android:background="#000000"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>
<TextView
android:id="#+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>
</RelativeLayout>
<View android:layout_height="1px"
android:layout_width="match_parent"
android:background="#000000"
/>
</LinearLayout>
Related
How could I add a line just above of first item (10) a line?
I'm using RecyclerView and LinearLayoutManager to implement my design.
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">
<SeekBar
android:id="#+id/timesTablesSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
list_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="beginning">
<TextView
android:id="#+id/timeTables"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="70px"
android:textStyle="bold|italic" />
</LinearLayout>
First of all, using LinearLayout's android:showDividers to divide items is not a best practice. First, I would remove outer LinearLayout from your list_layout.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/timeTables"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="70px"
android:textStyle="bold|italic" />
To show dividers, the best way is to use RecyclerView.ItemDecoration. Please see DividerItemDecoration to draw divider:
https://developer.android.com/reference/androidx/recyclerview/widget/DividerItemDecoration
dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
Then the only thing missing would be drawing top line. You can implement a custom ItemDecoration like this:
public class TopLineDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private final Drawable divider;
public TopLineDecoration(Context context) {
TypedArray a = context.obtainStyledAttributes(ATTRS);
divider = a.getDrawable(0);
a.recycle();
}
#Override
public void onDraw(#NonNull Canvas c, #NonNull RecyclerView parent, #NonNull RecyclerView.State state) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
if (parent.getChildAdapterPosition(child) == 0) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getTop() - params.topMargin;
int bottom = top + divider.getIntrinsicHeight();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
}
}
And your final implementation would be like:
dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
topLineDecoration = new TopLineDecoration(recyclerView.getContext())
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.addItemDecoration(topLineDecoration)
I want to show horizontal items in recyclerview, but I've tried but no work it all, only show me horizontal items with enlarge widh.
In Android Studio show as I want
But in runtime in my movile, show me
The items is width wraped but the other items appear after scroll right separated of my previus item.
This is my code.
LIST_ITEM
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearFondo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/item_list_sel"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
app:srcCompat="#drawable/ruta"
tools:srcCompat="#drawable/ruta" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/tnombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/thorarios"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And My Xml to show the recyclerview
<LinearLayout 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"
android:orientation="vertical"
tools:context="sitetech.NFCcheckPoint.ui.operador.CheckFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/SuccessColor"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/tfecha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Fecha"
android:textSize="20sp" />
<Button
android:id="#+id/bpruebas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pasar Tarjeta" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ruta para el registro" />
<com.omega_r.libs.omegarecyclerview.OmegaRecyclerView
android:id="#+id/rlista"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#888"
android:dividerHeight="0dp"
android:fadeScrollbars="true"
android:orientation="horizontal"
android:overScrollMode="never"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbars="horizontal"
app:itemSpace="0dp"
tools:listitem="#layout/ruta_sel_template">
</com.omega_r.libs.omegarecyclerview.OmegaRecyclerView>
</LinearLayout>
</LinearLayout>
And my Fragment code to load
dataAdapter = new rutaSelAdapter(lista, new onItemClick() {
#Override
public void onClickItemList(View v, int position) {
ToastHelper.info(lista.get(position).getNombre().toString());
}
});
rlista.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
rlista.setAdapter(dataAdapter);
And my dataAdapter class
public class rutaSelAdapter extends OmegaRecyclerView.Adapter<rutaSelAdapter.ViewHolder> {
public List<Ruta> lista;
private onItemClick onItemClick;
private Ruta selectedItem;
public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
}
#Override
public int getItemCount() {
return lista.size();
}
#Override
public rutaSelAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new rutaSelAdapter.ViewHolder(parent);
}
#Override
public void onBindViewHolder(rutaSelAdapter.ViewHolder holder, int position) {
Ruta dato = lista.get(position);
holder.display(dato);
}
public void setSelectedItem(Ruta rx){
selectedItem = rx;
}
public Ruta getSelectedItem(){
return selectedItem;
}
public void updateData(Ruta bx) {
boolean nuevo = true;
for (Ruta rx : lista) {
if (rx.getId() == bx.getId()) {
lista.set(lista.indexOf(rx), bx);
nuevo = false;
}
ToastHelper.info("Se a modificado el horario.");
}
if (nuevo) {
lista.add(bx);
ToastHelper.exito("Horario asignado.");
}
notifyDataSetChanged();
}
public void deleteData(Ruta rx) {
//ToastHelper.normal("Se a eliminado el horario " + rx.getHora());
lista.remove(rx);
notifyDataSetChanged();
}
public class ViewHolder extends SwipeViewHolder implements View.OnClickListener {
private final TextView tnombre;
private LinearLayout linearFondo;
HorarioDao horarioManager = AppController.daoSession.getHorarioDao();
private Ruta currentItem;
public ViewHolder(ViewGroup itemView) {
super(itemView, R.layout.ruta_sel_template, SwipeViewHolder.NO_ID, SwipeViewHolder.NO_ID);
tnombre = findViewById(R.id.tnombre);
linearFondo = findViewById(R.id.linearFondo);
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setSelectedItem(currentItem);
onItemClick.onClickItemList(v, getAdapterPosition());
}
});
}
private Ruta backItem;
private void setSelection(Ruta rx){
//if (backItem == null)
}
#Override
public void onClick(final View v) {
}
public void display(Ruta rx) {
currentItem = rx;
if (rx.getNombre() == null) tnombre.setText("");
else tnombre.setText(rx.getNombre().toString());
}
}
}
In Android Studio show me right as I want.
But in runtime, in my mobile the items have full width but wrap content.enter image description here
i think you just add notifyDataSetChanged in your class adapter
public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
notifyDataSetChanged();
}
In your recyclerview item layout update width of these 2 textviews [tnombre and thorarios] to wrap_content
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/tnombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/thorarios"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>
</LinearLayout>
If someone else have this issue, I fixed adding this line in the fragment class.
rlista.setHasFixedSize(true);
I have to make a listview from database and i want to put a image that is clickable in my list.I use CursorAdapter for it,but my image does not show on my list.Here is my BooksCursorAdapter
public class BooksCursorAdapter extends CursorAdapter {
public BooksCursorAdapter(Context context, Cursor c) {
super(context, c, 0 );
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.books_list_item, parent, false);
}
#Override
public void bindView(View view,final Context context, Cursor cursor) {
TextView productText = view.findViewById(R.id.productText);
TextView priceText = view.findViewById(R.id.priceText);
TextView quantityText = view.findViewById(R.id.quantityText);
ImageView shopButton = view.findViewById(R.id.shop_button);
int productColumnIndex = cursor.getColumnIndex(BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT);
int priceColumnIndex = cursor.getColumnIndex(BooksContract.BooksEntry.COLUMN_BOOKS_PRICE);
final int quantityColumnIndex = cursor.getColumnIndex(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY);
String bookProduct = cursor.getString(productColumnIndex);
String bookPrice = cursor.getString(priceColumnIndex);
final int bookQuantity = cursor.getInt(quantityColumnIndex);
productText.setText(bookProduct);
priceText.setText(bookPrice);
quantityText.setText(Integer.toString(bookQuantity));
int idColumnIndex = cursor.getColumnIndex(BooksContract.BooksEntry._ID);
final int booksId = cursor.getInt(idColumnIndex);
shopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri currentUri = ContentUris.withAppendedId(BooksContract.BooksEntry.CONTENT_URI, booksId);
makeSale(context, bookQuantity, currentUri);
}
});
}
private void makeSale(Context context, int bookQuantity, Uri uri) {
if (bookQuantity == 0) {
Toast.makeText(context, R.string.no_books, Toast.LENGTH_SHORT).show();
} else {
int newQuantity = bookQuantity - 1;
ContentValues values = new ContentValues();
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY, newQuantity);
context.getContentResolver().update(uri, values, null, null);
}
}
}
Everything works fine and how it should be only that my image is not showing.
My book_list_item.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="333dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_margin">
<TextView
android:id="#+id/productText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#2B3D4D" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/priceText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#AEB6BD"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/currencyTextList" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quantityTextList"
android:padding="5dp"/>
<TextView
android:id="#+id/quantityText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#AEB6BD" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="52dp"
android:layout_height="match_parent"
android:src="#drawable/shop_button"
android:id="#+id/shop_button"/>
</LinearLayout>
The image appears in my preview but not on my phone.I want to display an image stored on xml, that I can click on.
I did what #crammeur said, in second LinearLayout i replaced android:layout_width="333dp" with android:layout_width="0dp" and add android:layout_weight="1"
The first screen upload right and works fine (Toast and all other setting and parameters) until I try to save the result and open the new screen by the same button. Pushing the button crashes the app in my phone without opening the second screen. However in the emulator all work fine.
this is the Main activity:
public class MainActivity extends AppCompatActivity {
LineChart lineChart1;
RadioGroup rg1,rg2;
Button tipBtn;
Button checkAndCont;
int [] mSlop ={-1,3};
int [] b= {-3,3};
boolean userAnswer1 = false;
boolean userAnswer2 = false;
String quesType = "Slop Recognition";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lineChart1 = (LineChart)findViewById(R.id.lineChart1);
rg1 = (RadioGroup)findViewById(R.id.rg1);
rg2 = (RadioGroup)findViewById(R.id.rg2);
tipBtn = (Button)findViewById(R.id.tipBtb);
tipBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"גרף במערכת צירים נקרא משמאל לימין",Toast.LENGTH_SHORT ).show();
}
});
checkAndCont = (Button)findViewById(R.id.checkAndCont);
ArrayList<String> xAxes = new ArrayList<>();
ArrayList<Entry> yAxesLinear1 = new ArrayList<>();
ArrayList<Entry> yAxesLinear2 = new ArrayList<>();
int numberDataPoint = 21;
for (int i=0 ; i<numberDataPoint;i++ ){
int x=i-10;
float funLinear1= mSlop[0]*x+b[0];
float funLinear2= mSlop[1]*x+b[1];
yAxesLinear1.add(new Entry(x,funLinear1));
yAxesLinear2.add(new Entry(x,funLinear2));
//xAxes.add(i,String.valueOf(x));
}
//String [] xaxes = new String[xAxes.size()];
//for (int i=0; i<xAxes.size(); i++){
// xaxes[i]=xAxes.get(i);
//}
ArrayList<ILineDataSet> lineDataSet= new ArrayList<>();
LineDataSet lineDataSet1 = new LineDataSet(yAxesLinear1,"פונקציה קווית 1");
lineDataSet1.setDrawCircles(false);
lineDataSet1.setColor(Color.CYAN);
lineDataSet1.setDrawValues(false);
lineDataSet1.setLineWidth(3);
LineDataSet lineDataSet2 = new LineDataSet(yAxesLinear2,"פונקציה קווית 2");
lineDataSet2.setDrawCircles(false);
lineDataSet2.setColor(Color.GREEN);
lineDataSet2.setDrawValues(false);
lineDataSet2.setLineWidth(3);
lineDataSet.add(lineDataSet1);
lineDataSet.add(lineDataSet2);
lineChart1.setData(new LineData(lineDataSet));
lineChart1.setDrawBorders(true);
lineChart1.setBorderColor(0xffff00ff);
lineChart1.setBorderWidth(2);
lineChart1.setScaleEnabled(true);
//lineChart1.setDrawGridBackground(true);
//lineChart1.setGridBackgroundColor(0xffff00ff);
//lineChart1.setBackgroundColor(0x00000000);
XAxis xAxis = lineChart1.getXAxis();
//xAxis.setEnabled(true);
xAxis.setDrawAxisLine(true);
xAxis.setAxisLineColor(Color.WHITE);
lineChart1.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
lineChart1.getAxisRight().setEnabled(false);
lineChart1.getXAxis().setAxisMaximum(10);
lineChart1.getXAxis().setAxisMinimum(-10);
lineChart1.getXAxis().setGridColor(0xffff00ff);
//lineChart1.getXAxis().setDrawAxisLine(true);
//lineChart1.getXAxis().setAxisLineColor(Color.WHITE); // The buttom limit line of the chart
//lineChart1.setVisibleXRangeMaximum(50);
lineChart1.getXAxis().setTextColor(Color.WHITE);
lineChart1.getXAxis().setTextSize(15);
LimitLine ll = new LimitLine(0);
ll.setLineColor(Color.WHITE);
ll.setLineWidth(1);
xAxis.addLimitLine(ll);
YAxis yAxis = lineChart1.getAxisLeft();
yAxis.setDrawZeroLine(true);
yAxis.setZeroLineColor(Color.WHITE);// no grid lines
yAxis.setZeroLineWidth(1); //Sets the line-width of the zero line.
yAxis.setAxisMinimum(-10f); // start at zero
yAxis.setAxisMaximum(10f); // the axis maximum is 100
yAxis.setGridColor(0xffff00ff);
yAxis.setTextColor(Color.WHITE);
yAxis.setTextSize(15);
Legend legend = lineChart1.getLegend();
legend.setEnabled(true);
legend.setTextColor(Color.WHITE);
legend.setTextSize(18);
legend.setFormSize(13);
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton rb1 = (RadioButton) radioGroup.findViewById(i);
//if(R.id.rg1==i ? userAnswer1==true : userAnswer1==false);
if (null != rb1 && i > -1) {
// checkedId is the RadioButton selected
switch (i) {
case R.id.rb1:
userAnswer1=false;
break;
case R.id.rb2:
userAnswer1=true;
break;
}
}
Toast.makeText(MainActivity.this,Boolean.toString(userAnswer1),Toast.LENGTH_SHORT ).show();
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton rb2 = (RadioButton) radioGroup.findViewById(i);
//if(R.id.rg1==i ? userAnswer1==true : userAnswer1==false);
if (null != rb2 && i > -1) {
// checkedId is the RadioButton selected
switch (i) {
case R.id.rb3:
userAnswer2=false;
break;
case R.id.rb4:
userAnswer2=true;
break;
}
}
Toast.makeText(MainActivity.this,Boolean.toString(userAnswer2),Toast.LENGTH_SHORT ).show();
}
});
}
public void result(View view) {
if (isTrue(mSlop[0])==userAnswer1 && isTrue(mSlop[1])==userAnswer2){
Toast.makeText(MainActivity.this,"תשובה נכונה",Toast.LENGTH_SHORT ).show();
}else {
Toast.makeText(MainActivity.this,"לא נכון",Toast.LENGTH_SHORT ).show();
}
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}
boolean mType;
public boolean isTrue (int m){
if (m >0){
mType=true;
}else{
mType=false;
}
return mType;
}
}
this is the related 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:background="#drawable/background_color">
<com.github.mikephil.charting.charts.LineChart
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#+id/lineChart1">
</com.github.mikephil.charting.charts.LineChart>
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/lineChart1"
android:layout_marginStart="187dp"
android:paddingRight="15dp"
android:text="פונקציה 1 עולה"
android:textColor="#00FFFF"
android:textSize="25sp" />
<TextView
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/tv1"
android:layout_marginStart="187dp"
android:paddingRight="15dp"
android:text="פונקציה 2 עולה"
android:textColor="#ff00ff00"
android:textSize="25sp" />
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/lineChart1"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#00FFFF"
android:text="לא נכון"
android:textColor="#00FFFF"
android:paddingLeft="2dp"
android:textSize="25sp"
/>
<RadioButton
android:id="#+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#00FFFF"
android:text="נכון"
android:textColor="#00FFFF"
android:paddingLeft="2dp"
android:textSize="25sp"
android:layout_marginLeft="5sp"/>
</RadioGroup>
<RadioGroup
android:id="#+id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/rg1"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#ff00ff00"
android:text="לא נכון"
android:textColor="#ff00ff00"
android:paddingLeft="2dp"
android:textSize="25sp"
/>
<RadioButton
android:id="#+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#ff00ff00"
android:text="נכון"
android:textColor="#ff00ff00"
android:paddingLeft="2dp"
android:textSize="25sp"
android:layout_marginLeft="5dp"/>
</RadioGroup>
<Button
android:id="#+id/tipBtb"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentStart="true"
android:layout_below="#+id/rg2"
android:layout_marginStart="66dp"
android:layout_marginTop="11dp"
android:scaleType="fitCenter"
android:text="TIP" />
<Button
android:id="#+id/checkAndCont"
android:text="שלח"
android:layout_width="80sp"
android:layout_height="50sp"
android:layout_marginTop="11dp"
android:layout_below="#+id/tv2"
android:layout_centerHorizontal="true"
android:onClick="result"/>
</RelativeLayout>
this is the second activity:
public class Main2Activity extends AppCompatActivity {
LineChart lineChart2;
Button quickTipBtn,sendAndNext;
TextView question1,question2;
EditText answer1,answer2;
final int [] mSlop ={1,3};
final int [] b= {1,3};
private boolean userAnswer1 = false;
private boolean userAnswer2 = false;
final String quesType = "y-intercept Recognition";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
question1 = (TextView)findViewById(R.id.question1);
question2 = (TextView)findViewById(R.id.question2);
answer1 = (EditText)findViewById(R.id.answer1);
answer2 = (EditText)findViewById(R.id.answer2);
quickTipBtn =(Button)findViewById(R.id.quickTipBtn);
quickTipBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Main2Activity.this,"קואורדינטת החיתוך עם ציר Y ו b - שווים",Toast.LENGTH_SHORT ).show();
}
});
sendAndNext = (Button)findViewById(R.id.sendAndNext);
lineChart2 = (LineChart)findViewById(R.id.lineChart2);
ArrayList<String> xAxes = new ArrayList<>();
ArrayList<Entry> yAxesLinear1 = new ArrayList<>();
ArrayList<Entry> yAxesLinear2 = new ArrayList<>();
int numberDataPoint = 21;
for (int i=0 ; i<numberDataPoint;i++ ){
int x=i-10;
float funLinear1= mSlop[0]*x+b[0];
float funLinear2= mSlop[1]*x+b[1];
yAxesLinear1.add(new Entry(x,funLinear1));
yAxesLinear2.add(new Entry(x,funLinear2));
//xAxes.add(i,String.valueOf(x));
}
//String [] xaxes = new String[xAxes.size()];
//for (int i=0; i<xAxes.size(); i++){
// xaxes[i]=xAxes.get(i);
//}
ArrayList<ILineDataSet> lineDataSet= new ArrayList<>();
LineDataSet lineDataSet1 = new LineDataSet(yAxesLinear1,"פונקציה קווית 1");
lineDataSet1.setDrawCircles(false);
lineDataSet1.setColor(Color.CYAN);
lineDataSet1.setDrawValues(false);
lineDataSet1.setLineWidth(3);
LineDataSet lineDataSet2 = new LineDataSet(yAxesLinear2,"פונקציה קווית 2");
lineDataSet2.setDrawCircles(false);
lineDataSet2.setColor(Color.GREEN);
lineDataSet2.setDrawValues(false);
lineDataSet2.setLineWidth(3);
lineDataSet.add(lineDataSet1);
lineDataSet.add(lineDataSet2);
lineChart2.setData(new LineData(lineDataSet));
lineChart2.setDrawBorders(true);
lineChart2.setBorderColor(0xffff00ff);
lineChart2.setBorderWidth(2);
lineChart2.setScaleEnabled(true);
//lineChart1.setDrawGridBackground(true);
//lineChart1.setGridBackgroundColor(0xffff00ff);
//lineChart1.setBackgroundColor(0x00000000);
XAxis xAxis = lineChart2.getXAxis();
//xAxis.setEnabled(true);
xAxis.setDrawAxisLine(true);
xAxis.setAxisLineColor(Color.WHITE);
lineChart2.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
lineChart2.getAxisRight().setEnabled(false);
lineChart2.getXAxis().setAxisMaximum(10);
lineChart2.getXAxis().setAxisMinimum(-10);
lineChart2.getXAxis().setGridColor(0xffff00ff);
//lineChart1.getXAxis().setDrawAxisLine(true);
//lineChart1.getXAxis().setAxisLineColor(Color.WHITE); // The buttom limit line of the chart
//lineChart1.setVisibleXRangeMaximum(50);
lineChart2.getXAxis().setTextColor(Color.WHITE);
lineChart2.getXAxis().setTextSize(15);
LimitLine ll = new LimitLine(0);
ll.setLineColor(Color.WHITE);
ll.setLineWidth(1);
xAxis.addLimitLine(ll);
YAxis yAxis = lineChart2.getAxisLeft();
yAxis.setDrawZeroLine(true);
yAxis.setZeroLineColor(Color.WHITE);// no grid lines
yAxis.setZeroLineWidth(1); //Sets the line-width of the zero line.
yAxis.setAxisMinimum(-10f); // start at zero
yAxis.setAxisMaximum(10f); // the axis maximum is 100
yAxis.setGridColor(0xffff00ff);
yAxis.setTextColor(Color.WHITE);
yAxis.setTextSize(15);
Legend legend = lineChart2.getLegend();
legend.setEnabled(true);
legend.setTextColor(Color.WHITE);
legend.setTextSize(18);
legend.setFormSize(13);
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
}
public void sendAndNext(View view) {
String temValue1= answer1.getText().toString();
//Toast.makeText(secondScreen.this, answer1.getText(),Toast.LENGTH_LONG).show();
String temValue2= answer2.getText().toString();
if (Integer.parseInt(temValue1)==mSlop[0] && Integer.parseInt(temValue2)==mSlop[1]){
Toast.makeText(Main2Activity.this, "תשובה נכונה",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(Main2Activity.this, "תשובה לא נכונה או שלא הוזנו נתונים",Toast.LENGTH_LONG).show();
}
}
}
this is the second XML:
<?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="vertical"
android:weightSum="5"
android:gravity="top"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:background="#drawable/background_color2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.5"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp">
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/lineChart2"
android:layout_width="match_parent"
android:layout_height="400dp"
>
</com.github.mikephil.charting.charts.LineChart>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:weightSum="2"
android:gravity="right">
<EditText
android:id="#+id/answer1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:digits="0123456789."
android:inputType="numberSigned"
android:maxLength="2"
android:hint="הכנס מספר"
android:textColorHint="#78ffd6"
android:textSize="18dp"
android:textColor="#00FFFF"
/>
<TextView
android:id="#+id/question1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:text="למה שווה הפרמטר b ?"
android:textColor="#00FFFF"
android:textSize="25sp"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:weightSum="2"
android:gravity="right"
>
<EditText
android:id="#+id/answer2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:digits="0123456789."
android:maxLength="2"
android:hint="הכנס מספר"
android:textColorHint="#64f38c"
android:textSize="18dp"
android:inputType="numberSigned"
android:textColor="#ff00ff00"
/>
<TextView
android:id="#+id/question2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="מהי נק' החיתוך עם ציר Y?"
android:textColor="#ff00ff00"
android:textSize="25sp"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center">
<Button
android:layout_marginRight="40dp"
android:id="#+id/quickTipBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Quick Tip"
android:background="#android:color/transparent"
android:textColor="#f05053"
/>
<Button
android:id="#+id/sendAndNext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Next One"
android:background="#android:color/transparent"
android:textColor="#ffafbd"
android:onClick="sendAndNext"/>
</LinearLayout>
</LinearLayout>
this is the Logcat error:
FATAL EXCEPTION: main
Process: com.example.rachmani.mythematix_linears, PID: 18087
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.rachmani.mythematix_linears/com.example.rachmani.mythematix_linears.Main2Activity}:
android.view.InflateException: Binary XML file line #0: Error
inflating class
Thanks for your patience...
After a close review of the error log, I've indicated the next error:
com.example.rachmani.mythematix_linears:drawable/background_color2" (7f060055) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060055 a=-1 r=0x7f060055}
The original drawable was:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#0f0c29"
android:endColor="#302b63"
android:type="linear"
android:angle="90"/>
</shape>
</item>
</selector>
Just changed it to:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#141e30"
android:endColor="#243b55"
android:type="linear"
android:angle="90"/>
</shape>
</item>
</selector>
It's seems that the gradient background I tried to use make the problem...
Thanks all for your support.
Please check your second XML file for a closing </LinearLayout> tag in the end.
From the code snippet you have posted,it is evident that you haven't closed the first LinearLayout tag. Hope it helps!
I have very popular question - why the data not showing in the RecycleView.
That is my Adapter:
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
private static final String TAG = "CommentsAdapter";
public ImageView mThumbView;
private List<PostComment> postCommentList;
public CommentsAdapter(List<PostComment> postCommentList) {
this.postCommentList = postCommentList;
}
#Override
public CommentsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(CommentsAdapter.ViewHolder holder, int position) {
PostComment record = postCommentList.get(position);
holder.mAuthorTextView.setText(record.getUser().getFull_name());
holder.mDateTextView.setText(record.getCreated_at());
holder.mTextTextView.setText(record.getText());
}
#Override
public int getItemCount() {
Log.i(TAG + " comments size", Integer.toString(postCommentList.size()));
return postCommentList.size();
}
public void addItems(List<PostComment> postCommentList) {
this.postCommentList.addAll(postCommentList);
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mAuthorTextView;
public TextView mDateTextView;
public TextView mTextTextView;
public ViewHolder(View v) {
super(v);
mAuthorTextView = (TextView) itemView.findViewById(R.id.comment_author);
mDateTextView = (TextView) itemView.findViewById(R.id.comment_date);
mTextTextView = (TextView) itemView.findViewById(R.id.comment_text);
}
}
}
That is my view:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
....>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">
<ImageView
android:id="#+id/comment_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerInside"
android:src="#drawable/cat"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/middleLayout"
android:layout_toRightOf="#+id/comment_image"
android:layout_marginTop="8dp">
<TextView
android:id="#+id/comment_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="Dark Plastic"
android:textColor="?attr/colorPrimary"
android:textSize="14dp" />
<TextView
android:id="#+id/comment_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#+id/comment_author"
android:text="21 minutes"
android:textSize="14dp" />
</RelativeLayout>
<TextView
android:id="#+id/comment_text"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/comment_image"
android:layout_height="wrap_content"
android:maxLines="3"
android:layout_marginTop="8dp"
android:layout_below="#id/middleLayout"
android:text="sd"
android:textColor="#android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="bottom"
android:paddingLeft="16dp"
android:paddingRight="16dp"></View>
That is how I set up the adapter:
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
post_details_comments_list.setLayoutManager(mLayoutManager);
mCommentListAdapter = new CommentsAdapter(new ArrayList<PostComment>());
post_details_comments_list.setAdapter(mCommentListAdapter);
After I set up the adapter, I the getItemCount returns the quantity of the items - so that is not null.
RecycleView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/post_details_mapview"
android:layout_width="wrap_content"
android:layout_height="200dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/post_details_comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/TextLabelStyle"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/post_details_comments_list"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_comment"
android:hint="#string/hint_type_comment"/>
</LinearLayout>
I make the request to the server and here is the callback, where I get the results and set up the adapter:
#Override
public void onAllPostCommentsCallback(AllComments allComments) {
Log.i(TAG + " I've the comments", allComments.getPostCommentList().toString());
mCommentListAdapter.addItems(allComments.getPostCommentList());
post_details_comments_list.setAdapter(mCommentListAdapter);
mCommentListAdapter.notifyDataSetChanged();
}
But the list is not displaying.
What can be the reason?
put null inplace of parent in View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
I've already solved the issue. The problem was that I had RecycleView inside the NestedScrollView
Answer in this post helped me a lot:
How to use RecyclerView inside NestedScrollView?