i try to implements my own multi expandable RecyclerView it'w work fine for one sub-item but when i want sub-sub-item the view display but i need to click on it several times.
Example of hierarchy :
Title1
SubTitle1
SubTitle2
SubTitle3
SubSubTitle1
SubSubTitle2
Title2
...
EDIT : The problem is only on the SubTitle to display SubSub'sItem. I think i need to notify parent adapter that my view changed but i doesn't work to.
To do that i used RecyclerView with itemLayout who contain too an RecyclerView and when i click on item i set adapter off the child RecyclerView. I use custom LinearLayoutManager i found here https://stackoverflow.com/a/29261667/3289338.
My onBindViewHolder :
#Override
public void onBindViewHolder(final DrawerViewHolder holder, final int position) {
if (holder.type == TYPE_EXPANDABLE_ITEM) {
final ItemDrawer item;
if (header) {
item = itemDrawers.get(position - 1);
} else {
item = itemDrawers.get(position);
}
holder.textView.setText(item.getTitle());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (item.isOpen()) {
item.setOpen(false);
holder.recyclerView_child.setVisibility(View.GONE);
notifyItemChanged(position);
} else {
item.setOpen(true);
holder.recyclerView_child.setVisibility(View.VISIBLE);
holder.recyclerView_child.setAdapter(new DrawerAdapter(item.getSubItem(), drawer, fragmentManager, false));
holder.recyclerView_child.setLayoutManager(new MyLinearLayoutManager(holder.itemView.getContext().getApplicationContext()));
notifyItemChanged(position);
}
}
});
} else if (holder.type == TYPE_SIMPLE_ITEM) {
final ItemDrawer item;
if (header) {
item = itemDrawers.get(position - 1);
} else {
item = itemDrawers.get(position);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (item.isActivity()) {
holder.itemView.getContext().startActivity(item.getActivityIntent());
} else {
fragmentManager.beginTransaction().replace(R.id.drawer_frame_layout, item.getFragment()).commit();
}
}
});
} else {
//header
}
}
And the 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="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:paddingBottom="8dp"
android:paddingTop="8dp">
<ImageView
android:id="#+id/imageView_drawer_row_expandable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:src="#drawable/ic_drawer" />
<TextView
android:id="#+id/textView_drawer_row_expandable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/imageView_drawer_row_expandable"
android:paddingStart="12dp"
android:paddingTop="4dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="#+id/imageView_drawer_row_expandable_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#00000000"
android:paddingEnd="16dp"
android:src="#android:drawable/arrow_up_float" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_drawer_child"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:background="#ffffff"
android:paddingStart="16dp"
android:scrollbars="vertical"
android:visibility="gone" />
</LinearLayout>
Thanx.
https://github.com/itsnothingg/RecursiveRecyclerView
I made a open source for this.
you can easily implement multi-expanding recyclerview and use different layout for each expanding depth.
i've got same issue, solution is set setHasFixedSize(false) to your parent adapter
NOTE: im using vanilla LinearLayoutManager
Related
I'm trying to add a ripple effect for the entire row (like we see in listview) in my Recycler View when the row is clicked.
I have tried
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
But still, I couldn't get the desired behavior. Even I have tried
android:foreground="?android:attr/selectableItemBackground"
But still no improvement
My listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/heading"
android:text="Title"
android:textSize="20dp"
android:paddingTop="5dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_toLeftOf="#+id/subHeading"
android:id="#+id/size"
android:text="Sub Title"
android:paddingTop="3dp"
android:paddingLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
And My layout with Recycler View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<androidx.recyclerview.widget.RecyclerView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="#dimen/list_item_spacing_half"
android:paddingBottom="#dimen/list_item_spacing_half"
android:layout_alignParentTop="true"
tools:context=".ListFragment"
tools:listitem="#layout/listrow" />
<Button
android:id="#+id/Save"
android:layout_width="match_parent"
android:text="Save"
android:layout_below="#+id/list"
android:textAllCaps="false"
android:layout_height="wrap_content"/>
</RelativeLayout>
Here is my adapter source code
private class listAdapter extends RecyclerView.Adapter<ViewHolder> {
private final int mItemCount;
final ArrayList<String> mTitles;
final ArrayList<String> msTitles;
ArrayList<RadioButton> radioButtons=new ArrayList<>();
listAdapter(int itemCount, ArrayList<String> titles, ArrayList<String> sTitles) {
mItemCount = itemCount;
mtitles=titles;
msTitles=sTitles;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.sTitle.setTag(position);
holder.radioButton.setTag(position);
holder.title.setTag(position);
holder.title.setText(mTitles.get(position));
holder.sTitle.setText(msTitles.get(position));
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
ItemCheckChanged(v);
}
};
holder.radioButton.setOnClickListener(listener);
holder.sTitle.setOnClickListener(listener);
holder.title.setOnClickListener(listener);
if(radioButtons.size()==0){
holder.radioButton.setChecked(true);
selectedItem=0;
}
radioButtons.add(holder.radioButton);
}
#Override
public int getItemCount() {
return mItemCount;
}
void ItemCheckChanged(View v){
selectedItem=(int)v.getTag();
for(RadioButton radioButton:radioButtons){
radioButton.setChecked(false);
}
radioButtons.get(selectedItem).setChecked(true);
notifyDataSetChanged();
}
public int getSelectedIndex(){
return selectedItem;
}
}
NOTE: I'm using these all things in BottomSheetDialog may it be reason?
You should set background to views that have set a click listener.
If you want to set the listener to the whole row you need to call only:
holder.itemView.setOnclickListener() and remove rest of them.
I can't click on items in RecyclerView. For example, as shown in the code, I gave Tittle a click feature, but I can't click right now. In the same way I gave deleteimage a click feature, but I can't click. What do I need to do to make the items clickable? Where is the problem? I can't click on the items in RecyclerView, that's the problem.
XML Source Code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="horizontal">
<ImageView
android:id="#+id/productImage"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/roomlocation" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="9dp"
android:text="productAdi"
android:textSize="19dp"
android:clickable="true"
android:textStyle="bold" />
<TextView
android:id="#+id/productDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:text="Cihaza ait eklenmiş odalar"
android:textSize="14dp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/deleteproduct"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:clickable="true"
android:src="#drawable/ic_delete_yellow" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="stechome.berkeylen.firebasedatabase.RoomsActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRooms"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
RecyclerView Class Source:
public class RoomsAdapter extends RecyclerView.Adapter<RoomsAdapter.MyViewHolder> {
DatabaseReference ref = mDatabase.child("0").child("Rooms");
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
super(view);
productImage = (ImageView)
itemView.findViewById(R.id.productImage);
title = (TextView) view.findViewById(R.id.title);
deleteImage = (ImageView) view.findViewById(R.id.deleteproduct);
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String data = snapshot.getValue(String.class);
Rooms.add(data);
title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = 0;
position = getAdapterPosition();
if (getAdapterPosition() == position){
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("0");
rootRef.child("titles").child("0").child("places").setValue(Rooms.get(position));
}
}
});
}
deleteImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v == deleteproduct) {
deleteproduct(getLayoutPosition());
}
}
});
}
Set click listeners on recycler items by overriding onBindViewHolder method of adapter:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myTextView.setOnClickListener(...);
}
here you can find an example: https://developer.android.com/guide/topics/ui/layout/recyclerview
I had the same problem. After trying many things, the only one that worked was to add an atribute on the XML of the item layout:
onClick = the name of the method / function you want to trigger
I came across this UI and am trying to use it.
In the image what I guess is, the root layout is a Scroll view.
The top view inside the ScrollView is Relative layout and below it is ListView which gets loaded depending on the dates selected. (not sure)
When the view is scrolled up the button present on the top View aligns itself to the top of the parent layout.
And then the rest is scrolled like a listview.
When scrolled down again the top view is visible.
NB: I have tried to apply what I feel like has been used for such effect. But I am not able to align the button of the top view to the parent layout. If needed I can post the code.
Please help me to find what exactly is the logic behind this effect.
Thanks.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.mypackage.Appointments_Main_ActivityFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/linear"
android:layout_height="wrap_content">
<TextView
android:text="From Date"
android:id="#+id/frm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip" />
<TextView
android:text="To Date"
android:id="#+id/to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip" />
</LinearLayout>
<Button
android:id="#+id/alertbtn"
android:layout_below="#id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/DarkGray"
android:padding="16dp"
android:text="Alerts"
android:textColor="#color/accentforGray" />
<ListView
android:layout_below="#id/alertbtn"
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#B29090" >
</ListView>
</RelativeLayout>
I solved the problem myself.
For getting the same look and feel as mentioned in the image.
I did the following:
1) Defined 3 layouts used in ListView.
list_top_one -> for top view having from date and to date text view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/frmdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="from date"/>
<TextView
android:id="#+id/todate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="to date"/>
</LinearLayout>
b) list_item_in_middle -> for having button like feel
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:id="#+id/llHeader">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_background"
android:text="OK"
android:padding="16dp"/>
</LinearLayout>
c) list_items -> for holding the dynamically loaded value.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFBB00"
android:orientation="vertical" >
<TextView
android:textColor="#000"
android:layout_margin="30dp"
android:id="#+id/button1"
android:layout_gravity="center"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2) Main activity layout.
a) Contains a listview
b) And includes a layout that is to be shown as button.
<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=".MainActivity">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<!-- This LinearLayout's visibility is toggled -->
<include layout="#layout/list_item_in_middle" />
</RelativeLayout>
Reference:
Making a middle element to get stuck in the header (ScrollView/ListView)
3) ListAdapter class
public class ListAdapter extends BaseAdapter {
public Context mContext;
TextView btCurrent;
ArrayList<Integer> newarrays = new ArrayList<>();
ListAdapter(Context context,Integer[] arrval) {
mContext = context;
newarrays.addAll(Arrays.asList(arrval));
}
public void InitializeValues(Integer[] arrval){
newarrays.addAll(Arrays.asList(arrval));
}
#Override
public int getCount() {
return newarrays.size();
}
#Override
public Object getItem(int arg0) {
return newarrays.get(arg0);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position == 0){
convertView = inflater.inflate(R.layout.list_item_in_middle, null);//Adding btn layout
}else {
convertView = inflater.inflate(R.layout.list_items, null);// Adding other elements
btCurrent = (TextView) convertView.findViewById(R.id.button1);
if ((Integer) getItem(position) == 3) {
btCurrent.setText("Number " + getItem(position) + " is sticky");
} else {
btCurrent.setText("" + getItem(position));
}
}
return convertView;
}
}
4) MainActivity
a) OnScrollListener for attaching the btn like list item on top.
llHeader = (LinearLayout) findViewById(R.id.llHeader);//from the list_item_in_middle layout
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem > 0) { // 1st row will stick
llHeader.setVisibility(View.VISIBLE);
} else {
llHeader.setVisibility(View.GONE);
}
}
});
b) OnItemClickListener of listview
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int a = 0;
if(position == 0){//for clicking from date and to date.
TextView frm = (TextView) view.findViewById(R.id.frmdate);
frm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"From date clicked",Toast.LENGTH_SHORT).show();
}
});
TextView to = (TextView) view.findViewById(R.id.todate);
to.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"to date clicked",Toast.LENGTH_SHORT).show();
}
});
}else {
if(position == 1) {//for clicking the btn like list element.
swap();
Toast.makeText(MainActivity.this,"Header clicked",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
}
}
});
c) Linearlayout click function for element that gets stuck to the top
llHeader.setOnClickListener(new View.OnClickListener() { //for clicking the elements that gets stuck to the top
#Override
public void onClick(View v) {
swap();
Toast.makeText(MainActivity.this,"Header clicked",Toast.LENGTH_SHORT).show();
}
});
d) swap function called from linearlayout and the list element at position 1
public void swap(){
ltAdapter.notifyDataSetChanged();
new1 = new Integer[]{20, 21, 22, 23, 24, 25, 26, 27, 28};
ltAdapter.InitializeValues(new1);
}
NB:If any focusable view is taken in the listview, the OnItemclickListener doesnot work, read in stackoverflow itself
Finally the output:
before the second element click
after clicking
after scrolling
I think it happens in this way: when you scroll the page, linear layout gets invisible successfully but when you reache the button by doing scroll, the focus goes to listview and so it makes listview to scrolls instead of the whole page.
Do a fast scroll or scroll the page by swiping the button, not the listview and check if botton gets invisible.
Could someone give me an idea how to begin implementing vertical, non-linear stepper control described in the Android Material Design guide here:
http://www.google.com/design/spec/components/steppers.html
you can check this library , however this is still in development.
just extend the mobileStepperSimple class and implement the methods init,onFinished.
you can add the steppers as fragments by extending the stepperFragment and implement onNextButtonHandler to handle next button click.
check the demo for more usage.
any contributions and optimization will be helpful.
Well not exactly the same but as per my requirement, I developed custom VERTICAL STEPPER.
Below is the source code of whole demo.
activity_main.xml
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"/>
</LinearLayout>
Single Item for List(Design your single item of your stepper in raw.xml file)
raw.xml
<?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:layout_marginTop="25dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_upper_line"
android:layout_width="wrap_content"
android:layout_height="20dp"
app:srcCompat="#drawable/order_status_line" />
<ImageView
android:id="#+id/iv_circle"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/circle_o" />
<ImageView
android:id="#+id/iv_lower_line"
android:layout_width="wrap_content"
android:layout_height="20dp"
app:srcCompat="#drawable/order_status_line" />
</LinearLayout>
<LinearLayout
android:id="#+id/ly_status"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Order Rcived"
android:layout_gravity="left"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ly_orderstatus_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|top"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageview"
android:layout_width="20dp"
android:layout_height="20dp"
app:srcCompat="#drawable/ic_restore_black" />
<TextView
android:id="#+id/tv_orderstatus_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|top"
android:text="8:30am,Jan 31,2018"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
orderStatusList();
}
private void orderStatusList() {
ArrayList<OrderStatusModel> arrayOfStatus =OrderStatusModel.getStoreDetail();
OrderStatusAdapter adapter = new OrderStatusAdapter(this, arrayOfStatus);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
Adapter Class
class OrderStatusAdapter extends ArrayAdapter<OrderStatusModel> {
Context context;
ArrayList<OrderStatusModel> order_status;
boolean isOn = false;
public OrderStatusAdapter(Contextcontext,ArrayList<OrderStatusModel>order_status{super(context, 0, order_status);
this.context = context;
this.order_status = order_status;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view if(convertView==null)convertView=LayoutInflater.from(getContext()).inflate(R.layout.raw,parent,false);
}
// Get the data item for this position
OrderStatusModel order_status_data = getItem(position);
// Lookup view for data population
ImageView iv_upper_line = (ImageView)
convertView.findViewById(R.id.iv_upper_line);
ImageView iv_lower_line =(ImageView)
convertView.findViewById(R.id.iv_lower_line);
final ImageView iv_circle = (ImageView) convertView.findViewById(R.id.iv_circle);
TextView tv_status = (TextView) convertView.findViewById(R.id.tv_status);
TextView tv_orderstatus_time =(TextView)
convertView.findViewById(R.id.tv_orderstatus_time);
LinearLayout ly_orderstatus_time = (LinearLayout)
convertView.findViewById(R.id.ly_orderstatus_time);
LinearLayout ly_status = (LinearLayout) convertView.findViewById(R.id.ly_status);
// Populate the data into the template view using the data object
tv_status.setText(order_status_data.getTv_status());
tv_orderstatus_time.setText(order_status_data.getTv_orderstatus_time());
if(position == 0){
iv_upper_line.setVisibility(View.INVISIBLE);
}
else if (position == order_status.size()-1){
iv_lower_line.setVisibility(View.INVISIBLE);
ly_orderstatus_time.setVisibility(View.GONE);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iv_circle.setBackgroundResource(R.drawable.bullseye);
Toast.makeText(context, "You Clicked at
item"position,Toast.LENGTH_SHORT).show();
}
});
// Return the completed view to render on screen
return convertView;
}
}
Model Class
private String tv_status;
private String tv_orderstatus_time;
public OrderStatusModel(String tv_status, String tv_orderstatus_time) {
this.tv_status = tv_status;
this.tv_orderstatus_time = tv_orderstatus_time;
}
public String getTv_status() {
return tv_status;
}
public void setTv_status(String tv_status) {
this.tv_status = tv_status;
}
public String getTv_orderstatus_time() {
return tv_orderstatus_time;
}
public void setTv_orderstatus_time(String tv_orderstatus_time) {
this.tv_orderstatus_time = tv_orderstatus_time;
}
public static ArrayList<OrderStatusModel> getStoreDetail() {
ArrayList<OrderStatusModel> status = new ArrayList<OrderStatusModel>();
status.add(new OrderStatusModel("Order Rcived", "8:30am,Jan 31,2018"));
status.add(new OrderStatusModel("On The Way", "10:30am,Jan 31,2018"));
status.add(new OrderStatusModel("Delivered", "aaaaaa"));
return status;
}
Because no Support Library solution exists (still) I have tried several of these libraries in a recent project. My favourite (for appearance, smoothness and functionality) was This Project by "ernestoyaquello". I also added some options to it on My Fork.
The only thing to note with this, is that it does not use an 'adapter' class but instead uses a callback interface.
Demo Screen from Git:
In my project i have a recycler view which contains a text view in every row. when i select a text view in a row text views in some random rows are also selected and also whatever is typed into it, it is also showing in those rows.Here is my code.
onBindViewHolder
public void onBindViewHolder(final MainContentViewHolder holder, int position) {
Log.e("Sony","onBindViewHolder");
Item currentItem = items.get(position);
if (currentItem.imageURL != null) {
imageLoader.get(currentItem.imageURL, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
Log.e("Sony","onResponseImage");
holder.itemImage.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Sony","onErrorImage");
holder.itemImage.setImageResource(R.drawable.default_product);
}
});
}
//holder.itemImage.setImageBitmap(BitmapFactory.decodeStream((InputStream) new URL().getContent());
holder.itemPrice.setText(currentItem.price + "");
holder.itemName.setText(currentItem.itemName);
}
ViewHolder
public MainContentViewHolder(View itemView) {
super(itemView);
itemImage = (ImageView) itemView.findViewById(R.id.imgItemPic);
itemName = (TextView) itemView.findViewById(R.id.lblItemName);
itemPrice = (TextView) itemView.findViewById(R.id.lblItemPrice);
txtQty = (EditText) itemView.findViewById(R.id.txtQty);
btnAddToCart = (Button) itemView.findViewById(R.id.btnAddToCart);
btnAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Item item = items.get(getPosition());
CartMessageHandler.showToastMessage(context, item.itemName + " : " + item.price, Toast.LENGTH_LONG);
}
});
}
activity onCreate
mainListView = (RecyclerView) findViewById(R.id.recyclerList);
mainListView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
recyclerViewListAdapter = new MainContentRecyclerAdapter(this);
mainListView.setAdapter(recyclerViewListAdapter);
getData("fruits%20&%20vegetables");
mainListView.setLayoutManager(new LinearLayoutManager(this));
Custom Row for recyclerView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cardItemHolderRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgItemPic"
android:layout_width="#dimen/itemImageDimen"
android:layout_height="#dimen/itemImageDimen"
android:layout_centerVertical="true"
android:contentDescription="#string/productImage"
android:src="#drawable/default_product"
android:elevation="2dp"
android:focusableInTouchMode="false"
android:layout_marginTop="#dimen/loginWidgetTopMargin" />
<LinearLayout
android:layout_toRightOf="#+id/imgItemPic"
android:paddingLeft="10dp"
android:id="#+id/cardItemHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/lblItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/loginWidgetTopMargin"
android:text="Item Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblItemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/loginWidgetTopMargin"
android:text="Rs.250"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/primaryColor" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/loginWidgetTopMargin"
android:orientation="horizontal">
<TextView
android:id="#+id/lblItemQty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/quantity"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtQty"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/btnAddToCart"
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/cart_a16"
android:text="#string/addToCart" />
</LinearLayout>
what is wrong in my code? Can anyone suggest a way to fix this
RecyclerView reuses row views that was created earlier. That's why you see earlier typed text in random rows of your RecyclerView: you simply don't reset row view stuff state in onBindViewHolder.
So to avoid bug you should reset components state in onBindViewHolder: clear txtQty and etc. In case of expensive or heavyweight data documentation is recommended releasing this data with setRecyclerListener(..).