0

I've made an bottom sheet with listview which working really fin but, I also want, that listview items be clickable.

main_activity:

public class MainActivity extends AppCompatActivity {

String[] serialNumber = new String[]{"XX-453-CS","KF-009-KD","GD-098-ML","SG-865-43","IJ-736-OK"};
String[] fee = new String[]{"50თ","74თ","30თ","88თ","65თ"};
String[] avtime = new String[] {"02:36","03:32","10:55","00:36","03:46"};

Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.btn);
    final ArrayList<Offers> listViewData = getOffers();
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BottomSheetDialog dialog = new BottomSheetDialog(MainActivity.this);
            View parentView = getLayoutInflater().inflate(R.layout.bottom_sheet_layout,null);
            dialog.setContentView(parentView);
            BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) parentView.getParent());
            bottomSheetBehavior.setPeekHeight(
                    (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,100,getResources().getDisplayMetrics()));
            BottomSheetListView listView = (BottomSheetListView) dialog.findViewById(R.id.listViewBtmSheet);
            OffersAdapter adapter = new OffersAdapter(MainActivity.this,listViewData);
            listView.setAdapter(adapter);
            dialog.show();
        }
    });
}

private ArrayList<Offers> getOffers() {
    ArrayList<Offers> list =new ArrayList<>();
    for(int j=0;j<5;j++) {
        for (int i = 0; i < fee.length; i++) {
            Offers offer = new Offers(serialNumber[i], fee[i], avtime[i]);
            list.add(offer);
        }
    }
    return list;
}
}

Bottomsheetlistview is from here : ListView in BottomSheet

and finally bottom sheet layout:

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Offers"
            android:textSize="20sp"
            android:layout_gravity="center"/>

        <ge.yep.bb.BottomSheetListView
            android:id="@+id/listViewBtmSheet"
            android:divider="@color/colorPrimary"
            android:dividerHeight="10dp"
            android:layout_width="match_parent"
            android:layout_height="250dp" />

</LinearLayout>
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Tornike Kurdadze
  • 955
  • 2
  • 10
  • 24

1 Answers1

2

Add this to your code:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //TODO:: Whatever you want to do
        }
    });

This sets an individual click listener for each item. Inside the onItemClick you can implement anything you want.

Good day!

Ellisan
  • 563
  • 8
  • 22