2017-06-06 6 views
0

私は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> 

答えて

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内では、必要なものを実装できます。

おはよう!