Android ListView点击监听器不起作用

时间:2018-09-30 18:16:36

标签: android listview

我已经看到许多有关此问题的问题和答案。我几乎尝试了所有方法,但没有一个答案对我没有用。

我正在使用运行Android 8.0.0(API 26)的Samsung S9手机。

如果我尝试以下代码,则会调用setOnItemClickListener。

    mListView = (ListView) findViewById(R.id.azure_photo_list);
    mListView.setDividerHeight(1);
    registerForContextMenu(mListView);
    // ListView Item Click Listener
    mListView.setOnItemClickListener((parent, view, position, id) -> {
        Intent intent = new Intent(getBaseContext(), AzureImageActivity.class);
        intent.putExtra("image", images[position]);
        startActivity(intent);

    }); 

   String[] images = ImageManager.ListImages();
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(AzurePhotoList.this,
                            android.R.layout.simple_list_item_1, android.R.id.text1, images);
   mListView.setAdapter(adapter);

请注意,布局来自Android系统,文本视图来自Android系统。如果我提供自己的布局,如下所示:-

String[] images = ImageManager.ListImages();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AzurePhotoList.this,                         
R.layout.content_azure_photo_list,R.id.azure_list_item_name, images);

然后不再调用setOnItemClickListener。怎么了?

1 个答案:

答案 0 :(得分:1)

我克隆了存储库,发现代码库中几乎没有问题。

要使点击侦听器正常工作,您至少必须更改以下内容:-

  1. 在xml(如下所述)中,您已使用ConstraintLayout,并且未对任何视图赋予约束。结果,每个视图都绘制为(0,0)。
  2. Handler的实现不正确

为使您有一个良好的开端,请在代码中更改下面提到的2个文件,然后单击侦听器将始终起作用。

注意:我花了很少的时间编写代码,这不是可用于生产的代码。在下面给您要点和代码,以指明正确的方向。

  1. AzurePhotoList

    public class  AzurePhotoList extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    
    private final static int MY_REQUEST_PERMISSIONS_READ_EXTERNAL_STORAGE = 102;
    private String[] images;
    private ListView mListView;
    private Handler handler;
    private String[] images_lists;
    
    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_azure_photo_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                 images_lists = msg.getData().getStringArray("images_list");
     //                AzurePhotoList.this.images = images;
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(AzurePhotoList.this,
                    R.layout.content_azure_photo_list, R.id.azure_list_item_name, images_lists);
                        /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(AzurePhotoList.this,
                                R.layout.content_azure_photo_list,R.id.azure_list_item_name, images);*/
                mListView.setAdapter(adapter);
            }
        };
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    
        mListView = (ListView) findViewById(R.id.azure_photo_list);
        mListView.setDividerHeight(1);
        registerForContextMenu(mListView);
    
        // ListView Item Click Listener
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(AzurePhotoList.this.getBaseContext(), AzureImageActivity.class);
            intent.putExtra("image", images_lists[position]);
            AzurePhotoList.this.startActivity(intent);
    
        });
    
        loadImageFromAzure();
    }
    
    private void loadImageFromAzure(){
        Thread th = new Thread(new Runnable() {
            public void run() {
                try {
                    final String[] images = ImageManager.ListImages();
                    Bundle bundle = new Bundle();
                    bundle.putStringArray("images_list", images);
                    Message message = new Message();
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
                catch(Exception ex) {
                    final String exceptionMessage = ex.getMessage();
                    handler.post(new Runnable() {
                        public void run() {
                            Toast.makeText(AzurePhotoList.this, exceptionMessage, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }});
        th.start();
    }
    
    //your remaining code...
    //...
    
  2. content_azure_photo_list.xml

    <ListView
        android:id="@+id/azure_photo_list"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/azure_list_item_name"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:textAppearance="@android:style/TextAppearance.Medium" />
    
    </LinearLayout>