底部可点击列表视图

时间:2017-06-06 12:18:43

标签: android

我制作了一张带有listview的底页,其工作非常有效,但我也希望listview项可以点击。

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来自:ListView in BottomSheet

最后是底部工作表布局:

<?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>

1 个答案:

答案 0 :(得分:2)

将此添加到您的代码中:

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

这为每个项目设置了单独的单击侦听器。在onItemClick中你可以实现你想要的任何东西。

美好的一天!