2015-12-11 14 views
11

Tôi có cửa sổ bật lên trong trang có chế độ xem danh sách. Tôi đã tạo ra thiết kế của popup trong một xml riêng biệt và tải nó trên một số nút bấm trong trang chính của tôi. Cửa sổ bật lên có một listview với mỗi hàng có một hình ảnh và một textview. Tôi không thể có được lựa chọn hàng trong listview trong Android 4.1 nhưng nó đang làm việc trong 5.0. Bất cứ ai có thể vui lòng đề nghị tôi giải pháp? listview:không thể chọn hàng trong chế độ xem danh sách trong cửa sổ bật lên trong 4.1 nhưng hoạt động trong android 5.0

<ListView android:id="@+id/lstSites" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fastScrollEnabled="true" 
     android:layout_margin="5dp" 
     android:descendantFocusability="blocksDescendants"></ListView> 
</LinearLayout> 

listview Item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:descendantFocusability="blocksDescendants" 
android:focusable="false" 
android:focusableInTouchMode="false"> 
<ImageView 
    android:id="@+id/thumbImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="10dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false"/> 
<TextView 
    android:id="@+id/tvSite" 
    android:textSize="20dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:text="Name" 
    android:focusable="false" 
    android:focusableInTouchMode="false"/> 

thêm hàng nhấp chuột listner:

lstSiteMenu = (ListView) popupView.findViewById(R.id.lstSites); 

      adapter = new MenuItemsAdapter(SiteActivity.this, arrMenu); 
      // Attach the adapter to a ListView 
      lstSiteMenu.setAdapter(adapter); 
      lstSiteMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             final int position, long id) { 
      } 
+0

bất kỳ lỗi trong logcat?khi bạn nói hàng được chọn bạn có nghĩa là onItemClick không hoạt động hoặc bạn không nhận được vị trí? – virendrao

+1

Xóa 'android: descendantFocusability = "blocksDescendants" 'khỏi' ListView'. – Piyush

Trả lời

0

Bạn cần phải loại bỏ android:descendantFocusability="blocksDescendants" từ ListView của bạn.

Đồng thời xóa android:focusable="false"android:focusableInTouchMode="false" khỏi bố cục hàng của bạn trong ImageViewTextView.

Các mục danh sách được coi là nhấp được, có vô hiệu hóa nó lấy tập trung

1

loại bỏ thuộc tính này từ listview, android:descendantFocusability="blocksDescendants"

android:focusable="false" 
android:focusableInTouchMode="false" 

từ văn bản và ImageView.

Và thử Đây là ví dụ mẫu,

String names[] ={"A","B","C","D"}; 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); 
     LayoutInflater inflater = getLayoutInflater(); 
     View convertView = (View) inflater.inflate(R.layout.row_file, null); 
     alertDialog.setView(convertView); 
     alertDialog.setTitle("List"); 
     ListView lv = (ListView) convertView.findViewById(R.id.listView); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names); 
     lv.setAdapter(adapter); 
     alertDialog.show(); 

khác mã của bạn là tốt. nó hoạt động.

xem qua số link để biết thêm chi tiết.

Một số khác official link cho tương tự.

+0

bạn đã thử điều này chưa? @pankaj –

1

Hãy thử tất cả các giải pháp:


popupwindow.setFocusale(true); 
listView xml : "android:focusable="true" 

không sử dụng lstSiteMenu.setOnItemClickListener
thay vì đi đến Adapters bạn getView và thêm

convertView.setOnClickListener 

Hy vọng nó sẽ giúp bạn!

-1

Set hàng nghe onclick trong lớp bộ chuyển đổi

public class MenuItemsAdapter extends Base Adapter{ 
     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      /* 
          Paste your adapter code 
      */ 
       customView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Log.v("log", "You clicked at pos "+position); 
       } 
      }); 
     } 
} 

Mã này làm việc trong tất cả các phiên bản sdk.

Cũng loại bỏ các thuộc tính từ các tập tin xml

android:descendantFocusability="blocksDescendants" 
android:focusable="false" 
android:focusableInTouchMode="false" 
Các vấn đề liên quan