I am trying to implement Custom ListView into my android app. But it doesn't works. I've tried to solve this by adding the following lines, but it didn't work for me.
android:descendantFocusability="blocksDescendants"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
The codes are given below-
my_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#00aee5">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/laptop"
android:layout_gravity="center_vertical"
android:descendantFocusability="blocksDescendants"/>
<TextView
android:id="@+id/mytxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Demo"
android:textColor="#fff"
android:textSize="18sp"
android:layout_margin="5dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Laptop"
android:background="#53e1e9"
android:textColor="#fff"
android:textSize="20sp"
android:padding="10dp"
android:gravity="center"/>
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="2dp">
</ListView>
</LinearLayout>
MainActivity.java
package com.android.imran.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView) findViewById(R.id.mylist);
final String[] my_laptops={"Asus","Acer","Lenovo","HP","DELL","Surface","MacBook"};
ArrayAdapter<String> my_adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.my_list, R.id.mytxt,my_laptops);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "U clicked on "+my_laptops[position], Toast.LENGTH_LONG).show();
}
});
list.setAdapter(my_adapter);
}
}
What is causing the problem? How do I fix it?
Try this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#00aee5"
android:descendantFocusability="blocksDescendants">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/mytxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Demo"
android:textColor="#fff"
android:textSize="18sp"
android:layout_margin="5dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
If you are using Image-Buttons in list item, try this:
android:descendantFocusability="blocksDescendants"
in the mylist or row activity's main layout.
Now for Image-Button(s) in the mylist , try this:
android:focusableInTouchMode="true"
Let me know, if it works for you.!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.